3 Replies - 711 Views - Last Post: 17 November 2010 - 05:22 PM

#1 Claud745  Icon User is offline

  • D.I.C Head

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

Array help

Posted 17 November 2010 - 02:28 PM

I know this might seem easy or stupid to someone but I am just not getting the array to work. I want it to add more than one student and then take their score and average it out. So far I can't even get my array to work so I can't even work on my average. Anyways if anyone can help or show me some examples, would be great. I have looked at examples and just can't seem to make it work.

<html>
<title>Assignment 4.3</title>
<head>

<script>
function gradereport()
{
   if (num<60)
   {
      grade="F";
   }
   else if (num<70)
   {
      grade="D";
   }
   else if (num<80)
   {
      grade="C";
   }
   else if (num<90)
   {
      grade="B";
   }
   else
   {
      grade="A";
   }
   return grade;
}
</script>

</head>
<body>
<script>
size = window.prompt("How many students do you want to enter? ");
var num = new Array(size);
var i = 0;
for (i = 0; i < size; i++);
{
  
   num[i] = window.prompt("Please enter a number:");
   if ((num >= 0) && (num <= 100))
   {
   gradereport();
   window.alert("Your letter grade is: " +grade);
   }
}


alert("THANK YOU!")

</script>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Array help

#2 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: Array help

Posted 17 November 2010 - 02:51 PM

There are a couple of error that I can spot. Let me go through these one by one.

var num = new Array(size);

In JS you don't specify the array's length. In fact, the arguments to the Array function are the elements to populate it with. So you are basically populating the array with one integer: the value of size.

for (i = 0; i < size; i++);
{
   num[i] = window.prompt("Please enter a number:");
   if ((num >= 0) && (num <= 100))
   {
   gradereport();
   window.alert("Your letter grade is: " +grade);
   }
}

In this code, you are setting num[i] to the number, but then trying to access it back with just num. You need to use num[i] to access it back.

gradereport();

It would be best to pass the number into the gradereport function and have the function return the grade. That way you're not messing with any global variables

window.alert("Your letter grade is: " + grade);

The grade variable isn't defined in this scope. It's defined in the gradereport function, but outside of the function it loses scope and it's inaccessible. gradereport returns the grade as a value. You need to set the grade variable equal to the return value of gradereport.

Here's the code with everything updated:
<html>
<title>Assignment 4.3</title>
<head>
<script>
function gradereport( number )
{
   if (number<60)
   {
      grade="F";
   }
   else if (number<70)
   {
      grade="D";
   }
   else if (number<80)
   {
      grade="C";
   }
   else if (number<90)
   {
      grade="B";
   }
   else
   {
      grade="A";
   }
   return grade;
}
</script>

</head>
<body>
<script>
var grade;
var size = window.prompt("How many students do you want to enter? ");
var num = new Array();
var i = 0;
for (i = 0; i < size; i++);
{
   num[i] = window.prompt("Please enter a number:");
   if ((num[i] >= 0) && (num[i] <= 100))
   {
   grade = gradereport( num[i] );
   window.alert("Your letter grade is: " + grade);
   }
}
alert("Thank you!");
</script>
</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#3 Claud745  Icon User is offline

  • D.I.C Head

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

Re: Array help

Posted 17 November 2010 - 02:59 PM

Awesome. The only problem I have for it is that it won't input more than one person. For example when I ask how many students do you want to enter no matter what I put in it will only do it for one student.

Either way, sorry for all the questions. I'm new to Javascript.
Was This Post Helpful? 0
  • +
  • -

#4 Claud745  Icon User is offline

  • D.I.C Head

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

Re: Array help

Posted 17 November 2010 - 05:22 PM

Sorry, I got it. I don't know why I didn't see the ;.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1