need help guys

Discussion in 'JavaScript' started by rafiseo, Dec 5, 2006.

  1. #1
    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
     
    rafiseo, Dec 5, 2006 IP
  2. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #2
    same issue over and over again... ;)

    without more information, no help will be forthcoming... :)

    post the code boy!
     
    daboss, Dec 5, 2006 IP
  3. rafiseo

    rafiseo Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <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
     
    rafiseo, Dec 5, 2006 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    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.....
     
    krakjoe, Dec 5, 2006 IP
  5. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    daboss, Dec 5, 2006 IP
  6. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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):
     
    daboss, Dec 5, 2006 IP
  7. rafiseo

    rafiseo Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    oh jesus christ thank you so much! i get it now... i owe you one:D
     
    rafiseo, Dec 5, 2006 IP