if / else statement.
//The Conditional Challenge
//Use conditional statements to build a multiple question quiz.
/* codiing
1. Ask five questions
2. Keep track of the number of questions the user answered correctly
3. Provide a final message after the quiz letting the user know
the number of questions he or she got right.
4. rank the player
- 5 correct gold crown
- 3-4 correct silver crown
- 2-1 correct bronze crown
- 0
*/
// Start with a variable to hold the number of correct answers
// quiz begins, no answers correct
var correct = 0;
// ask questions
var answer1 = prompt("Name a programming language that's also a gem");
if ( answer1.toUpperCase() === 'RUBY'){
correct += 1; // add 1 to this variable for each question answer correctly
}
var answer2 = prompt("Name a programming language that's also a snake");
if ( answer2.toUpperCase() === 'PYTHON'){
correct += 1; // add 1 to this variable for each question answer correctly
}
var answer3 = prompt("What language do you use to style web pages");
if ( answer3.toUpperCase() === 'CSS'){
correct += 1; // add 1 to this variable for each question answer correctly
}
var answer4 = prompt("What language do you use to build the structure of web pages");
if ( answer4.toUpperCase() === 'HTML'){
correct += 1; // add 1 to this variable for each question answer correctly
}
var answer5 = prompt("What language do you use to add interactivity to the web");
if ( answer5.toUpperCase() === 'JAVSCRIPT'){
correct += 1; // add 1 to this variable for each question answer correctly
}
// output the result
// tell the player how many questions she have right
document.write('<p>You got " + correct + " out of 5 questions</p>');
// I provide a message based on the number of questions answer correctly
//outpt ranking
if(correct === 5){
document.write('<p>You earned a gold crown!</p>');
} else if (correct >= 3){
document.write('<p>You earned a silver crown!</p>');
} else if (correct >= 1){
document.write('<p>You earned a bronze crown!</p>');
} else {
document.write('<p>No crown for you. You need to study!</p>');
}
------------
var money = 9;
var today = 'Friday'
if ( money > 100 && today === 'Friday' ) { // false - true
alert("Time to go to the theater");
} else if ( money > 50 && today === 'Friday' ) { // false - true
alert("Time for a movie and dinner");
} else if ( money > 10 && today === 'Friday' ) {// false - true
alert("Time for a movie");
} else if ( money < 100 && today === 'Friday' ) {// false
alert("It's Friday, but I don't have enough money to go out");
} else {
alert("This isn't Friday. I need to stay home.");
}
//Guess Game
// Perform 3 test
// Does the player guess match the random number
// is the player guess least than the random number
// is the player guess greater than th erandom number
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt("I'm thinking of a number between 1 and 6. What is it?");
if(parseInt(guess) === randomNumber){
correctGuess = true;
} else if (parseInt(guess) < randomNumber){
var guessMore = prompt('Try again. The number I am thinking is more than ' + guess);
if(parseInt(guessMore) === randomNumber){ // only run if the else if statement is true
correctGuess = true;
}
} else if (parseInt(guess) > randomNumber){
var guessLess = prompt('Try again. The number I am thinking is less than ' + guess);
if(parseInt(guessLess) === randomNumber){ // only run if the else if statement is true
correctGuess = true;
}
}
if(correctGuess){
document.write('<p>You guessed the number!</p>');
}
else{
document.write('<p>Sorry. the number was' + randomNumber + '</p>');
}
// Document Your Code With Comments
/* The random Number Guessing Game
generates a number between 1 and 6
and gives a player two attempts to guess the number */
// assume the player didn't guess correctly
var correctGuess = false;
//generate random number from 1 to 6
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt("I'm thinking of a number between 1 and 6. What is it?");
/* test to see if player is
1. correct
2. guessed to high
3. guesse to low
*/
if(parseInt(guess) === randomNumber){
correctGuess = true;
} else if (parseInt(guess) < randomNumber){
var guessMore = prompt('Try again. The number I am thinking is more than ' + guess);
if(parseInt(guessMore) === randomNumber){
correctGuess = true;
}
} else if (parseInt(guess) > randomNumber){
var guessLess = prompt('Try again. The number I am thinking is less than ' + guess);
if(parseInt(guessLess) === randomNumber){
correctGuess = true;
}
}
// test if player is corretc and output response
if(correctGuess){
document.write('<p>You guessed the number!</p>');
}
else{
document.write('<p>Sorry. the number was' + randomNumber + '</p>');
}
//if statement
//quiz
var answer = prompt('what programming language is the name of the gem?');
if( answer === 'Ruby'){ //if answer is = to ruby run code
document.write("<p>That's right!</p>"); // if is true
}
else{
document.write("<p>You wrong!</p>"); // if is false
}
// is is true the first condition runs if not the second condition run
-------------
Comparison
if (spaceShips === 0){
alert('Game Over!');
}
(100 <= 100) - true
(-12 < 0 ) - true
("3" == 3) - true
("3" === 3) - false (always use ===)
Better to use !== than != (compare types two strings two numbers)
(10 !== 9) - true
('10' !== 9) - true
(-59 !== 59) - false
('apple' < 'bear') - true because a came first that b on the alphabet
//make a game harder if the score ids greater than 100
------------
//Build a random number guessing game
// var randomNumber = Math.floor(Math.random() * 6 ) + 1; Basic formula for generating a random number
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt('I'm thinking of a number between 1 and 6. What is it?");
if(parseInt(guess) === randomNumber){
document.write('<p>You guessed the number!</p>');
}
else{
document.write('<p>Sorry. the number was' + randomNumber + '</p>');
}
-----------
// Boolean
//You can assign a boolean to a variable
var correctGuess = false; //the variable only change if the player get the correct answer
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt('I'm thinking of a number between 1 and 6. What is it?");
if(parseInt(guess) === randomNumber){
correctGuess = true; //if it guess the condition change to true
}
//then test variable
if(correctGuess === true){
document.write('<p>You guessed the number!</p>');
}
else{
document.write('<p>Sorry. the number was' + randomNumber + '</p>');
}
if (){
//code block that holds different statements
}
if (12 / 4 === "Ari".length) {
confirm("Will this run the first block?");
} else {
confirm("Or the second block?");
}
//----------
if("Jon".length * 2 / (2+1) === 2)
{
console.log("The answer makes sense!");
}
else {
console.log("Error Error Error");
}
//----------
var number;
number = 21;
if (number > 10 && number < 20) {
console.log(number + " is between 10 and 20.");
} else {
console.log(number + " is NOT between 10 and 20.");
}
//---------
var number;
number = 21;
if (number > 10 && number < 20) {
console.log(number + " is between 10 and 20.");
} else if (number <= 10) {
console.log(number + " is less than 10.");
} else {
console.log(number + " is greater than 20.");
}