* fix: add 9 solutions to challenges * fix: remove extra line Co-Authored-By: RandellDawson <5313213+RandellDawson@users.noreply.github.com>
2.6 KiB
2.6 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
587d7b86367417b2b2512b3d | Prevent Infinite Loops with a Valid Terminal Condition | 1 |
Description
while
loop inside loopy()
. Do NOT call this function!
function loopy() {It's the programmer's job to ensure that the terminal condition, which tells the program when to break out of the loop code, is eventually reached. One error is incrementing or decrementing a counter variable in the wrong direction from the terminal condition. Another one is accidentally resetting a counter or index variable within the loop code, instead of incrementing or decrementing it.
while(true) {
console.log("Hello, world!");
}
}
Instructions
myFunc()
function contains an infinite loop because the terminal condition i != 4
will never evaluate to false
(and break the looping) - i
will increment by 2 each pass, and jump right over 4 since i
is odd to start. Fix the comparison operator in the terminal condition so the loop only runs for i
less than or equal to 4.
Tests
tests:
- text: Your code should change the comparison operator in the terminal condition (the middle part) of the <code>for</code> loop.
testString: assert(code.match(/i\s*?<=\s*?4;/g).length == 1, 'Your code should change the comparison operator in the terminal condition (the middle part) of the <code>for</code> loop.');
- text: Your code should fix the comparison operator in the terminal condition of the loop.
testString: assert(!code.match(/i\s*?!=\s*?4;/g), 'Your code should fix the comparison operator in the terminal condition of the loop.');
Challenge Seed
function myFunc() {
for (let i = 1; i != 4; i += 2) {
console.log("Still going!");
}
}
Solution
function myFunc() {
for (let i = 1; i <= 4; i += 2) {
console.log("Still going!");
}
}