Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Luqman Shah
Luqman Shah
3,016 Points

Please review my code - Teacher tests student - Conditionals

var homework = false
var assignments = prompt('How many assignments do you have today?');
var hint = alert("It's an even number, less than 10");
if (parseInt(assignments) === 4) {
  homework = true
} else if (assignments !== 4) {
  var tryAgain = prompt('Try again');
  if (parseInt(tryAgain) === 4) {
    homework = true
  }
}
if (homework) {
  alert("Good job, you've been paying attention");
} else {
  alert('I am dissapointed');
}

1 Answer

Steven Parker
Steven Parker
231,261 Points

Here's a few observations/suggestions:

  • "alert" doesn't return a value, so don't use it in an assignment
  • the variable "hint" is never used anyway
  • you might want to move the alert message to after you test "assignments"
  • "assignments" is a string, so it will never be equal to 4
  • you don't need to retest it anyway, you can use a plain "else"
  • while not a language requirement, it is considered "best practice" to end each statement with a semicolon
Luqman Shah
Luqman Shah
3,016 Points

assignments is a string? I thought it would be considered a variable, as I've set a variable name to assignments, and I haven't placed it in quotation marks. So the number value the user puts into the prompt will never be equal to 4, even if they typed in the number 4? I tested it out on the browser through atom, seemed to have ran the program as expected. Thank you so much for your suggestions!

Steven Parker
Steven Parker
231,261 Points

Sorry if I wasn't clear, "assignments" is a variable that contains a string that comes in from "prompt". In the first test, it is converted to a number using *parseInt" but in the second test (the one you don't need) it is compared directly to the number 4. Since a string is a different type from a number, and since you use the type-sensitive test ("==="), then even if "4" was entered it will still not equal the number 4.

So "else if (assignments !== 4)" does exactly the same as plain "else".