Sample Coding Questions Flashcards
When logical OR, if one of the sides is TRUE it will return…
True!
Example:
true || false // it will evaluate to true!
When logical AND, if one of the sides is false it will return…
False!
Example:
true && false // will evaluate to false
Syntax of the IF statement
if (condition) { // block of code to be executed if the condition is true }
Make a Good Day greeting if the hour is less then 18:00
if (hour < 18) {
greeting = “Good day”;
}
If…else syntax
if (condition) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false }
Else if syntax
if (condition1) { // block of code to be executed if condition1 is true } else if (condition2) { // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false }
Why use the Switch Statement?
Use the switch statement to select one of many code blocks to be executed.
Switch statement syntax
switch(expression) { case x: // code block break; case y: // code block break; default: // code block }
Always have a DEFAULT!
How does the switch statement work?
The switch expression is evaluated once.
The value of the expression is compared with the values of each case.
If there is a match, the associated block of code is executed.
If there is no match, the default code block is executed.
Example of Switch statement
The getDay() method returns the weekday as a number between 0 and 6.
(Sunday=0, Monday=1, Tuesday=2 ..)
switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; }
What is the break keyword meant for in a switch statement?
When JavaScript reaches a break keyword, it breaks out of the switch block.
This will stop the execution inside the switch block.
It is not necessary to break the last case in a switch block. The block breaks (ends) there anyway.
Possible Exam Question….
Does the default case has to be the last case in a switch block?
No
IF default is not the last case in the switch block, remember to END the default case with a break.
Example: switch (new Date().getDay()) { default: text = "Looking forward to the Weekend"; break; case 6: text = "Today is Saturday"; break; case 0: text = "Today is Sunday"; }
What happends if multiple cases matches a case value (Switch statement)
If multiple cases matches a case value, the first case is selected.
If no matching cases are found, the program continues to the default label.
If no default label is found, the program continues to the statement(s) after the switch.
Switch cases use strict comparison (===).
The values must be of the same type to match.
A strict comparison can only be true if the operands are of the same type.
What will happen if the break statements in the following code block are replaced with the continue statements? (Assume that the value of test is ‘C’.)
switch(test) {
case ‘A’: alert(“Excellent.”); break;
case ‘B’: alert(“Good.”); break;
case ‘C’: alert(“Average.”); break;
case ‘D’: alert(“Below average.”); break;
default: (“Disqualified.”); break;
}
The given code block will display the SyntaxError: Illegal continue statement: no surrounding iteration statement message in the console window, because of the continue statement, which can be used in loops (or iteration statements).
Consider X and Z to be true and Y to be false, which of the following conditions will return false?
Y || Z
Y && Z
X && Z
X || Y
The condition Y && Z will return false, because the AND operator returns true only if both the operands are true. And, in this case, the operand Y is false
Which of the following statements will execute only if the conditions A and B are true? A. if (A) && if (B) B. if (A || B) C. if (A) & if (B) D. if (A && B)
The && logical operator is used to combine expressions so that the entire expression returns true only if all the individual conditions in the expression are true.
What is is true of control structures?
They make decisions based on Boolean values.
What can be a substitute for the nested if-else statements?
The switch statements can be used to substitute a nested if-else block. The conditions that are being tested in the if-else block can be specified as a case in the switch block
What is the purpose of the switch statement?
To compare a value against other values, to search for a match.
The switch statement compares a value against other values, to search for a match. If a match is found, then the code associated with the match will execute. The break statement is then used to exit the switch block of code. Essentially, the switch statement functions the same as multiple if-else clauses within an if statement. However, the switch statement is more readable and allows you to specify a default set of statements to execute if no match is foun
What is true about the do-while statement?
The do-while statement operates like the while statement, with one key difference. The do-while statement does not check the conditional expression until after the first time through the loop, guaranteeing that the code within the curly braces will execute at least once. A do-while loop can accomplish some tasks that the while loop cannot. The difference between the two control structures is that the do-while loop will execute its code at least one time, regardless of the Boolean value returned by the test condition.
Consider the following code:
for (X; Y; Z)
What does Y represent in this statement?
In the given statement for (X; Y; Z), Y is the condition under which the loop will execute. X is the loop counter variable initialization expression. Z is the expression that increments or decrements the loop counter
Consider the following code snippet:
var x = 100; while (x > 5) { x--; }
What will be the value of x after the execution of the given code snippet?
The value of x after the execution of this code will be 5. The condition under which the while statement executes is x > 5, so the last condition of the while loop will be executed when x has a value of 6. Now, the code inside the while statement will execute for x = 6, which is greater than 5, and the value of x will be decremented by 1 one more time making its value 5
Consider the following code:
var myPet= "rabbit"; switch (myPet) { case "Rabbit": document.write("My pet is a Rabbit."); break; case "Frog": document.write("My pet is a Frog."); break; default: document.write("I do not have a pet."); }
JavaScript is a case-sensitive language. Thus, the default output (I do not have a pet.) will be the result because the variable rabbit did not match with the variable Rabbit due to the case of the letter ‘R’
What is a way to exit a loop that would otherwise continue to execute?
A way to exit a loop that would otherwise continue to execute is to use the break statement. Usually, you will find the break keyword inside an if clause. If a certain condition is reached, the program will break out of the loop. If not, the loop will continue. You can use the break statement within the for, while, and do…while loops, as well as with the switch statement
Which JavaScript statement is used to force the flow of control back to the top of a loop?
continue
Feedback:
The continue statement is used to force the flow of control back to the top of a loop. The continue statement can be considered as a “skip” or bypass command. When execution hits the continue statement, all statements between it and the end of the loop block are skipped, and execution returns to the top of the loop. The test condition for the loop is then evaluated to determine whether the loop should execute again. The continue statement can be used only within the for or the while loop
Consider the following code:
var j = 0, i; for (i = 0; i < 10; i++) { document.write(i + " + "); j += i; } document.write("=" + j);
What is the output when this script is run in the browser?
The loop executes 10 times and sums the numbers using j. An extra addition operator (+) appears because the document.write() method includes it after every number. It performs addition rather than concatenation because it recognizes the numbers are not string
Is the number of iterations in a do-while loop always one more than the while loop?
Not always.
Here both iterate 1 time:
// while loop var x = 1; while (x > 0) { document.write("a"); x--; } // dowhile loop var x = 1; do { document.write("a"); x--;
Which JavaScript statement should you use to test for a single condition and branch to a process?
The if and if…else statements provide the ability to branch to one of two or more processes, depending on the result of some test condition that you have scripted. The if statement is used to test for a single condition.
Consider the following code block:
if (test == 10) {
alert(“Disqualified.”);
} else if (test == 25) {
alert(“Passed with grace marks.”);
} else if (test == 40) {
alert(“Passed.”);
} else if (test == 50) {
alert(“Passed with honors.”);
} else {
alert(“Inconclusive score.”);
}
The switch statement is best suited to replace the given code of segment. while and for are looping statements, therefore they will not be of much help. The problem with if statements without the else block is the last alert “inconclusive score.” If all other conditions fail, the else block must execute. Realizing this logic through if statements only will be inefficien
Consider the following code snippet:
if (example = 25) {
document.write(“Correct answer.”);
} else {
document.write(“Wrong answer.”);
}
Assuming the value of example is 10, what will be the output of the code snippet?
In the if block, the test expression example = 25 will always return 25, because instead of the equality comparison operator (==), the assignment operator (=) is used.
Will this run? Look at the missing curly braces….
if (timerMinutes == 10) alert(“Time Expired!”);
Yes, it will!
If the code associated with a test condition consists of just one line, you can omit the curly braces. There should be no quotation marks around the variable named (timerMinutes)
How does JS evaluate the day’s placement in a week?
It doesn’t. It compares the values of the strings letter-by-letter and returns the value in the Boolean form.
Example:
var day = "Thursday"; if(day < "Wednesday"){ window.alert("It's before today"); } else{ window.alert("It's after today") }
In this case, the string Thursday has the first letter as ‘T’ which is less than the letter ‘W’ of the string Wednesday, so the value Thursday is less than Wednesday. So, the operation evaluates to true and the script displays the output as It’s before today
Consider the following code:
var test = 100; if(test <= 6 && test >= 200 || test != 2) { window.alert("Good"); } else{ window.alert("Bad"); }
The code is well-formed, so the window alert box will appear and display Good. Because this script uses an if/else statement, only one alert box will appear. The value of test (100) is not equal to 2 (see the last condition after the || operator), so the entire expression evaluates to true. Thus, the Boolean evaluates to true so the value Good is displayed.
What will be the output of the following code block?
for (a = 10; a > 0; a–) {
document.write(a + “, “);
if (a == 5) break;
}
The for block will execute as usual, displaying the value of a at each iteration. But at the sixth iteration, the condition inside the if statement will result true, and the break; statement will execute, causing the loop to end. (
Different Kinds of Loops
JavaScript supports different kinds of loops:
for - loops through a block of code a number of times
for/in - loops through the properties of an object
for/of - loops through the values of an iterable object
while - loops through a block of code while a specified condition is true
do/while - also loops through a block of code while a specified condition is true
What does the forEach method?
The forEach() method calls a function for each element in an array. The forEach() method is not executed for empty elements.
Example of forEach method
let sum = 0; const numbers = [65, 44, 12, 4]; numbers.forEach(myFunction);
document.getElementById(“demo”).innerHTML = sum;
function myFunction(item) { sum += item; }