How to capitalize all words in an array using toUpper?

Asked

Viewed 876 times

3

The function needs to return all words of the array in uppercase.

function transformaParaMaiusculo(palavras){

   for (let i=0; i<palavras.lengh; i++){
      palavras.push(palavras[i].toUpperCase())
   }
   return palavras
}

console.log(transformaParaMaiusculo(["arroz", "blusa", "lápis"])

Once executed the program returns without being capitalized.

4 answers

3

If you want to make a copy of it, you can use the map, example:

var min = ["arroz", "blusa", "lápis"];
var mai = min.map(p => p.toUpperCase());

console.log(mai);

The function map will scroll through each element of your array and return a new array from how you transformed the elements into the internal of the map

2

You must create a new array and return it, it’s doing almost right, only it’s not creating the array, You’re trying to do it on your own, it doesn’t make sense. using a function itself mounted the logic since it seems an exercise that tests just that (there are functions ready, but it does not seem to be what you want):

function transformaParaMaiusculo(palavras) {
    var novo = [];
    for(var i = 0; i < palavras.length; i++) novo.push(palavras[i].toUpperCase());
    return novo;
}

console.log(transformaParaMaiusculo(["arroz", "blusa", "lápis"])) 

I put in the Github for future reference.

Why don’t I do it another way?

The most important thing is that being an algorithm exercise it’s important to do it in the most basic way possible, the way the exercise probably wants, it’s not the way someone would do it with more advanced knowledge

Also because even giving to do more abstractly, the performance of the abstract is not good and this can make a difference in some cases, even if for the specific case is secondary, but it is good to warn because many people read these things and do not understand that there are reasons to do each way.

You could use the variable itself without using the array new, but that’s not what’s on the path that exercise was doing, and it’s probably not what you want because it would change the content of a variable that is passed to that function.

0

if you want to transform the array instead of generating a new one you can try this here:

palavras.forEach(palavra => palavra = palavra.toUpperCase());

if you want to generate a new array you can use it here:

const maiusculas = palavras.map(palavra => palavra.toUpperCase());

I hope I’ve helped!

0

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.