The problem is that you are running .push(...)
and this is used to add a new item, if the purpose is to update the existing items just access and apply .toUpperCase()
directly, there is also no need to create a new array
, unless you intend to do a more complex filtering, but so far it doesn’t seem the case, so update directly like this:
palavras[i] = palavras[i].toUpperCase();
Example:
function transformaParaMaiusculo(palavras)
{
for (let i = 0, j = palavras.length; i < j; i++) {
palavras[i] = palavras[i].toUpperCase();
}
return palavras;
}
console.log(transformaParaMaiusculo(["arroz", "blusa", "lápis"]));
Note that I put the palavras.length
in the variable j
, for your code there is no performance gain, but if it was a giant array you may feel a small improvement (it is not so big, it is a small improvement only)
Really .map()
here too will not have advantages, nor disadvantage, of course an advantage in the future would apply a context if it is a class/object applied to the callback (function):
arr.map(callback[, thisArg])
So in the this
would have access to the class context, of course writing .map()
and much smaller than a for()
, but if you are interested in controlling the Dice or know which one is with a basic will already solve (you can even do inside the .map()
, but there involves understanding well the context of the variables, none is better or worse than the other, each one will serve you better one time.