



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A collection of javascript exercises focused on loops, conditional statements, and other fundamental programming concepts. It includes example code snippets and solutions for each exercise, making it a valuable resource for learning and practicing javascript programming. The exercises cover various topics, including if-else statements, switch statements, logical operators, and function return values. Suitable for beginners and intermediate learners who want to solidify their understanding of javascript fundamentals.
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Create an ๐if statement inside the function to return "Yes, that was true" if the parameter ๐ wasThatTrue is true and return "No, that was false" otherwise. - Correct Ans: Add the equality operator to the indicated line so that the function will return "Equal" when val is equivalent to 12. - Correct Ans: if (val==12) Use the strict equality operator in the if statement so the function will return "Equal" when val is strictly equal to 7 - Correct Ans: if (val===7) Practice comparing different values || The โกcompareEqualityโฌ function in the editor compares two values using the equality operator. Modify the function so that it returns "Equal" only when the values are strictly equal. - Correct Ans: if (a === b) Comparison with the Inequality Operator - Correct Ans: if (val != 99) Comparison with the Strict Inequality Operator - Correct Ans: if (val!==17) Comparison with the Greater Than Operator - Correct Ans: Comparison with the Greater Than Or Equal To Operator - Correct Ans: if (val >= 10) Comparison with the Less Than Operator - Correct Ans: if (val<25) Comparison with the Less Than Or Equal To Operator - Correct Ans: if (val<=24)
Comparisons with the Logical And Operator - Correct Ans: if (val>=25 && val<=50) Comparisons with the Logical Or Operator - Correct Ans: if (val <=9 || val >=21 ) { Introducing Else Statements - Correct Ans: Introducing Else If Statements - Correct Ans: Chaining If Else Statements - Correct Ans: ๐Golf Code || Your function will be passed par and strokes arguments. Return the correct string according to this table which lists the strokes in order of priority; top (highest) to bottom (lowest): Strokes Return 1 "Hole-in-one!" <= par - 2 "Eagle" par - 1 "Birdie" par "Par" par + 1 "Bogey" par + 2 "Double Bogey"
= par + 3 "Go Home!" par and strokes will always be numeric and positive. We have added an array of all the names for your convenience. - Correct Ans: Selecting from Many Options with Switch Statements Write a switch statement which tests val and sets answer for the following conditions: 1 - "alpha" 2 - "beta"
7-9 - "High" - Correct Ans: switch(val){ case 1: case 2: case 3: return "Low"; break; case 4: case 5: case 6: return "Mid"; break; case 7: case 8: case 9: return "High"; break; } Replacing If Else Chains with Switch || if (val === "bob") { answer = "Marley"; } else if (val === 42) { answer = "The Answer"; } else if (val === 1) { answer = "There is no #1"; } else if (val === 99) { answer = "Missed me by this much!"; } else if (val === 7) {
answer = "Ate Nine"; } - Correct Ans: Returning Boolean Values from Functions || function isLess(a, b) { if (a < b) { return true; } else { return false; } } Fix the function isLess to remove the if/else statements. - Correct Ans: Return Early Pattern for Functions || function myFun() { console.log("Hello"); return "World"; console.log("byebye") } myFun(); The above outputs "Hello" to the console, returns "World", but "byebye" is never output, because the function exits at the return statement. - Correct Ans:
case 6: count++; break; case 10: case 'J': case 'Q': case 'K': case 'A': count--; break; } if (count>0){ return count + ' Bet'; }else{ return count +' Hold'; } // Only change code above this line } cc(2); cc(3); cc(7); cc('K'); cc('A');