Question:
Min and Max Numbers in Javascript?
anonymous
2013-04-08 23:56:44 UTC
This is what I have so far, I need to calculate the min and max for the evenSum and oddSum.

I have put the formula that I think I need to use but am unsure how to incorporate it into what I already have.

Four answers:
Almighty Wizard
2013-04-09 05:55:24 UTC
The logic you have currently added will not work for your problem. The algorithm you have in place is designed for an array of input, but you are only taking one input at a time. Instead of storing input as an array, it would be much easier to calculate the min/max values on the fly since you are already determining what is odd and what is even.



Like your calculation of the sum and count, the min/max values need to be calculated inside the loop.



You should create 4 new variables: minEven, maxEven, minOdd, maxOdd.



Set the min values to positive infinity, and the max values to negative infinity.



Then, in each section of your IF-ELSE statement, you need to do the min and max tests. The tests will be nearly identical in both the even and odd sections, with the only different being the variables in use.



Here is the check you can use to determine the max value, which I hope you can adapt yourself to work with the min value:



if(max < userInput) {

max = userInput;

}

// don't need an else because reassignment should only happen if the value changes



Remember, you will have one max check and one min check in each section of your IF-ELSE block.



Good luck!
AnalProgrammer
2013-04-09 00:47:49 UTC
Give min an initial high value

var min = 9999;



Max will need a low number

var max = -9999;



Have fun.
Erika
2016-10-26 16:45:07 UTC
sounds like a extra useful-suited software to using a for loop... is it a call for that you employ even as? dumb... dumb dumb dumb...
Continue reading on narkive:
Loading...