javascript - Appending a variable to an array without changing it -


i'm working on pre-bootcamp course , haven't been able find online in order solve problem has me bit confused.

i understand when use javascript have push(), pop(), shift() , unshift() methods can use in order add/remove array. problem i'm running test question ask:

  1) arrays appendkitten(name) appends kitten kittens array , returns new array,      leaving kittens array unchanged:  

i've been confused day in solving problem. when want change array we'll use 1 of methods. example when append it'll be:

var kittens = ["milo", "otis", "garfield"]  function destructivelyappendkitten(name){  kittens.push(name)  return kittens } 

which push new kitten(name) array. in order solve previous test i've written:

function appendkitten(name){  var newarray = []  var kittens = kittens.concat(newarray);  kittens.push(name)  return kittens } 

but continue receive same error. there i'm missing?

my thought process in solving question:

1- calling function names appendkittens single argument -> name.

2- pushing data kitten variable local kitten instance in order have same array.

3- push whatever name passes in parameter new array.

4- return new array had push() performed leaving global kitten untouched.

is thinking process off? ideas i'm off? help. thank you, appreciate it.

function appendkitten(name){   var newarray = kittens.slice();   // or es6 way   // var newarray = [...kittens];   newarray.push(name)   return newarray } 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -