I want to take the following array and turn it into a sequence of ##### using array method. Note: I'm using a prototype method inside a class.
this.secretWord = ['e', 'l', 'e', 'p', 'h', 'a', 'n', 't']
Below is my code:
getSecretWordPuzzle () {
let newArr = [];
return this.secretWord.map((elem) => {
let finalStr = newArr.push('#')
console.log(finalStr)
})
}
My output is:
[ undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined ]
My desired output is: '########'
What am I doing wrong?
You should return a value, not push to an array; map returns a new array:
const secretWord = ['e', 'l', 'e', 'p', 'h', 'a', 'n', 't']
const getSecretWordPuzzle = () => secretWord.map(_ => '#')
console.log(getSecretWordPuzzle())
Or, use a foreach and return the array, using your approach:
const secretWord = ['e', 'l', 'e', 'p', 'h', 'a', 'n', 't']
function getSecretWordPuzzle() {
let newArr = [];
secretWord.forEach(() => newArr.push('#'))
return newArr
}
console.log(getSecretWordPuzzle())