Docsity

JavaScript Echelons 5: Loop Exercises and Solutions, Exams of Computer Science

Toggle sidebar
bg1
๐•๐•’๐•ง๐•’๐•Š๐•”๐•ฃ๐•š๐•ก๐•ฅ ๐”ผ๐•”๐•™๐•–๐•๐• ๐•Ÿ ๐Ÿ -
ใ€Ž๐•ƒ๐• ๐• ๐•กใ€Exan Questions and Complete
Solutions
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)
pf3
pf4
pf5

Partial preview of the text

Download JavaScript Echelons 5: Loop Exercises and Solutions and more Exams Computer Science in PDF only on Docsity!

ใ€Ž๐•ƒ๐• ๐• ๐•กใ€ Exan Questions and Complete

Solutions

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');