8 Replies - 1793 Views - Last Post: 10 December 2010 - 06:33 PM

#1 Claud745  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 29-October 09

Print out numbers array

Posted 10 December 2010 - 03:57 PM

I have an assignment that just asks you to input some numbers and then if the number isn't between 100-999 just to enter the final number and whatever the final number is to print out everything that is less then that number.

For example. I enter 120, 128, and 1.. It will then prompt me to enter the final number which I put 124. Now I have it set as 124 so everything less than 124 it will print. So I want the array to print 124, 120 and 1. Now I just need that in code. I can't seem to figure out what to do and can't find any examples. Any help with examples or anything, I could use. Thank you!

Code so far:

<html>
<title>Homework 5</title>
<body>
<script>
var num = new Array();
var x = 0;
var lastnum;

for (x = 0; x < 3; x++)
{
   num[x] = window.prompt("Please enter a number between 100-999:");
   if((num[x] < 100) || (num[x] > 999))
   {
   lastnum = window.prompt("Enter your last number:");
   for (x = 0; x < 1; x++)
    {
    num.sort();
    window.alert(num);
    }
   break; 
   }
}

window.alert("DONE");


</script>
</body>



Is This A Good Question/Topic? 0
  • +

Replies To: Print out numbers array

#2 Claud745  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 29-October 09

Re: Print out numbers array

Posted 10 December 2010 - 04:45 PM

I have kind-of figured it out but I need some help

This is my code:
<html>
<title>Homework 5</title>
<body>
<script>
var num = new Array();
var x = 0;
var lastnum;

function remove(arrayName)
{
arrayName.pop();
}

for (x = 0; x < 99999; x++)
{
   num[x] = window.prompt("Please enter a number between 100-999:");
   if((num[x] < 100) || (num[x] > 999))
   {
   remove(num);
   lastnum = window.prompt("Enter your last number:");
   for (x = 0; x < 1; x++)
    {
    num.unshift(lastnum);
    num.sort();
    num.slice(0,lastnum)
    window.alert(num);
    }
   break; 
   }
}

window.alert("DONE");


</script>
</body>






This section:
 {
    num.unshift(lastnum);
    num.sort();
    num.slice(0,lastnum)
    window.alert(num);
    }


Is supposed to add the lastnum to the array
sort it
and then slice it from 0 to lastnum
So say I put in 120,121,555,1,123(lastnum)
It will output 120,121,123(lastnum).. but instead it prints out 120,121,123,555.
So why does it not cut the array?

This post has been edited by Claud745: 10 December 2010 - 04:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: Print out numbers array

Posted 10 December 2010 - 04:52 PM

It is because the value of 'lastnum' is not the size of the array being checked. If you enter "123" as the lastnum, then it tries to look into an array with at least 123 elements.
Was This Post Helpful? 1
  • +
  • -

#4 Claud745  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 29-October 09

Re: Print out numbers array

Posted 10 December 2010 - 04:56 PM

I understand now.. So I guess I'm back to how to print out a part of an array.
Was This Post Helpful? 0
  • +
  • -

#5 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: Print out numbers array

Posted 10 December 2010 - 05:06 PM

Maybe try:
  lastnum = window.prompt("Enter your last number:");
  num.push(lastnum);
  num.sort();
  var str = '';
  for (x=0; x<num.length; x++) {
    if (num[x] <= lastnum) { str += num[x]+','; }
                      else { break; }
  }
  window.alert(num);


Untested, but should be close...
:bananaman:
Was This Post Helpful? 1
  • +
  • -

#6 Claud745  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 29-October 09

Re: Print out numbers array

Posted 10 December 2010 - 05:12 PM

It does the same thing mine does.. prints out 120,121,123,555.
Was This Post Helpful? 0
  • +
  • -

#7 Claud745  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 29-October 09

Re: Print out numbers array

Posted 10 December 2010 - 05:19 PM

OKAY! We got it. Thank you JMRKER, all I had to do was change yours to print the str not the num.
Was This Post Helpful? 0
  • +
  • -

#8 Claud745  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 29-October 09

Re: Print out numbers array

Posted 10 December 2010 - 05:25 PM

Final code! :)


<html>
<title>Homework 5</title>
<body>
<script>
var num = new Array();
var x = 0;
var lastnum;

for (x = 0; x < 99999; x++)
{
   num[x] = window.prompt("Please enter a number between 100-999:");
   if((num[x] < 100) || (num[x] > 999))
   {
   num.pop();
   lastnum = window.prompt("Enter your last number:");
   for (x = 0; x < 1; x++)
    {
    num.push(lastnum);
    num.sort();
    var str = '';
    for (x=0; x<num.length; x++) 
    {
     if (num[x] <= lastnum) 
       { 
       str += num[x]+','; 
       }
    }
    window.alert(str);
    }
   break; 
   }
}

window.alert("DONE");


</script>
</body>



Was This Post Helpful? 0
  • +
  • -

#9 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 826
  • Joined: 25-October 08

Re: Print out numbers array

Posted 10 December 2010 - 06:33 PM

View PostClaud745, on 10 December 2010 - 04:19 PM, said:

OKAY! We got it. Thank you JMRKER, all I had to do was change yours to print the str not the num.

You're most welcome. :)

Like I said ... "untested" and your fix was what I had intended, so good eye! (I had originally copied and pasted from your post).
It's what you get when typing too fast with fat fingers!

This post has been edited by JMRKER: 10 December 2010 - 06:34 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1