Loops - Exercise 1
Question 1: List Positive Steps Completed
Using a loop, return the integers from 1 through the supplied non-negative limit, inclusive.
Expected output: [1,2,3,4] (print this with console.log)
function listPositiveSteps(limit) {
// Write your solution here
}
console.log(listPositiveSteps(4));function listPositiveSteps(limit) {
const result = [];
for (let i = 1; i <= limit; i++) result.push(i);
return result;
}
console.log(listPositiveSteps(4));Question 2: List Even Steps Completed
Return the even integers from 0 through the supplied non-negative limit, inclusive.
Expected output: [0,2,4] (print this with console.log)
function listEvenSteps(limit) {
// Write your solution here
}
console.log(listEvenSteps(5));function listEvenSteps(limit) {
const result = [];
for (let i = 0; i <= limit; i += 2) result.push(i);
return result;
}
console.log(listEvenSteps(5));Question 3: List an Inclusive Range Completed
Return the integers from start to end inclusive. Return an empty array when start exceeds end.
Expected output: [3,4,5,6] (print this with console.log)
function listInclusiveRange(start, end) {
// Write your solution here
}
console.log(listInclusiveRange(3, 6));function listInclusiveRange(start, end) {
const values = [];
for (let i = start; i <= end; i++) values.push(i);
return values;
}
console.log(listInclusiveRange(3, 6));Question 4: List Descending Odd Numbers Completed
Return the positive odd integers no greater than the non-negative start, in descending order.
Expected output: [5,3,1] (print this with console.log)
function descendingOddNumbers(start) {
// Write your solution here
}
console.log(descendingOddNumbers(6));function descendingOddNumbers(start) {
const values = [];
for (let i = start; i > 0; i--) if (i % 2 !== 0) values.push(i);
return values;
}
console.log(descendingOddNumbers(6));Question 5: Sum an Inclusive Range Completed
Return the sum of integers from start to end inclusive, or zero when start is greater than end.
Expected output: 14 (print this with console.log)
function sumInclusiveIntegerRange(start, end) {
// Write your solution here
}
console.log(sumInclusiveIntegerRange(2, 5));function sumInclusiveIntegerRange(start, end) {
let total = 0;
for (let i = start; i <= end; i++) total += i;
return total;
}
console.log(sumInclusiveIntegerRange(2, 5));Question 6: Build Multiples Completed
Return the first count positive multiples of the supplied number. Count is a non-negative integer.
Expected output: [3,6,9,12] (print this with console.log)
function buildFirstMultiples(number, count) {
// Write your solution here
}
console.log(buildFirstMultiples(3, 4));function buildFirstMultiples(number, count) {
const values = [];
for (let i = 1; i <= count; i++) values.push(number * i);
return values;
}
console.log(buildFirstMultiples(3, 4));Question 7: Calculate a Factorial Completed
Return the factorial of a non-negative integer using a loop. The factorial of 0 is 1.
Expected output: 120 (print this with console.log)
function factorialWithLoop(number) {
// Write your solution here
}
console.log(factorialWithLoop(5));function factorialWithLoop(number) {
let result = 1;
for (let i = 2; i <= number; i++) result *= i;
return result;
}
console.log(factorialWithLoop(5));Question 8: Sum the First Squares Completed
Return the sum of the squares from 1 through the non-negative limit.
Expected output: 14 (print this with console.log)
function sumFirstSquares(limit) {
// Write your solution here
}
console.log(sumFirstSquares(3));function sumFirstSquares(limit) {
let total = 0;
for (let i = 1; i <= limit; i++) total += i * i;
return total;
}
console.log(sumFirstSquares(3));Question 9: Build a Number Staircase Completed
Return strings of one through count asterisks in an array. Count is non-negative.
Expected output: ["*","**","***"] (print this with console.log)
function buildAsteriskStaircase(count) {
// Write your solution here
}
console.log(buildAsteriskStaircase(3));function buildAsteriskStaircase(count) {
const rows = [];
for (let i = 1; i <= count; i++) rows.push("*".repeat(i));
return rows;
}
console.log(buildAsteriskStaircase(3));Question 10: Sum Odd Numbers to a Limit Completed
Return the sum of positive odd integers no greater than the non-negative limit.
Expected output: 16 (print this with console.log)
function sumOddNumbersToLimit(limit) {
// Write your solution here
}
console.log(sumOddNumbersToLimit(7));function sumOddNumbersToLimit(limit) {
let total = 0;
for (let i = 1; i <= limit; i += 2) total += i;
return total;
}
console.log(sumOddNumbersToLimit(7));