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 Solution

Dave Harrison
seal-mask
.a{fill-rule:evenodd;}techdegree
Dave Harrison
Front End Web Development Techdegree Student 7,400 Points

Using Booleans in the Quiz

Hi,

I tried to use booleans in one of my 5 questions but couldn't get it to work without changing answerOne to a string and comparing that with the response to questionOne.

Do you need to convert the answer to questionOne from a string to a boolean to get this to work....or something else?

var correctGuess = 0; var answerOne = true; var questionOne = prompt( "True or false - Berlin is the capital city of Germany?" ); if ( questionOne ) { alert("That's right, well done"); correctGuess += 1; } else { alert( "Doh!" ); }

3 Answers

Adam Beer
Adam Beer
11,314 Points
var correctGuess = 0; 
var answerOne = undefined; 
var questionOne = prompt( "True or false - Berlin is the capital city of Germany?" ); 

  if (questionOne.toUpperCase() === 'TRUE') {
        answerOne = true;
    } else {
        answerOne = false;
    }

  if ( answerOne === true) { 
      alert("That's right, well done"); 
      correctGuess += 1; 
  } else { 
      alert( "Doh!" ); 
  }

or

var correctGuess = 0; 
var answerOne = undefined; 
var questionOne = prompt( "True or false - Berlin is the capital city of Germany?" ); 

  if (questionOne.toUpperCase() === 'TRUE') {
        answerOne = true;
          if ( answerOne === true) { 
            alert("That's right, well done"); 
            correctGuess += 1;
          }
    } else {
        answerOne = false;
          if (answerOne === false) {
            alert( "Doh!" ); 
          }
    }

or

var correctGuess = 0; 
var answerOne = undefined; 
var questionOne = prompt( "True or false - Berlin is the capital city of Germany?" ); 

  if (questionOne.toUpperCase() === 'TRUE') {
        answerOne = true;
          if ( answerOne === true) { 
            alert("That's right, well done"); 
            correctGuess += 1;
          }
    } 

  if(questionOne.toUpperCase() === 'FALSE') {
        answerOne = false;
          if (answerOne === false) {
            alert( "Doh!" ); 
          }
    }

or

var correctGuess = 0; 
var answerOne = undefined; 
var questionOne = prompt( "True or false - Berlin is the capital city of Germany?" ); 

  if (questionOne.toUpperCase() === 'TRUE') {
        answerOne = true;
    }
  if (questionOn.toUpperCase() === 'FALSE')
        answerOne = false;
    }

  if ( answerOne === true) { 
      alert("That's right, well done"); 
      correctGuess += 1; 
  } else { 
      alert( "Doh!" ); 
  }

And now does one of them work? These are my last ideas. I prefer the stackoverflow instead of mdn and w3school.

Dave Harrison
seal-mask
.a{fill-rule:evenodd;}techdegree
Dave Harrison
Front End Web Development Techdegree Student 7,400 Points

Thanks Adam. I admire your persistence. The 4th one has a curly brace missing and a typo in the 2nd if statement, but other than that they all work. Where did you pick up on the idea for the answerOne variable being undefined?

Adam Beer
Adam Beer
11,314 Points

Just more transparent.

var answerOne; 
var answerOne = undefined;

Both are the same. Or because you expect a value that is true or false. We do not know yet.

Adam Beer
Adam Beer
11,314 Points

Code

Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

      ```html
      <p>This is code!</p>
      ```
var correctGuess = 0; 
var answerOne = 'true'; 
var questionOne = prompt( "True or false - Berlin is the capital city of Germany?" ); 
  if ( questionOne === answerOne.toUpperCase()) { 
      alert("That's right, well done"); 
      correctGuess += 1; 
  } else { 
      alert( "Doh!" ); 
  }

or

var correctGuess = 0; 
var questionOne = prompt( "True or false - Berlin is the capital city of Germany?" ); 
  if ( questionOne === 'true'.toUpperCase()) { 
      alert("That's right, well done"); 
      correctGuess += 1; 
  } else { 
      alert( "Doh!" ); 
  }

I don't fully understand. That's what you thought?

Dave Harrison
seal-mask
.a{fill-rule:evenodd;}techdegree
Dave Harrison
Front End Web Development Techdegree Student 7,400 Points

Thanks for taking a look at this Adam. I'm not sure I understand your response.

I was wondering whether there was actually something I could change or add to the JavaScript snippet included in my original post?

Adam Beer
Adam Beer
11,314 Points
var correctGuess = 0; 
var answerOne = undefined; 
var questionOne = prompt( "True or false - Berlin is the capital city of Germany?" ); 

  if (questionOne.toUpperCase() === 'true') {
        answerOne = true;
    } else {
        answerOne = false;
    }

  if ( answerOne === true) { 
      alert("That's right, well done"); 
      correctGuess += 1; 
  } else { 
      alert( "Doh!" ); 
  }

or

var correctGuess = 0; 
var answerOne = undefined; 
var questionOne = prompt( "True or false - Berlin is the capital city of Germany?" ); 

  if (questionOne.toUpperCase() === 'true') {
        answerOne = true;
          if ( answerOne === true) { 
            alert("That's right, well done"); 
            correctGuess += 1;
          }
    } else {
        answerOne = false;
          if (answerOne === false) {
            alert( "Doh!" ); 
          }
    }

Did you mean that? So now you do not have to convert the boolean value. I'm sorry, I did not try to figure out how this works :)

Dave Harrison
seal-mask
.a{fill-rule:evenodd;}techdegree
Dave Harrison
Front End Web Development Techdegree Student 7,400 Points

Hi Adam. Thanks again for looking at this. I have tried all these different ways and can't get any of them to work. I have looked on MDN and W3S and it doesn't appear that JS supports String to Boolean natively although it does appear to support Boolean to String. I have also seen on one or two forums that people have created code snippets which do the job. However, for the moment I am going to leave this one and move on.