1

Hi im doing the first part of eloquent javascript chp4 The Sum of a Range. After spending some time i was sure i cracked the first part.

"Write a range function that takes two arguments, start and end, and returns an array containing all the numbers from start up to (and including) end."

I have looked at peoples answers but they all include the further parts of the question. I want to keep it simple, after all if i cant do the first part then there's no hope. it seems easy.

function range(start, end) {
  let array = [];

  for (let i = start; i <= end; i++){array.push(i);}
}

console.log(range(20 , 25));

but i get undefined, i have tried even copying and reducing the books answers to a similar situation.

It feels like my brain just cant do code. Where am i going wrong? Why is it undefined?

below is given answer

function range(start, end, step = start < end ? 1 : -1) {
  let array = [];

  if (step > 0) {
    for (let i = start; i <= end; i += step) array.push(i);
  } else {
    for (let i = start; i >= end; i += step) array.push(i);
  }
  return array;
}

function sum(array) {
  let total = 0;
  for (let value of array) {
    total += value;
  }
  return total;
}

console.log(range(1, 10))
// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(range(5, 2, -1));
// → [5, 4, 3, 2]
console.log(sum(range(1, 10)));
// → 55

thx guys

2
  • 1
    You're not returning the array at the end of your range function, resulting in calls to range always returning undefined. Commented May 28, 2018 at 11:20
  • return array just before closing your loop Commented May 28, 2018 at 11:23

3 Answers 3

4

You are not returning anything from range. Use return array; in range function:

function range(start, end) {
  let array = [];

  for (let i = start; i <= end; i++){array.push(i);}
  return array;
}

console.log(range(20 , 25));

Sign up to request clarification or add additional context in comments.

Comments

1

The variable array is declared with let which gives it block scope. It is declared inside the function range

function range(start, end) {
  let array = [];

  for (let i = start; i <= end; i++){array.push(i);}
}

and its duration expires when the function terminates. It does not exist anymore afterwards.

As others have already said, return array.

This way a reference is kept for the line where it is called. And if assigned to another reference there, the duration of the array will be extended until unreferenced. Then it goes to garbage collection.

Example:

let arr = range(5,10);

Comments

0

the code for eloquent java script can be like this:

function range(start,end,step){
  let myArr=[];
  if(step){
    for(let i=start;i<=end;i+=step){
    myArr.push(i);
  }
return myArr;
  }
  else{
  for(let i=start;i<=end;i++){
    myArr.push(i);
  }
  return myArr;}
}


function sum(arr){
  let total=0;
  for (let item of arr){
    total +=item;
  }
  return total;
}
sum(range(1,10,2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.