Making Decisions with Conditional Statements Flashcards

1
Q
  • 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!”
A

var answer = prompt(“What is the best programming language?”);
if (answer === “JavaScript”) {
alert(“You are correct”);
} else {
alert(“JavaScript is the best language!”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

T/F: You must add an else clause to a conditional statement.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

T/F: Is the following condition true or false?

( ‘javascript’ === ‘JavaScript’ )

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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!’);

}

A

The user will see an alert dialog box with the message “You lose!”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

T/F: Is the following condition true or false?

( ‘lion’ !== ‘zebra’ )

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

T/F: Is the following condition true or false?

( ‘3’ === 3 )

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

T/F: Is the following condition true or false?

( ‘lion’ > ‘zebra’ )

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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;
A

if (a > b) {
alert(“a is greater than b”);
} else {
alert(“a is not greater than b”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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’);
}

A

if ( true ) {
alert(‘This is true’);
} else {
alert(‘This is false’);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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?”

A

if ( isAdmin ) {
alert(‘Welcome administrator’);
} else if (isStudent) {
alert(‘Welcome student’);
} else {
alert(‘Who are you?’);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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?

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A

/*

This isn’t part of the program It’s just a message to myself

*/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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…’; }

A

‘Computer’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does this condition evaluate to: true or false?

( true || false )

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.’);

}

A

The condition fails.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does this condition evaluate to: true or false?

( true && false )

A

False

17
Q

Complete the code below so that either condition must be true for the alert message to appear:

if ( username === ‘’ _____ password === ‘’) {

alert(“Password or user name is missing”);

}

A

||

18
Q

Given the following code, what will appear in the browser’s console?

var ships = 10;

var score = 0;

if ( ships === 0 || score === 0 ) {

console.log(‘Game over.’);

} else {

console.log(‘Your score is zero.’);

}

A

Game over.

19
Q

Complete the code below so that both conditions must be true for the alert message to appear:

if ( loggedIn === true _____ level === ‘admin’) {

alert(“Access granted.”);

}

A

&&