+ 3

How can I use .map() or .filter() method with this code? Anyone??

let name = prompt("name: "); let allWord = name.split(" "); let wordStore = allWord.filter(word => word !== ""); let emailSchool = "2174@sinhvien.hoasen.edu.vn"; let studentName = []; // i want to use .map() with code below. studentName.push(wordStore[wordStore.length-1]+"."); for(let i = 0; i < wordStore.length-1; i++){ studentName.push(wordStore[i][0]); } console.log(studentName.join("") + emailSchool); ex: nguyen hoang anh -> output: anh.nh2174@sinhvien.hoasen.edu.vn

17th Jun 2018, 9:07 AM
Hoang Anh
Hoang Anh - avatar
6 odpowiedzi
+ 4
If wordStore is an array, then you can do let studentName = wordStore.map( function(n){ return n[0]; }) then studentName[0] += "."; It would be better if you provide a full code so we can see the whole context of it.
17th Jun 2018, 9:36 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 4
Hoang Anh check this code and see if I got it correctly https://code.sololearn.com/WiyZVkLqk2TO/?ref=app
17th Jun 2018, 11:02 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 4
thank you guys so much
17th Jun 2018, 11:16 AM
Hoang Anh
Hoang Anh - avatar
+ 3
You can replace the loop with: wordStore.map( function(item){ studentName.push(item[0]) }) But why?
17th Jun 2018, 9:35 AM
KrOW
KrOW - avatar
+ 3
i just updated my code. I want to get the last element in wordStore. please help me!
17th Jun 2018, 9:56 AM
Hoang Anh
Hoang Anh - avatar
+ 3
let wordsCount= wordStore.length let lastWord= wordStore[wordsCount-1] let firstLetters= '' for(let i=0; i<wordCount-2; ++i){ firstLetters+= wordStore[i][0] } let finalEmail= lastWord+"."+firstLetters+emailSchool P.S. I presumed that your final email will be formed with last word, an dot and first char of first n-1 words. If its not in this format, please, explain better
17th Jun 2018, 10:57 AM
KrOW
KrOW - avatar