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 JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Jennifer Crawford
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Crawford
Full Stack JavaScript Techdegree Student 1,231 Points

Numbers keep acting like strings

I am trying to add the number of correct phrases together but every time I try to 1 + 1, I get 11 not 2. correct = 1; correct += 1; alert("You have answered " + correct + " phrases correct.");

Michael Caveney
Michael Caveney
50,144 Points

Can't help unless we see your code verbatim.....

Jennifer Crawford
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Crawford
Full Stack JavaScript Techdegree Student 1,231 Points

Thank you Corey, I had not realized I had to put parseInt() around each string tat I wanted to behave as a number. Now that I have them acting like a number I can not get them to come up with alert() or document.write()

if (correct === parseInt(5)) {
  document.write("<h1>You got all 5 phrases right, you recieve a gold crown!</h1>");

} else if (correct === parseInt(4) || correct === parseInt(3)) {
  document.write("<h1>You got ") + correct + (" phrases right, you recieve a silver crown!</h1>");

} else if (correct === parseInt(2) || correct === parseInt(1)){
  document.write("<h1>You got ") + correct + (" phrases right, you recieve a bronze crown!</h1>");

} else {
  document.write("<h1>Sorry you did not get any phrases right, you do not receive any crown</h1>");
}

Please paste all of the code in your .js file, and I'll take a look.

3 Answers

If you are using user-generated info in response to a prompt (in a pop-up window), it will be interpreted as a string, even if they entered a number. Any numbers users submit need to be converted to a real number, and you can do this by using parseInt( variableNameHere ). Here's an example of when it will not work:

var exampleVariable = prompt( 'What is a number between 1 and 3?' );
var message = exampleVariable + exampleVariable;
document.write(' Double your number is ' + message + ' .' );

It will not work because if the user enters 2, it will be seen as '2', which is a string, and '2' + '2' = '22'. It's adding the characters, not doing math. Instead try parseInt(), like this:

var exampleVariable = prompt( 'What is a number between 1 and 3?' );
var message = parseInt(exampleVariable) + parseInt(exampleVariable);
document.write(' Your number, doubled, is ' + message + ' .' );

Hi Jennifer,

You're doing well, keep going. Before you read on, I would like to suggest that you watch the videos more carefully, because all of the mistakes were explained very clearly by the instructor. The goal is not to move forward in the videos, but to learn.

Below are a few specific suggestion regarding the code and then the fixed code at the end for you to compare with your code.

  1. You unnecesarily wrote all of the possible entries a visitor might type. For example, you wrote: if (down === 'down' || down === 'Down' || down === 'DOWN'). What if your user wrote doWn, would it be wrong? Also, it is a lot of work to think of all of the possible entries someone might enter. So, to make it simple, you can use this toUpperCase(). Check the code below to see an example. This was explained in the course, I suggest you re-watch it: link
  2. You only need to use parseInt() if the number was entered in a box by a user, or if you want to turn a string into a number. In your case, you don't need to use parseInt. This also was explained clearly in your course video: link
  3. You were having trouble printing the crowns/results to the document because you had parenthesis in the wrong places. Please compare your "Crown" section with what I have submitted below.

Below is the code for comparison. I hope this helps. Keep on going :)

var correct = 0;

var down = prompt("What goes up must come ____.");

if (down.toUpperCase() === 'DOWN'){
  alert("YES! What goes up must come DOWN.");
  correct += 1;
  // my instructor was able to use correct += 1; I a not
} else {
  alert("Sorry, the answer was Down. What goes up must come down.");
}

var rock = prompt("Stuck between a ____ and a hard place.");

if (rock.toUpperCase() === 'ROCK'){ 
  alert("YES! Stuck between a ROCK and a hard place.");
  correct += 1;

} else {
 alert("Sorry, the answer was Rock. Stuck between a rock and a hard place."); 
}

var nose = prompt("Cut your ____ off to spite your face.");

if (nose.toUpperCase() === 'NOSE') {
  alert("YES! Cut your NOSE off to spite your face");
  correct += 1;

} else {
  alert("Sorry, the answer was Nose. Cut your nose off to spite your face.");
}

var mine = prompt("Your guess is as good as ____.");

if (mine.toUpperCase() === 'MINE') {
  alert("YES! Your guess is as good as MINE.");
  correct += 1;

} else {
  alert("Sorry, the answer was Mine. Your guess is as good as mine.");
}

var thumb = prompt("To _____ one's nose.");

if (thumb.toUpperCase() === 'THUMB') {
  alert("YES! To THUMB one's nose.");
  correct += 1;

} else {
  alert("Sorry, the answer was Thumb. To thumb one's nose.");
}

/* 5 correct answers Gold Crown
   3-4 correct Silver Crown
   1-2 correct Bronze Crown
*/

if (correct === 5) {
  document.write("<h1>You got all 5 phrases right, you recieve a gold crown!</h1>");

} else if (correct === 4 || correct === 3) {
  document.write("<h1>You got " + correct + " phrases right, you recieve a silver crown!</h1>");

} else if (correct === 2 || correct === 1){
  document.write("<h1>You got " + correct + " phrases right, you recieve a bronze crown!</h1>");

} else {
  document.write("<h1>Sorry you did not get any phrases right, you do not receive any crown</h1>");
}
Jennifer Crawford
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Crawford
Full Stack JavaScript Techdegree Student 1,231 Points

Thank you I took your advice and used .toUpperCase(). Guess I just wanted to use one of the new operations I had just learned, At first I had not used the parseInt() but when I was trying to get the code to add for some reason they were being treated like strings not numbers. I typed 1 + 1 + 1 = 3 but it was acting as if I had typed '1' + '1' + '1' = '111' which I did not. I added the parseInt() and only then did I get them to add up. I deleted the parseInt() and now it works fine. The parenthesis got me, that was the problem I was having that I should have caught with proofreading.

Now is there a reason that it did not work before? After I watched the videos again, I went back and changed the if clauses to down.toUpperCase() === 'DOWN'. Deleted the parseInt() and removed the extra parenthesis. Why did the numbers not work before?

Why did the numbers not work before?

The only reason I can think of it not working before is if you put the numbers in single or double quotes (e.g. '1' + '1' + '1' = 111, and "1" + "1" + "1" = 111, but 1 + 1 + 1 = 3). Anything within quotes is a string, but a number outside of quotes is treated like a number. Perhaps you had your numbers in quotes before. If you didn't, then I'm at a loss, and don't know why they would have acted like a string.

Jennifer Crawford
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Crawford
Full Stack JavaScript Techdegree Student 1,231 Points
/* This quiz.js code file was to ask five questions
    Keep track of number of correct answers
    Then award a crown for the amount of correct answers
*/

var correct = parseInt(0);

var down = prompt("What goes up must come ____.");

if (down === 'down' || down === 'Down' || down === 'DOWN'){
  alert("YES! What goes up must come DOWN.");
  correct += parseInt(1);
  // my instructor was able to use correct += 1; I a not

} else {
  alert("Sorry, the answer was Down. What goes up must come down.");
}


var rock = prompt("Stuck between a ____ and a hard place.");

if (rock === 'rock' || rock === 'Rock' || rock === 'ROCK'){ 
  alert("YES! Stuck between a ROCK and a hard place.");
  correct += parseInt(1);

} else {
 alert("Sorry, the answer was Rock. Stuck between a rock and a hard place."); 
}


var nose = prompt("Cut your ____ off to spite your face.");

if (nose === 'nose' || nose === 'Nose' || nose === 'NOSE'){
  alert("YES! Cut your NOSE off to spite your face");
  correct += 1;

} else {
  alert("Sorry, the answer was Nose. Cut your nose off to spite your face.");
}


var mine = prompt("Your guess is as good as ____.");

if (mine === 'mine' || mine === 'Mine' || mine === 'MINE') {
  alert("YES! Your guess is as good as MINE.");
  correct += parseInt(1);

} else {
  alert("Sorry, the answer was Mine. Your guess is as good as mine.");
}


var thumb = prompt("To _____ one's nose.");

if (thumb === 'thumb' || thumb === 'Thumb' || thumb === 'THUMB') {
  alert("YES! To THUMB one's nose.");
  correct += parseInt(1);

} else {
  alert("Sorry, the answer was Thumb. To thumb one's nose.");
}

/* 5 correct answers Gold Crown
   3-4 correct Silver Crown
   1-2 correct Bronze Crown
*/

if (correct === parseInt(5)) {
  document.write("<h1>You got all 5 phrases right, you recieve a gold crown!</h1>");

} else if (correct === parseInt(4) || correct === parseInt(3)) {
  document.write("<h1>You got ") + correct + (" phrases right, you recieve a silver crown!</h1>");

} else if (correct === parseInt(2) || correct === parseInt(1)){
  document.write("<h1>You got ") + correct + (" phrases right, you recieve a bronze crown!</h1>");

} else {
  document.write("<h1>Sorry you did not get any phrases right, you do not receive any crown</h1>");
}
// Once I got correct to be a number and not a character, now it stops after you got