Javascript Functions & Array Help

Slottr

Estimable
Jul 25, 2015
1
0
4,510
0
I have to write this program for a school project and I've been stumped quite well.

The assignment is:

Create a webpage that asks for a student’s name and four quiz scores. All pieces of data should be recorded in an array. Student[0] should hold the student’s name. Student[1] should hold test score 1, Student[2] should hold student test score 2 etc. Student[5] should hold the average for that student.

Use prompts to collect the data.

Use a function to calculate the average for the student. Output all the information using an alert.
I have limited knowledge of Javascript and I cant really go further with things such as loops.


My code so far: https://js.do/code/ohgoodgodwhatamidoing

Anybody have an idea on how to do this, using the current code I have?
 

randomizer

Distinguished
Moderator
Jul 24, 2006
880
0
19,260
62
There are a few problems with this code. There are also a few bits that you may have intended to work differently than they actually do, or it could just be a misunderstanding of the required syntax.

First, the simple problems:

Line 22: You are referring to the score array, but it doesn't exist inside this function so you get an error on this line. You accept the four values as parameters (score1, score2 etc.) so you can just add those instead of trying to refer to the array. Well, not exactly, but we'll get to that later.

Line 24: You are returning from this function before you display the result. Line 25 is never executed. Just remove this.

Now the not-so-obvious things that will cause you issues or don't do what you expect:

Lines 10-14: You prompt for four variables but don't do anything with them. Since you ended each line with a comma I assume you wanted to initialise the array with these variables. However, since you didn't add parentheses around them they're not being passed to the Array constructor function. It is instead receiving no arguments and being initialised as an empty array. The commas mean something totally different in this context. For ease of understanding I recommend assigning to each of the array elements individually so that you can clearly see what it's doing. eg.

JavaScript:
score[0] = prompt("Enter Test Score 1", "0")
Line 22: Even if you apply the fix I mentioned earlier, this will not calculate the average correctly. If you score 1 for all four tests you'll get an average of 277.75 rather than 1. The prompt() function returns the user's input as a string (text) rather than a number. In JS, the + operator also serves as a way to concatenate (join) strings together. Therefore your code (assuming it has been corrected as above) actually does this:

JavaScript:
average = ("1" + "1" + "1" + "1") / 4
Which evaluates to:

JavaScript:
average = ("1111") / 4
Now since the / operator only works on numbers, the JS engine must try to convert your string into a number before performing the last operation. Therefore your final expression is:

JavaScript:
average = (1111) / 4
The simplest solution is to convert each of the strings to numbers before doing any calculations.
 

randomizer

Distinguished
Moderator
Jul 24, 2006
880
0
19,260
62
There are a few problems with this code. There are also a few bits that you may have intended to work differently than they actually do, or it could just be a misunderstanding of the required syntax.

First, the simple problems:

Line 22: You are referring to the score array, but it doesn't exist inside this function so you get an error on this line. You accept the four values as parameters (score1, score2 etc.) so you can just add those instead of trying to refer to the array. Well, not exactly, but we'll get to that later.

Line 24: You are returning from this function before you display the result. Line 25 is never executed. Just remove this.

Now the not-so-obvious things that will cause you issues or don't do what you expect:

Lines 10-14: You prompt for four variables but don't do anything with them. Since you ended each line with a comma I assume you wanted to initialise the array with these variables. However, since you didn't add parentheses around them they're not being passed to the Array constructor function. It is instead receiving no arguments and being initialised as an empty array. The commas mean something totally different in this context. For ease of understanding I recommend assigning to each of the array elements individually so that you can clearly see what it's doing. eg.

JavaScript:
score[0] = prompt("Enter Test Score 1", "0")
Line 22: Even if you apply the fix I mentioned earlier, this will not calculate the average correctly. If you score 1 for all four tests you'll get an average of 277.75 rather than 1. The prompt() function returns the user's input as a string (text) rather than a number. In JS, the + operator also serves as a way to concatenate (join) strings together. Therefore your code (assuming it has been corrected as above) actually does this:

JavaScript:
average = ("1" + "1" + "1" + "1") / 4
Which evaluates to:

JavaScript:
average = ("1111") / 4
Now since the / operator only works on numbers, the JS engine must try to convert your string into a number before performing the last operation. Therefore your final expression is:

JavaScript:
average = (1111) / 4
The simplest solution is to convert each of the strings to numbers before doing any calculations.
 
Thread starter Similar threads Forum Replies Date
T Apps General Discussion 5
A Apps General Discussion 2
anustarique Apps General Discussion 3
alanps Apps General Discussion 3
D Apps General Discussion 1
S Apps General Discussion 2
R Apps General Discussion 1
V Apps General Discussion 4
The_Freeman Apps General Discussion 11
E Apps General Discussion 3
viveknayyar007 Apps General Discussion 0
ivideo1011 Apps General Discussion 7
Orogi Apps General Discussion 6
R Apps General Discussion 1
S Apps General Discussion 1
S Apps General Discussion 11
C Apps General Discussion 9
jappe66 Apps General Discussion 4
R Apps General Discussion 1
G Apps General Discussion 1

ASK THE COMMUNITY