Warning: unexpected symbol 'if' when using else if

Inside of my learnr tutorial, I'm getting warnings whenever I use else if. See below (lines 6, 8, 10):

Here is the code for the exercise and its solution

{r KC2, exercise=TRUE, exercise.lines=15}
# YOUR WORK HERE
# Subtract 85 from 100 and assign the result to an object named `score`

# If score is 90 or above, assign 'A' to an object named `grade`

# Otherwise if the `score` is 80 or above, assign 'B' to an object named `grade`

# Otherwise if the `score` is 70 or above, assign 'C' to an object named `grade`

# Otherwise if the `score` is 60 or above, assign 'D' to an object named `grade`

# Otherwise if the `score` is below 60, assign 'Hidden' to an object named `grade`

# Print the value stored in `grade`
{r KC2-solution}
# Subtract 85 from 100 and assign the result to an object named `score`
score = 100 - 85

if (score >= 90){          # If score is 90 or above, assign "A" to an object named `grade`
  grade <- "A"
} else if (score >= 80){   # Otherwise if the `score` is 80 or above, assign "B" to an object named `grade`
  grade <- "B"
} else if (score >= 70){   # Otherwise if the `score` is 70 or above, assign "C" to an object named `grade`
  grade <- "C"
} else if (score >= 60){   # Otherwise if the `score` is 60 or above, assign "D" to an object named `grade`
  grade <- "D"
} else{                   # Otherwise if the `score` is below 60, assign "Hidden" to an object named `grade`
  grade <- Hidden
}

# Print the value stored in `grade`
print(grade)

Does anyone know why this is happening?

I'm not sure why this is happening, but I can reproduce it. Most likely the exercise editor requires some additional configuration for linting R code. Can you please file an issue on the learnr issue tracker? Thanks!

1 Like

Is it a red herring that this is unquoted?

It’s just user issues. I have a bad habit of using single quotes instead of double quotes for strings, and I’m still working on correcting that.

I dont quite understand your comment.... I was pointing out the lack of any quotes, be they a pair of singles or doubles.... that would imply that there is a variable called Hidden whose value should be assigned to grade.

Ahh I see. I thought you were referring to the switching between the quotations in the comments.
Thanks for the catch!

I added the issue, and folks can find it here.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.