Making Decisions with Conditional Statements Flashcards
- Add a variable named answer and use the prompt() method to ask the question ‘What is the best programming language?’
- Add a conditional statement that opens an alert dialog box with the message “You are correct” when the answer contains the string ‘JavaScript’.
- Add an else clause to complete the conditional statement. Inside the else clause add another alert that says “JavaScript is the best language!”
var answer = prompt(“What is the best programming language?”);
if (answer === “JavaScript”) {
alert(“You are correct”);
} else {
alert(“JavaScript is the best language!”);
}
T/F: You must add an else clause to a conditional statement.
False
T/F: Is the following condition true or false?
( ‘javascript’ === ‘JavaScript’ )
False
When a browser runs the code listed below, what will the user see?
var spaceShips = 0;
if (spaceShips === 0 ) {
alert(‘You lose!’);
} else {
alert(‘You are still alive!’);
}
The user will see an alert dialog box with the message “You lose!”
T/F: Is the following condition true or false?
( ‘lion’ !== ‘zebra’ )
True
T/F: Is the following condition true or false?
( ‘3’ === 3 )
False
T/F: Is the following condition true or false?
( ‘lion’ > ‘zebra’ )
False
Add a conditional statement that tests if the value in the variable a is greater than the value in variable b. If it is, pop up an alert with the message ‘a is greater than b’; also add an else clause that pops up the message ‘a is not greater than b’.
var a = 10; var b = 20; var c = 30;
if (a > b) {
alert(“a is greater than b”);
} else {
alert(“a is not greater than b”);
}
Add a Boolean value to the condition in this conditional statement. Use a boolean value that will result in the message “This is true” appearing in an alert box.
if ( ) {
alert(‘This is true’);
} else {
alert(‘This is false’);
}
if ( true ) {
alert(‘This is true’);
} else {
alert(‘This is false’);
}
Add a conditional statement that test to see if the isAdmin variable is true. If it is then open an alert with the message “Welcome administrator”.
var isAdmin = true; var isStudent = false;
And an else if clause that tests to see if the isStudent variable is true. If it is then open an alert dialog with the message ‘Welcome student’.
Add a final else clause to this conditional statement so that if the isAdmin variable andisStudent variables are both false an alert opens with the message “Who are you?”
if ( isAdmin ) {
alert(‘Welcome administrator’);
} else if (isStudent) {
alert(‘Welcome student’);
} else {
alert(‘Who are you?’);
}
Say you had a conditional statement with one if clause, two else if clauses and an else clause, each with its own code block. What is the maximum number of code blocks in that conditional statement that can run?
1
Comment out the two lines of code below using JavaScript’s multi-line commenting syntax:
This isn’t part of the program It’s just a message to myself
/*
This isn’t part of the program It’s just a message to myself
*/
After this code runs, what is the value of the variable itemToBuy?
var itemToBuy = ‘’;
var savings = 1000;
if ( savings > 500 ) {
itemToBuy = ‘Computer’;
} else if ( savings > 200 ) {
itemToBuy = ‘Phone’;
} else if ( savings > 0 ) {
itemToBuy = ‘Dinner’;
} else { itemToBuy = ‘…still saving…’; }
‘Computer’
What does this condition evaluate to: true or false?
( true || false )
True
Given the following code, what will appear in the browser’s console?
var x = 10;
var y = 20;
if ( x < 10 && y > 10 ) {
console.log(‘The condition passes.’);
} else {
console.log(‘The condition fails.’);
}
The condition fails.