i have a problem on our javascript class; i need to create a script where users can input 5 integers then the program outputs the highest and the lowest numbers entered by the user. i was able to do that but the problem is , whenever i input same integers together, either the highest or the lowest, it outputs as much as the number of times i entered that integer.. ex.. i entered 22345 output: 22 5 need help gurus
same issue over and over again... without more information, no help will be forthcoming... post the code boy!
<script> var firstinteger, secondinteger, thirdinteger, firstnum, secondnum, thirdnum, fourthinteger, fourthnum, fifthinteger, fifthnum; firstinteger=window.prompt(" Enter first number:", "0"); firstnum=parseInt(firstinteger); secondinteger=window.prompt(" Enter second number:", "0"); secondnum=parseInt(secondinteger); thirdinteger=window.prompt(" Enter third number:", "0"); thirdnum=parseInt(thirdinteger); fourthinteger=window.prompt(" Enter fourth number:", "0"); fourthnum=parseInt(thirdinteger); fifthinteger=window.prompt(" Enter fifth number:", "0"); fifthnum=parseInt(fifthinteger); if(firstnum > secondnum && firstnum > thirdnum && firstnum > fourthnum && firstnum > fifthnum) document.writeln(firstnum+" is the highest number"); else if(firstnum <= secondnum && firstnum <= thirdnum && firstnum <= fourthnum && firstnum <= fifthnum){ document.writeln(firstnum+" is the lowest number"); } if(secondnum > firstnum && secondnum > thirdnum && secondnum > fourthnum && secondnum > fifthnum) document.writeln(secondnum+" is the highest number"); else if(secondnum <= firstnum && secondnum <= thirdnum && secondnum <= fourthnum && secondnum <= fifthnum){ document.writeln(secondnum+" is the lowest number"); } if(thirdnum >= firstnum && thirdnum >= secondnum && thirdnum >= fourthnum && thirdnum >= fifthnum) document.writeln(thirdnum+" is the highest number"); else if(thirdnum <= firstnum && thirdnum <= secondnum && thirdnum <= fourthnum && thirdnum <= fifthnum){ document.writeln(thirdnum+" is the lowest number"); } if(fourthnum >= firstnum && fourthnum >= secondnum && fourthnum >= thirdnum && fourthnum >= fifthnum) document.writeln(fourthnum+" is the highest number"); else if(fourthnum <= firstnum && fourthnum <= secondnum && fourthnum <= thirdnum && fourthnum <= fifthnum){ document.writeln(fourthnum+" is the lowest number"); } if(fifthnum >= firstnum && fifthnum >= secondnum && fifthnum >= thirdnum && fifthnum >= fourthnum) document.writeln(fifthnum+" is the highest number"); else if(fifthnum <= firstnum && fifthnum <= secondnum && fifthnum <= thirdnum && fifthnum <= fourthnum){ document.writeln(fifthnum+" is the lowest number"); } </script> i know the coding is all messed up and stupid.. but bear with me here pleeeeeaaaassee
ouch, I have a headache, I haven't read that code properly, but I suggest that u utilize arrays to tidy things up and give the code more of a direction, do that and you'll probably be able to tell where the mistake is.....
well, based on the existing code, if the highest or lowest values occur more than once, it will be printed multiple times because your code doesn't stop looking for the highest or lowest values even after it has been found. the 5 iterations get called no matter what happens. the only way out is to create 2 flags - 1 for highest and 1 for lowest. set both to false at the beginning. add each flag as a condition in each if statement - i.e. the if statement will only evaluate to true if the respective flag is false. then within each if statement code, set the flag to true once a highest or a lowest value has been found.
use something like this: var firstinteger, secondinteger, firstnum, secondnum, ... highflag, lowflag; firstinteger=window.prompt(" Enter first number:", "0"); firstnum=parseInt(firstinteger); secondinteger=window.prompt(" Enter second number:", "0"); secondnum=parseInt(secondinteger); ... highflag=0; lowflag=0; if(highflag==0 && firstnum > secondnum && ...) { document.writeln(firstnum+" is the highest number"); highflag=1; } else if(lowflag==0 && firstnum <= secondnum && ...) { document.writeln(firstnum+" is the lowest number"); lowflag=1; } if(highflag==0 && secondnum > firstnum && ...) { document.writeln(secondnum+" is the highest number"); highflag=1; } ... Code (markup):