5
let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog']; let secretMessage = animals.map(function(animal) { for(animal = 0; animal <= animals.length-1; animal++) { return animals[animal].charAt(animal); } }); console.log(secretMessage.join('')); 

Hi, thru this piece of code i want to output the string HelloWorld, which is formed by the first characters of each string/element in the animals array. However, the output is instead HHHHHHHHHH. I don't know if the for loop is the problem here?

Could someone please tell me why the code produces such an output and how i can modify it in order for the desired result to be returned successfully?

I'm just a novice for now & that's why your help will play a tremendous part in my growth as a programmer. Thanks in advance!

2

10 Answers 10

10

Array.prototype.map() is a for loop in and of itself.

Try:

// Animals. const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'] // Secret Message. const secretMessage = animals.map((animal) => animal[0]).join('') // Log. console.log(secretMessage) // "HelloWorld"

Or alternatively:

// Animals. const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'] // Secret Message. const secretMessage = animals.reduce((accumulator, animal) => accumulator + animal[0], '') // Log. console.log(secretMessage) // "HelloWorld"

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

1 Comment

Plus 1 for using .reduce() in this context -- expanded my use case scenarios.
4

The map method does the for loop for you. So all you need in the function is animal[0]

let secretMessage = animals.map(function(animal) { return animal[0] }); 

This will return every first character in every string in the array, animals. Everything else you have is right.

Comments

1

You don't need inner loop:

let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog']; let secretMessage = animals.map(function(animal) { return animal.charAt(0); // or simply animal[0] }); console.log(secretMessage.join(''));

Comments

0

The error here is that you have a loop inside the map function, which already loops the array.

The correct code is

animals.map(x=>x[0]).join(''); //or animals.map(function(animal){ return animal[0]; //First letter of word }).join(''); //Array -> String 

Comments

0

This should be the code:

let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog']; let secretMessage = animals.map(function(animal) { return animal.charAt(0); }); console.log(secretMessage.join('')); 

Comments

0

Just do this.

let animals = [ 'Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog' ]; console.log(animals.map(e => e[0]).join(""));

1 Comment

For ES5 or less use this: animals.map(function(s) { return s[0]; }).join(""); For ES6: () => { }
0
let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog']; let secretMessage = animals.map(function(animals) { return animals[0]; }); console.log(secretMessage.join('')); 

or

let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog']; let secretMessage = animals.map(animals => animals[0]); console.log(secretMessage.join('')); 

Comments

0

.map() array function take callback function as an argument and return new array so that can use this method to get our result see the syntax below:

const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog']; const secretMessage = animals.map(animals => { return animals[0]; }) console.log(secretMessage.join('')); 

now when .map method is applied on the animals array its return only the first element of the each string because we use animals[0] which read only first element of each string so our output is HelloWorld

Comments

0

const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog']; const secretMessage = animals.map(animal => { return animal[0]; }) console.log(secretMessage.join(''));

Comments

0

Try this solution : ---

const user = 'Anisul Haque Bhuiyan'; const username = user.toLowerCase().split(' ').map((elem) => elem[0]).join(''); console.log(username); 

Output : 'ahb'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.