Loops - Exercise 4

Question 1: Multiply Until a Limit

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)

javascript

function doubleUntilLimit(limit) {
  // Write your solution here
}

console.log(doubleUntilLimit(10));

Question 2: Count Doublings

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)

javascript

function countDoublingsToLimit(limit) {
  // Write your solution here
}

console.log(countDoublingsToLimit(10));

Question 3: Sum Until a Zero

Sum numbers in order until the first zero, excluding the zero and all later values.

Expected output: 5 (print this with console.log)

javascript

function sumBeforeFirstZero(numbers) {
  // Write your solution here
}

console.log(sumBeforeFirstZero([2,3,0,9]));

Question 4: Skip Negative Values

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)

javascript

function skipNegativeLoopValues(numbers) {
  // Write your solution here
}

console.log(skipNegativeLoopValues([-1,0,2,-3]));

Question 5: Find the First Multiple

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)

javascript

function firstDivisibleLoopValue(numbers, divisor) {
  // Write your solution here
}

console.log(firstDivisibleLoopValue([7,10,12], 5));

Question 6: Count Until a Word

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)

javascript

function countWordsBeforeStop(words, stop) {
  // Write your solution here
}

console.log(countWordsBeforeStop(["a","b","stop","c"], "stop"));

Question 7: Collect Until a Limit

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)

javascript

function collectUntilLimitExceeded(numbers, limit) {
  // Write your solution here
}

console.log(collectUntilLimitExceeded([1,3,8,2], 5));

Question 8: Count Digits with Division

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)

javascript

function countDigitsByDivision(number) {
  // Write your solution here
}

console.log(countDigitsByDivision(2048));

Question 9: Sum Digits with Division

Return the sum of the decimal digits of a non-negative integer using a loop.

Expected output: 8 (print this with console.log)

javascript

function sumDigitsByDivision(number) {
  // Write your solution here
}

console.log(sumDigitsByDivision(305));

Question 10: Find a Divisor Count

Return the number of positive integers that divide the supplied positive integer without a remainder.

Expected output: 4 (print this with console.log)

javascript

function countPositiveDivisors(number) {
  // Write your solution here
}

console.log(countPositiveDivisors(6));