+ 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
6 Antworten
+ 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.
+ 4
Hoang Anh check this code and see if I got it correctly
https://code.sololearn.com/WiyZVkLqk2TO/?ref=app
+ 4
thank you guys so much
+ 3
You can replace the loop with:
wordStore.map( function(item){
studentName.push(item[0])
})
But why?
+ 3
i just updated my code. I want to get the last element in wordStore. please help me!
+ 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