Web Analytics Made Easy -
StatCounter Need help writing script to output 3 numbers smallest to largest - CodingForum

Announcement

Collapse
No announcement yet.

Need help writing script to output 3 numbers smallest to largest

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help writing script to output 3 numbers smallest to largest

    my teacher wants me to write a script that takes in 3 numbers and outputs them from smallest to largest...im pretty new at this and need some help..

    I started it and figure its "something" like this...any help? I dont know if im even on the right track?


    <script language="javascript">
    var a=parseInt(window.prompt("Please enter a number",""));
    var b=parseInt(window.prompt("Please enter a number",""));
    var c=parseInt(window.prompt("Please enter a number",""));

    if(var a<=b)

    {

    document.write(a,b,c)

    }
    else

    document.write(b,a,c);

    </script>

    Thanks for any help!
    -Ryan

  • #2
    Since this is obviously an assignment, I'm not gonna give you the exact code. Just a hint.

    Put the 3 input strings in an array. The array object has the sort() method:

    arrayVariable.sort();
    Glenn
    vBulletin Mods That Rock!

    Comment


    • #3
      Hey thanks for the reply...im not to famliar with js yet...i played with that code some, but no luck...this completes w/ no errors, but doesnt print it out. Any more tips? thx!!

      <script language="javascript">
      var a=parseInt(window.prompt("Please enter a number",""));
      var b=parseInt(window.prompt("Please enter a number",""));
      var c=parseInt(window.prompt("Please enter a number",""));

      arrayVariable.sort(a,b,c);
      document.write(sort);
      </script>

      Comment


      • #4
        You should declare an array variable and populate it with the result of the prompts.

        var arrNum = new Array();

        Here's a column on populating javascript arrays and here's a reference on Array.sort()
        Glenn
        vBulletin Mods That Rock!

        Comment

        Working...
        X