0

The goal was to get the first letters of the items in the array to be capitalized but it's printing out undefined

const words = ["planes", "trains", "automobiles"];
const singularWords = words.map(w => w.slice(0, -1))
// The below line should console.log: ["plane", "train", "automobile"]
console.log(singularWords);

// Bonus:
const capitalizedWords = words.map((w) => {
  for (let i = 0; i < words.length; i++) {
    words[i] = words[i].charAt(0).toUpperCase();
  }
});
// The below line should console.log: ["Planes", "Trains", "Automobiles"]
console.log(capitalizedWords);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Mozarts
  • 51
  • 1
  • 1
  • 6
  • 1
    You are not returning anything from `.map`. You are mutating `words` instead. Try `words.map((word) => ( word[0].toUpperCase() + word.substr(1) ))` – Rajesh May 19 '20 at 06:29
  • Als why map words and then loop too? – mplungjan May 19 '20 at 06:30
  • 1
    beautiful, thank you so much!, I'm still having problems understanding this but hopefully, I'll get better – Mozarts May 19 '20 at 06:35

4 Answers4

3

You map AND loop

Just map, and use slice again

PS: a discussion of w[0] vs w.charAt(0)

const words = ["planes", "trains", "automobiles"];
const singularWords = words.map(w => w.slice(0, -1))
// The below line should console.log: ["plane", "train", "automobile"]
console.log(singularWords);

// Bonus:
const capitalizedWords = words.map(w => w.charAt(0).toUpperCase()+w.slice(1));
// The below line should console.log: ["Planes", "Trains", "Automobiles"]
console.log(capitalizedWords);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

This is more of an alternate approach signifying use of desctructuring pattern.

Idea

  • In the arrow function, use a destructuring pattern for first argument
  • Have 2 variables:
    • firstChar: holding the first character and this will be used for upperCase.
    • rest: The rest of characters. However, since we are using spread operator, it'll be an array and we will have to use .join('') for it.

const words = ["planes", "trains", "automobiles"];
const singularWords = words.map(([ firstChar ]) => firstChar )
// The below line should console.log: ["plane", "train", "automobile"]
console.log(singularWords);

// Bonus:
const capitalizedWords = words.map(( [ firstChar, ...rest ] ) => firstChar.toUpperCase() + rest.join('') );
// The below line should console.log: ["Planes", "Trains", "Automobiles"]
console.log(capitalizedWords);

If you wish this formatting for representation, using CSS would be better:

const words = ["planes", "trains", "automobiles"];
const div = document.querySelector('.content');
words.forEach((word) => {
  const p = document.createElement('p');
  p.classList.add('title-case');
  p.innerText = word;
  div.append(p);
})
.title-case {
  text-transform: capitalize;
}
<div class='content'></div>
Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

The function - map() is basically looping for you.

const words = ["planes", "trains", "automobiles"];
const singularWords = words.map(w => w.slice(0,-1))
// The below line should console.log: ["plane", "train", "automobile"]
console.log(singularWords);

// Bonus:
const capitalizedWords = words.map((w) => {
  return w.charAt(0).toUpperCase() + w.slice(1);
});
// The below line should console.log: ["Planes", "Trains", "Automobiles"]
console.log(capitalizedWords);

Here is the correct code.

Hope this helps!

zenwraight
  • 2,002
  • 1
  • 10
  • 14
0
const words = ["planes", "trains", "automobiles"];
const singularWords = words.map(w => w.slice(0, -1))
// The below line should console.log: ["plane", "train", "automobile"]
console.log(singularWords);

// Bonus:
const capitalizedWords = words.map(w => w[0].toUpperCase()+w.slice(1));
// The below line should console.log: ["Planes", "Trains", "Automobiles"]
console.log(capitalizedWords);

Use only map . You can dele charAt , and add w[0]

Nikita
  • 121
  • 6