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

Aaron Coursolle
Aaron Coursolle
18,014 Points

Even at its simplest level, I only get black:

I wanted to do an experiment before applying it to the crown code challenge, but even at the simplest level, I can't get it to work. So I added a prompt so that anyone can try out different score levels. Why am I only getting black?

var score = prompt("Type in a number between 0 to 5, please");

if (score === 1) {
  var color = "orange";
}
else if (score === 2) {
  var color = "blue";
}
else if (score === 3) {
  var color = "green";
}
else if (score === 4) {
  var color = "purple";
}
else if (score === 5) {
  var color = "yellow";
}
else {
  var color = "black";
}

document.write(color);

1 Answer

You almost have it. You need to convert the users input from a string to an integer using parseInt. View below.

var score = parseInt(prompt("Type in a number between 0 to 5, please"));

This will convert a string into a number, assuming the users input is a number.

Aaron Coursolle
Aaron Coursolle
18,014 Points

Thank you. This will make it easier to test my code when it gets more complex, as well.