Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    New to Javascript; Problem with document.write coding

    I'm taking a beginning class at my school for Javascript. My teacher refuses to answer e-mails so I cannot get in touch with him to fix my problem. My assignment was to prompt a user to enter 5 integers, and then have a browser display the largest of the 5 integers. I was then asked to prompt the user to enter 1 more integer and if it was equal to one of the 5 other integers, display a statement respectively.

    What happens with my script is once the user puts in the integers, the browser displays every integer, and also both statements (this integer is NOT equal/equal to the other integers.) I'm sure its a very simple problem, but I am a complete noob with coding, so I don't know what to do to just have the browser display the LARGEST integer, not every integer, as well as display ONE statement, not both.

    Here is the code:
    </html>
    </body>

    <script language="JavaScript" type="text/javascript">

    var firstnumber = Number(prompt("Enter the first number"));
    var secondnumber = Number(prompt("Enter the second number"));
    var thirdnumber = Number(prompt("Enter the third number"));
    var fourthnumber = Number(prompt("Enter the fourth number"));
    var fifthnumber = Number(prompt("Enter the fifth number"));

    if ( (firstnumber > secondnumber) && (firstnumber > thirdnumber) && (firstnumber > fourthnumber) && (firstnumber > fifthnumber) );
    {
    document.write(firstnumber);
    }

    if ( (secondnumber > firstnumber) && (secondnumber > thirdnumber) && (secondnumber > fourthnumber) && (secondnumber > fifthnumber) );
    {
    document.write(secondnumber);
    }

    if ( (thirdnumber > firstnumber) && (thirdnumber > secondnumber) && (thirdnumber > fourthnumber) && (thirdnumber > fifthnumber) );
    {
    document.write(thirdnumber);
    }

    if ( (fourthnumber > firstnumber) && (fourthnumber > secondnumber) && (fourthnumber > thirdnumber) && (fourthnumber > fifthnumber) );
    {
    document.write(fourthnumber);
    }

    if ( (fifthnumber > firstnumber) && (fifthnumber > secondnumber) && (fifthnumber > thirdnumber) && (fifthnumber > fourthnumber) );
    {
    document.write(fifthnumber);
    }

    var lastnumber = Number(prompt("Enter one last number"));
    if ( (lastnumber == firstnumber) || (lastnumber == secondnumber) || (lastnumber == thirdnumber) || (lastnumber == fourthnumber) || (lastnumber == fifthnumber) );
    {
    document.write("The last number is equal to one of the other integers.");
    }

    if ( !(lastnumber == firstnumber) || (lastnumber == secondnumber) || (lastnumber == thirdnumber) || (lastnumber == fourthnumber) || (lastnumber == fifthnumber) );
    {
    document.write("The last number is NOT equal to one of the other integers.");
    }

    </script>

    </body>
    </html>

    Here is a link to the site:
    file:///C:/Documents%20and%20Settings/Fred/Desktop/JS_HW2.htm

    I apologize if I'm breaking any rules on the board. The class is in about 2 hours, so I'm just hoping I can get a quick answer from anyway that can even read my code.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,240
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Presumably you have solved your problem now.

    Code:
    if ((lastnumber != firstnumber) && (lastnumber != secondnumber) &&(lastnumber != thirdnumber) && (lastnumber != fourthnumber) &&  (lastnumber!= fifthnumber) );
    {
    document.write("The last number is NOT equal to one of the other integers.");
    }
    I expect you realise that this is a terribly cumbersome way of comparing numbers.

    Sign on a New York convalescent home: For the sick and tired of the Episcopal Church.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •