0

I am trying to make a function called range which takes 3 integer parameters: start, end, and step.

The function should return an array of numbers from start to end counting by step.

The function should return an empty array if given incorrect parameters, such as:

Start, end, or step being undefined. Start being greater than end. Step being negative.

This is my code:

const range = function(start, end, step) {
  const result = []
  for (let i = start; i <= end; i += step) {
    return i || end || step == undefined ? []
                                        : i > end ? []
                                                  : step < 0 ? []
                                                            : result.push(i)
    }
  return result;
}

console.log(range(0, 10, 2));
console.log(range(10, 30, 5));
console.log(range(-5, 2, 3));

All of these return an empty array.

I am new to JavaScript, so please...the more you explain, the more you help me.

Thank you.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • `I am new to Java` A suggestion: learn the name of the language you're writing in – CertainPerformance Aug 10 '18 at 04:44
  • Looks familiar, except for the error handling: https://stackoverflow.com/questions/51777264/looping-range-step-by-step – ASDFGerte Aug 10 '18 at 04:46
  • Reformatted your code : helps to show the structure. Suggestion: multiple condition operators are hard to follow so generally they are avoided. – Richard Aug 10 '18 at 04:48
  • What would be a good way of writing the conditions? – Chris Gardner Aug 10 '18 at 04:49
  • I recommend to read about `clean code` :-) there are so many `if` statements, that it surely can be written more human-friendly :-) I believe that when you will split all those `?` operations to separate pieces of code, you will get better understanding of your own code :-) – Paradowski Aug 10 '18 at 04:50
  • @ChrisGardner: see the answers... – Richard Aug 10 '18 at 04:52

3 Answers3

2

The problem is that

return i || end || step == undefined ? []

will test for the truthyness of i, or for the truthyness of end, or for if step is undefined. If either i or end are positive numbers (among other things), the entire condition will evaluate to true, and the empty array will be returned.

If you want to check to see if any of the arguments are undefined, you might use .some, passing in an array of the arguments.

You also shouldn't return inside the for loop - test to see if the inputs are invalid first and return if invalid. Then, iterate through the loop, pushing to result, and once the loop finishes, return the result. For example, here's one possible implementation:

const range = function(start, end, step) {
  if (
    [start, end, step].some(arg => arg === undefined)
    || start > end
    || step < 0
  ) {
    return [];
  }
  const result = []
  for (let i = start; i <= end; i += step) {
    result.push(i)
  }
  return result;
}

console.log(range(10, 0, 3));
console.log(range(0, 10, 2));
console.log(range(10, 30, 5));
console.log(range(-5, 2, 3));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

The first step in the for loop is a return. This will exit the function.

The expression on the return will return start (the initial value of i) unless it is zero, in which case end will be returned.

Richard
  • 106,783
  • 21
  • 203
  • 265
0

const range = function(start, end, step) {
  const result = []
  for (let i = start; i <= end; i += step) {
    i==undefined ? [] : end==undefined
    ? [] : step==undefined 
    ? [] : i > end 
    ? [] : step < 0 
    ? [] : result.push(i)
  }
  return result;
}

console.log(range(10, 0, 3));
console.log(range(0, 10, 2));
console.log(range(10, 30, 5));
console.log(range(-5, 2, 3));
Abhishek
  • 382
  • 1
  • 6