+ 31
Getting the maximum number out of a list of numbers!
I need someone to show me an example of a function that returns the maximum number out of any given amount of parameters. NOTE:please do not use predefined functions like Math.max() e.t.c Do not use anything from Math library,create your own functions from scratch.This is not a challenge,i just need help😊Thanks!
62 Réponses
+ 14
There's a few things that would determine the best way to do that including how are the arguments being passed in ie as in an array vector or individual arguments. if your passing as an array or placing them in an array the easiest method is to do a sort placing them in order.
+ 30
//old code , get some idea from it
//its for minimum , but no difference of logic used
https://code.sololearn.com/c4T4311YtAQQ/?ref=app
+ 21
Can I use Array methods (Array.prototype.reduce)?
https://code.sololearn.com/Wz78NUTnIIH3/?ref=app
+ 16
The most basic way I can think of is this
https://code.sololearn.com/Wj1VzZWJ6sE5/?ref=app
+ 14
One-liner in JS
https://code.sololearn.com/WLMp4F11Jj60/?ref=app
+ 13
https://code.sololearn.com/c3ALOI412dGu/?ref=app
+ 12
what does knowing the strength and weakness of JavaScript have to do here?I'm returning the closest IP addresses of individuals,something the "standard library function" cannot do,thats why I need to know how it works so I can modify it.
+ 12
take a variable max with value of 0 and put conditional statement
example:
int max=0;
using loop according to your question
{
if(number>max)
{
max=number;
}
update your number;
}
your work is doooone!😊
+ 11
Hey, try this one)
https://code.sololearn.com/cBCLZ2CP0s6b/?ref=app
+ 10
@Brains, you didn't tag the question, so is the question for Java or JavaScript? or other language?
+ 10
The first thing that comes to my mind is this:
https://code.sololearn.com/csPGq6fm4oca/?ref=app
+ 9
oohhh...a sort,thanks Micheal,didnt think of that
+ 9
https://www.sololearn.com/Discuss/1093044/?ref=app
check that if you want..
+ 8
write in your array as the function's argument
https://code.sololearn.com/Wcujj23Iz5S7/?ref=app
+ 8
https://code.sololearn.com/compq9ELbTq7/?ref=app
Maybe I'm the only one here who suffers with python and tries ruby 😓
+ 7
Assuming JavaScript, you can use arguments object for functions with variable number of arguments.
function foo()
{
if(arguments.length < 1) return 0;
var rv = arguments[0] < 0 ? 0 : arguments[0];
for(i = 0; i < arguments.length; i++)
if(rv < arguments[i]) rv = arguments[i];
return rv;
}
window.onload = function()
{
console.log(foo(-5,-3,-1,0,1,3,5));
console.log(foo(26,2,2018));
console.log(foo(1,2,4,8));
console.log(foo(-10));
console.log(foo());
};
I still don't get what this has to do with IP number though : )
+ 7
array=[a,b,c,...,x]
count=size(array)
max= minimum posible value
for(indx=1 to count)
if array[indx] > max
max=array[indx]
position=indx
end
end
pseudocode 😀
+ 7
reduce with Kotlin
https://code.sololearn.com/cEu1IEY4aKYv/?ref=app
+ 6
It can be done by sorting the list in ascending or descending order.. and print the first or last number