Loops - Exercise 4
Question 1: Multiply Until a Limit Completed
Starting at 1, repeatedly double the value until it is at least the supplied positive limit. Return the resulting value.
Expected output: 16 (print this with console.log)
function doubleUntilLimit(limit) {
// Write your solution here
}
console.log(doubleUntilLimit(10));function doubleUntilLimit(limit) {
let value = 1;
while (value < limit) value *= 2;
return value;
}
console.log(doubleUntilLimit(10));Question 2: Count Doublings Completed
Starting at 1, count how many doublings are needed to reach or exceed the supplied positive limit.
Expected output: 4 (print this with console.log)
function countDoublingsToLimit(limit) {
// Write your solution here
}
console.log(countDoublingsToLimit(10));function countDoublingsToLimit(limit) {
let value = 1;
let count = 0;
while (value < limit) { value *= 2; count++; }
return count;
}
console.log(countDoublingsToLimit(10));Question 3: Sum Until a Zero Completed
Sum numbers in order until the first zero, excluding the zero and all later values.
Expected output: 5 (print this with console.log)
function sumBeforeFirstZero(numbers) {
// Write your solution here
}
console.log(sumBeforeFirstZero([2,3,0,9]));function sumBeforeFirstZero(numbers) {
let total = 0;
for (const number of numbers) { if (number === 0) break; total += number; }
return total;
}
console.log(sumBeforeFirstZero([2,3,0,9]));Question 4: Skip Negative Values Completed
Return a new array containing the non-negative numbers from the input, using continue to skip negatives.
Expected output: [0,2] (print this with console.log)
function skipNegativeLoopValues(numbers) {
// Write your solution here
}
console.log(skipNegativeLoopValues([-1,0,2,-3]));function skipNegativeLoopValues(numbers) {
const values = [];
for (const number of numbers) { if (number < 0) continue; values.push(number); }
return values;
}
console.log(skipNegativeLoopValues([-1,0,2,-3]));Question 5: Find the First Multiple Completed
Return the first value in the array divisible by the positive divisor, or null if none exists.
Expected output: 10 (print this with console.log)
function firstDivisibleLoopValue(numbers, divisor) {
// Write your solution here
}
console.log(firstDivisibleLoopValue([7,10,12], 5));function firstDivisibleLoopValue(numbers, divisor) {
for (const number of numbers) if (number % divisor === 0) return number;
return null;
}
console.log(firstDivisibleLoopValue([7,10,12], 5));Question 6: Count Until a Word Completed
Return the number of words before the first exact occurrence of the supplied stop word. If absent, return the array length.
Expected output: 2 (print this with console.log)
function countWordsBeforeStop(words, stop) {
// Write your solution here
}
console.log(countWordsBeforeStop(["a","b","stop","c"], "stop"));function countWordsBeforeStop(words, stop) {
let count = 0;
for (const word of words) { if (word === stop) break; count++; }
return count;
}
console.log(countWordsBeforeStop(["a","b","stop","c"], "stop"));Question 7: Collect Until a Limit Completed
Return numbers from the start of the array until the first value greater than the limit, excluding that value.
Expected output: [1,3] (print this with console.log)
function collectUntilLimitExceeded(numbers, limit) {
// Write your solution here
}
console.log(collectUntilLimitExceeded([1,3,8,2], 5));function collectUntilLimitExceeded(numbers, limit) {
const result = [];
for (const number of numbers) { if (number > limit) break; result.push(number); }
return result;
}
console.log(collectUntilLimitExceeded([1,3,8,2], 5));Question 8: Count Digits with Division Completed
Count the decimal digits in a non-negative integer using a do...while loop. Zero has one digit.
Expected output: 4 (print this with console.log)
function countDigitsByDivision(number) {
// Write your solution here
}
console.log(countDigitsByDivision(2048));function countDigitsByDivision(number) {
let count = 0;
do { count++; number = Math.floor(number / 10); } while (number > 0);
return count;
}
console.log(countDigitsByDivision(2048));Question 9: Sum Digits with Division Completed
Return the sum of the decimal digits of a non-negative integer using a loop.
Expected output: 8 (print this with console.log)
function sumDigitsByDivision(number) {
// Write your solution here
}
console.log(sumDigitsByDivision(305));function sumDigitsByDivision(number) {
let total = 0;
while (number > 0) { total += number % 10; number = Math.floor(number / 10); }
return total;
}
console.log(sumDigitsByDivision(305));Question 10: Find a Divisor Count Completed
Return the number of positive integers that divide the supplied positive integer without a remainder.
Expected output: 4 (print this with console.log)
function countPositiveDivisors(number) {
// Write your solution here
}
console.log(countPositiveDivisors(6));function countPositiveDivisors(number) {
let count = 0;
for (let i = 1; i <= number; i++) if (number % i === 0) count++;
return count;
}
console.log(countPositiveDivisors(6));