0

So, I have this here function designed to split the array elements and return them to two separate arrays. Problem is, when I try yo run it it returns "Challenges.js:32 Uncaught TypeError: Cannot read properties of undefined (reading 'split')" I don't really get it, what am I missing here? Sorry for the noob question, been studying for less than two weeks :D

const array0 = [
    "3:1",
    ...
    "4:0",
  ];
let array1 = [];
let array2 = [];

for (let i = 0; i <= array0.length; i++) {
    array1.push(array0[i].split(":")[0]);
    array2.push(array0[i].split(":")[1]);
  }
  
  console.log(array1, array2);

3 Answers 3

4

The condition of for loop should be i < array0.length instead of i <= array0.length

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

Comments

2

your for loop is wrong, it should be

for (let i = 0; i < array0.length; i++) {

}

and not

for (let i = 0; i <= array0.length; i++) {

}

Comments

0

You need to change that <= to < or add -1 after array0.length:

for (let i = 0; i < array0.length; i++) {
    array1.push(array0[i].split(":")[0]);
    array2.push(array0[i].split(":")[1]);
  }

both ok

for (let i = 0; i < array0.length -1; i++) {
    array1.push(array0[i].split(":")[0]);
    array2.push(array0[i].split(":")[1]);
  }

1 Comment

Removed the = and we good :D best of the day to you mate!

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.