1

I have the following js code.

When I call the arrHelper function with the string "martin" I have noticed that my console.log is only showing the first three characters i.e ["m", "a", "r"] but not the full ["m", "a", "r","t","i","n"]. This happens even when I change the argument to a different name I am not getting the full converted array. This does not happen if I don't use the function Array.isArray().

let arrHelper = (str) => {
  let splittedStr = Array.isArray(str) ? str : str.split('')
  console.log(splittedStr)
  return (
    splittedStr.map((v, i, a) => {
      return (
        a.splice(-1)
      )
    })
  )
}

console.log(arrHelper("martin"));
isherwood
  • 58,414
  • 16
  • 114
  • 157
  • `Array.prototype.splice` mutates the array, and console is often [lazy about evaluating](https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays). – ASDFGerte Oct 14 '21 at 15:22
  • 1
    can you elaborate on what are you trying to do here – Pranshu Dubey Oct 14 '21 at 15:24

0 Answers0