Jump to content

parseInt and Arrays [Novice]

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Dnectrum

Dnectrum

    Newbie

  • Members
  • Pip
  • 9 posts
I am new to Javascript and this forum alike, so forgive me if I break any rules. I have searched the forum a bit, but to no avail.

My problem is that I currently am having troubling using parseInt and an array. The goal is to allow a user to enter a series of numbers and than pass those numbers to an array which would be able to run arithmetic off of them.

Am I using this properly?

<html>

 <head>

  <title> lab7 </title>

 </head>


 <body>

 <script>


 var nums = new Array();

 var integer;

 var sum;

 var average;

 var counter;

 var index = 1;

 var integer_count;

 var integer_total = 0;


 while (index <= 10)

 {

	nums[index] = prompt("Enter a number");

	nums[index] = parseInt(nums[index]);

	integer_total = integer_total + nums[index];

	index++;

 }


 average = integer_total / 10;

  

 index = 1;

 while (index <= 10)

 {

	if (nums[index] < average)

	{

		integer_count++;

	}

	index++

 }

 alert(average + integer_count);



 </script>

  

 </body>

</html>


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
NaN stands for Not a Number. Check user input with isNaN to make sure it is a number before using it.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
Dnectrum

Dnectrum

    Newbie

  • Members
  • Pip
  • 9 posts
Okay thanks, but I am new so I really wouldn't know what to do with that just yet. Even if I check user input against isNaN, how could i go about changing the code to assure that workable numbers are being set in the array?

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
An idea of the top of my head, you can use the "continue;" statement if isNaN fails at the beginning of the while loop.

i.e. an array containing "14, 10, f, 40, 12"
(index) (integer)
1, 14
2, 10
(continue), f
3, 40
4, 12
...

This will leave you with nine numbers however, you will have to decide if you want to cause a failure, or to run the arithmetic with only nine (or less if more garbage) in the array.

Addendum: You may wish to start arrays at zero, as in your line of thinking, the first element starts at zero and takes place between zero and one. Having to off set your indexes from 1..10 can cause a lot of issues as the zeroth number can be garbage automatically if unassigned (I am not sure if uninitialisation occurs in Javascript however)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Don't forget to do index-- when it's not an int, or you'll end up with less-than-10 numbers.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users