Implement a function named generateRange(start, stop, step), which takes three arguments and generates a range of integers from start to stop, with the step. The first integer is value of the start, the second is the end of the range and the third is the step. (start may be less then stop (then step < 0)
Note: step != 0
Task
Create a function named
generateInterval(2, 10, 2) // [2,4,6,8,10]
generateInterval(2, -10, -2) // [2,0,-2,-4,-6,-8,-10]
function generateInterval(start, stop, step) { const arr = []; let value = start; for (let index = 0; index <= (stop - start) / step; index++) { arr.push(value); value += step; } return arr; }
// Since Node 10, we're using Mocha. // You can use `chai` for assertions. const chai = require("chai"); const assert = chai.assert; const Test = require("@codewars/test-compat"); describe("generateInterval(2, 10, 2)", function() { it("startLessThenStop", function() { Test.assertSimilar(generateInterval(2, 10, 2), [2, 4, 6, 8, 10]); }); }); describe("generateInterval(2, 10, -2)", function() { it("startBiggerThenStop", function() { Test.assertSimilar(generateInterval(2, -10, -2), [2,0,-2,-4,-6,-8,-10]); }); });
Implement a function named generateRange(start, stop, step), which takes three arguments and generates a range of integers from start to stop, with the step. The first integer is value of the start, the second is the end of the range and the third is the step. (start may be less then stop (then step < 0)
Note: step != 0
Task
Create a function named
generateInterval(2, 10, 2) // [2,4,6,8,10]
generateInterval(2, -10, -2) // [2,0,-2,-4,-6,-8,-10]
function generateInterval(start, stop, step) {
const arr = [];
let value = start;
for (let index = 0; index <= (stop - start) / step; index++) {
arr.push(value);
value += step;
}
return arr;
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
const Test = require("@codewars/test-compat");
describe("generateInterval(2, 10, 2)", function() {
it("startLessThenStop", function() {
Test.assertSimilar(generateInterval(2, 10, 2), [2, 4, 6, 8, 10]);
});
});
describe("generateInterval(2, 10, -2)", function() {
it("startBiggerThenStop", function() {
Test.assertSimilar(generateInterval(2, -10, -2), [2,0,-2,-4,-6,-8,-10]);
});
});