Here is my code, but it does not work.
function makeAbbr(words) {
var text = '';
words.split(' ');
for (i = 0; i < words.length; i++) {
text += words[i].substr(0, 1)
}
return text
}
console.log(makeAbbr('java script'))
The following should work:
function makeAbbr(someStr) {
const words = someStr.split(" ");
let abbr = "";
for (const word of words) {
abbr += word.substring(0,1);
}
return abbr;
}
makeAbbr("I love JavaScript!"); // IlJ
.split(...) does not mutate the string (and convert it to an array), but returns the array. Therefore, you have to assign the result to a variable.
/**
* @param {string} words
*
* @returns {string}
*/
function makeAbbr(words) {
let wor = words.split(" ");
let abbreviation = "";
for (const word of wor) {
abbreviation += word[0].substring();;
}
return abbreviation.toUpperCase();
}