Hola, no entiendo porque me muestra como mal el ejercicio si las pruebas que me dan si funcionan en mi codigo

Tell us what’s happening:

  **Your code so far**

function titleCase(str) {
let newStr = '';
let arr = str.toLowerCase().split(" ");
for(let chart in arr){
  newStr = `${newStr} ${arr[chart][0].toUpperCase() + arr[chart].slice(1)}`;
}
return newStr;
}

console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36 Edg/89.0.774.75.

Challenge: Haz que la primera letra de una palabra este en mayúscula

Link to the challenge:

Hi @danielveraangulo703 !

Welcome to the forum!

The problem is that you are adding an extra space at the beginning of the sentence.

Correct output:

Your output:

Can you spot the difference?
It’s subtle but that extra space in the beginning is preventing you from passing the test.

Hope that helps!

Hi @danielveraangulo703

Primero como dice @jwilkins.oboe tienes un typo hay un spacio, al principio del array, por eso no reconoce tu resultado pero eso no es todo. Quita esa variable

let newString= ' ';

Segundo no deberias hacer un for in loop porque estas haciendo un metodo split(" "); y estas cambiando de formato string a un array, y con arrays no puedes usar for in, solo en objetos.

La manera correcta es hacer un for loop para arrays.

for (let i = 0; i < arr.length; i++) {
    
  }

Despues ya solo regresa tu array a un string con el metodo.

yourArray[].join('' '');

Dejanos saber si tienes dudas en el for loop.

Translate.

Hi @danielveraangulo703

First, of all, as @jwilkins.oboe says you have a typo, there is a space, at the beginning of the array, so it doesn’t recognize your result, but that’s not all. Remove that variable

let newString = '';

Second, you shouldn’t do a ** for in ** loop because you’re doing a split (" "); method and you’re changing from string to array, and with arrays, you can’t use ** for in **, only in objects.

The correct way is to do a for loop for arrays.

 for (let i = 0; i <arr.length; i ++) {      

 }

Then just return your array to a string with the method.

yourArray []. join ('' '');

Let us know if you have questions about the for loop.

Hola @imendieta y @jwilkins.oboe ,

Muchas gracias por su respuesta, ya he corregido los puntos que me comentaron y ya funciono correctamente.

Me llamo mucho la atención sobre el tema del for in que solo se usa para los objetos, creí que el for in es como el foreach en PHP, respecto a los for loops el map y reduce igual se usan solamente para los objetos?

Nuevamente muchas gracias por sus respuestas, espero tener una respuesta sobre mi duda de los for loops.

I would suggest reading the documentation on for…in loops and arrays.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

But the gist of it is that there is no guarantee that a for…in loop will return the array indexes in a particular order.

So it is just something to be aware of when working with arrays.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.