-1

So, I'm learning Javascript through a book and it has some exercises. One of the exercises asks for you to build two functions, one that creates an array from two numbers provided in the arguments, and the other function has to sum all the numbers in the array. Here's my code:

let beg = 1;
let end = 3;
array = [];
sumNum = 0;

function range(begg, endd) {
    for (let count = begg; count <= endd; count++) {
        array.push(count);
    }
    return array;
}

console.log(range(beg, end));

function sum(arrayy) {
    for (let i = 0; i <= arrayy.length - 1; i++) {
        sumNum = arrayy[i] + sumNum;
        console.log(sumNum);
        
    }
    console.log("\n")
    console.log(arrayy.length - 1);
    return sumNum / 2;
}

console.log(sum(range(beg, end)));

array2 = [1, 2, 3];
console.log("\n");
console.log(array2.length);

As I was solving the exercise I kept getting double the sum of all the numbers in the array. I started to print some information and discovered that my arrayy.length is returning double the value it's supposed to return and the loop runs double the times it should run.

Here's my output:

[ 1, 2, 3 ]
1
3
6
7
9
12


5
6


3

Sorry it this is a noob question, but my curiosity is killing me and I have not found anything on the internet, so why am I getting this result? Thanks in advance.

  • 1
    Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jan 31 '23 at 12:33
  • `array` variable is global, so each time you call the `range` function you keep appending for items to that shared array. – Iván Pérez Jan 31 '23 at 12:34
  • You call `range()` 2 times so the array has double the content. – takendarkk Jan 31 '23 at 12:35

1 Answers1

1

As Ivan said: The "array" variable is global, so each time you call the range function you keep appending items to that shared array. You should add the array inside your function and return it. Other than that you did a pretty nice job!

function range(begg, endd) {    
    let array = []
    for (let count = begg; count <= endd; count++) {
        array.push(count);
    }
    return array;
}

Also: The sum function should have the "sumnum" variable inside the function to prevent it from increasing every time you call the function:

function sum(arrayy) {
    let sumnum = 0;
    for (let i = 0; i <= arrayy.length - 1; i++) {
        sumNum = arrayy[i] + sumNum;
        console.log(sumNum);
    }
    console.log("\n");
    console.log(arrayy.length - 1);
    return sumNum / 2;
}

remove the array and sumnum variables from the top of your code to get rid of the global variables.

Hacky
  • 305
  • 4
  • 15