Having Trouble with an If Statement in a For loop: JavaScript

Hey everyone,

I am working on a project for a college course, and I’ve run into an issue I can’t figure out with the JavaScript code. The button that I’ve made doesn’t run this function when it’s called. If someone could take a look at this code and help me understand why it won’t work, that would be awesome!

Here’s the code:

// Function that will create the array of scores. It will then use the array to arrange and average our grades
function newArray() {
  // Making the array to store all of the test scores
  var scores = [60, 70, 99, 82, 77, 100, 75, 88, 100, 75]	  

// The second half of the function, this part will use our array to tell the user how many A's, B's, C's, D's and F's they have

  // Making variables to hold the number of each letter grade
  var gradeA = 0
  var gradeB = 0
  var gradeC = 0
  var gradeD = 0
  var gradeF = 0
  // Making the for loop that categorizes the scores and determines how many we have of each
  for (var i = 0; i <= 9; i = i + 1){
    if (scores[i] >= 90 && scores[i] <= 100 {
	  gradeA = gradeA + 1 ;
	if (scores[i] >= 80 && scores[i] < 90 {
	  gradeB = gradeB + 1 ;
	if (scores[i] >= 70 && scores[i] < 80 {
	  gradeC = gradeC + 1 ;
	if (scores[i] >= 60 && scores[i] < 70 {
	  gradeD = gradeD + 1 ;
	if (scores[i] < 60 {
	  gradeF = gradeF + 1 ;
  } // End of for loop
  // Writing to the screen the number of each letter grade
  document.write ("There are " + gradeA "A grades.") ;
    // Haven't written this all out yet: will do when I get it to work

} // End of function

most of your if statements dont have closing perentheses

1 Like
  • You need to be using else if after your first if statement.

  • You’re missing a lot of closing parens )

  • Fix those and your code will run.

  • You may want to think about using .length in your loop. This way you don’t have to update your code if more grades are entered (or removed).

  • As an aside, you should run your code through a JS validator. It will help you find your typo’s if your editor isn’t picking them up for you.

1 Like

Thanks guys for the feedback, I can’t believe I forgot the closing parentheses around the if’s! I also changed the if statements after the first one to be else if. It still won’t run, however, even if I just call it right in the code. :frowning:

You were missing a plus sign in your write statement. That’s why I suggested you run your code through a validator.

1 Like

Thank you, that did the trick! I tried a couple after a quick google search, but a lot of them were spitting out errors about syntactically sound stuff, or recommending that I put a comma in a strange place. Is there a specific one that you would recommend? Again, thank you so much for your help!

None specific…though I’m sure some reading this may chime in with their personal favorite.

Happy coding!

1 Like

You might want to use array.length (in this case scores.length) for your loops to be more safe in the case the array adds or removes values. Also, think about using i++ as a short-form for i = i +1.