1

Apologies if this has been asked in other ways but my google-fu is lacking today. I just started learning Javascript today and I'm having an issue passing arrays into functions. I am trying to add the values of two arrays at each index, but sololearn's code playground is giving me an error when it tries to read the length of arr1 in the second function: "Uncaught TypeError: Cannot read property 'length' of undefined".

I have tried changing the iterator to "i <=6", but then it tells me it cannot read the property at index 0 of undefined at the "sum[i]..." line. I've also tried declaring the arrays with var instead of let.

Can anyone offer me insight as to why the array isn't passing into the function properly?

function generateStats(){
    let race = "halfOrc";
    const halfOrc = [2,1,0,0,0,0];
    let stats = [0,0,0,0,0,0];

    switch(race){
        case "halfOrc":
            stats = sumArray(stats,halfOrc);
            break;

    //more code
}

function sumArray(arr1,arr2){
        var sum = [];
        for (let i = 0; i <= arr1.length;) {
            sum[i] = arr1[i] + arr2[i];
            i++;
        return sum
        }
    }
4
  • 1
    array of length 2 for example has valid indices of 0 and 1 so, never use <= when comparing to array.length - your also returning in the for loop so only the first iteration will run Commented Jan 7, 2020 at 2:04
  • quick fix ... change i <= to i < and move return sum after } and move i++ to the for loop line ... Commented Jan 7, 2020 at 2:08
  • Thanks for the quick feedback. I just changed the <= to = and moved the return - the compiler is still giving me the type error at the arr1.length reference though Commented Jan 7, 2020 at 2:09
  • in that case you're doing something in real code that you haven't shown here - look at the answer ... it works with just the changes I mentioned Commented Jan 7, 2020 at 2:10

1 Answer 1

2

array of length 2 for example has valid indices of 0 and 1 so, never use <= when comparing to array.length -

you're also returning in the for loop so only the first iteration will run –

quick fix ... change i <= to i < and move return sum after } and move i++ to the for loop line .

Here's your code - it works now

function generateStats() {
  let race = "halfOrc";
  const halfOrc = [2, 1, 0, 0, 0, 0];
  let stats = [0, 0, 0, 0, 0, 0];

  switch (race) {
    case "halfOrc":
      stats = sumArray(stats, halfOrc);
      break;

      //more code
  }
  console.log(stats);
}

function sumArray(arr1, arr2) {
  var sum = [];
  for (let i = 0; i < arr1.length; ++i) {
    sum[i] = arr1[i] + arr2[i];
  }
  return sum
}
generateStats();

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

4 Comments

To put the indices issue in a different way, in JavaScript (and a lot of other languages), arrays are zero indexed. This means that the first element in an array is found at arr[0] and the last element is found at arr[length - 1]. When you use a <= comparison with an array in a loop, you're actually attempting to access a nonexistent element at the end of the array (remember, the last element is found at arr[length - 1] NOT arr[length]. That's the cause of the error - that nonexistent element is, by definition, undefined.
@MajorProductions - I'd suspect the OP knew that Arrays are zero indexed, since his code starts i at 0 :p
Comments aren't just for the person asking the question, but for other people who may have the same question. Moreover, a lot of (bad) tutorial sites just have "here, copy this" examples without providing any reasoning behind them. Cargo cult coding is definitely a thing.
I believe my confusion came from a misconception about how the iterator worked. I thought the sumArray function would stop at 4 if I just used "<". I just reread through the documentation and I see where my mixup was. It doesn't help that SoloLearn's code playground still won't log anything to console, but I see it runs fine on stack. Not sure why SoloLearn can't handle it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.