skip to Main Content

Here I want to use array method ‘pop’, but i cant bcz num has a returned integer value, so how can i turn the vallue into an arry.

let num = 0;
for(i=0; i<=10; i++){
    num = i
  let r = num.pop()
  console.log(num, r)
}

I expecting —

num return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] & console [1, 2, 3, 4, 5, 6, 7, 8, 9] 10

2

Answers


  1. You can initialize array by num = [].

    And num.pop() will read and delete element.

    Here is a modified code

    let num = []; 
    for(i=1; i<=10; i++)
    {     
    num[i] = i   
    let r = num[num.length - 1];   
    console.log(num, r) 
    }
    
    Login or Signup to reply.
  2. .pop() is an array method and it won’t work here because your num variable is not an array.
    You can solve it by creating another variable of an empty array, and actually populate that one.
    Something like this
    let num_array = [];

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search