Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

What should max be initialised to?

NUMBER_OF_INPUTS = 5;

var i;
var max;
var userInput; // Input from user

max = ???? ;
for(i=0; i<NUMBER_OF_INPUTS; i++)
{ userInput = parseInt(prompt('Enter input: '));
if (userInput > max)
max = userInput;
}

alert('Max: ' + max);
share|improve this question
1  
What is max, anyway? Please clarify. – codesparkle Aug 14 '12 at 4:38
1  
It's on SO too: stackoverflow.com/questions/11945546/… – palacsint Aug 14 '12 at 19:06

closed as off topic by RoToRa, James Khoury, Corbin, palacsint, sepp2k Aug 15 '12 at 20:26

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

If the final value must come from the users, you could initialize it to:

max = Number.MIN_VALUE;

Otherwise, it should be initialized to a default value that would be valid in case all the inputs are less than it. In this case it's entirely dependant on your application.

Also, the test can be shortened to:

max = Math.max(max, userInput);
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.