Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
Can someone tell me what is wrong here? I want to return the vegetable array in CAPITAL letters

let vegetables = ["cucumbers", "carrots", "tomatoes"];
let upperCase = function() {
  for (let i = 0; i <= vegetables.length; i++) {
    vegetables[i].toUpperCase();
  }
  return vegetables[i];
};
console.log(upperCase());

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I think toUpperCase() method returns a string value, not changes yours, so you need to do like this vegetables[i] = vegetables[i].toUpperCase();

about 4 years ago · Juan Pablo Isaza Relatório

0

let vegetables = ["cucumbers", "carrots", "tomatoes"];

let upperCase = function(){
    return vegetables.map( vegetable => vegetable.toUpperCase())
}

console.log(upperCase()); // [ 'CUCUMBERS', 'CARROTS', 'TOMATOES' ]

// OR YOU CAN ALSO DO THE SAME WITH THIS

let vegetables = ["cucumbers", "carrots", "tomatoes"];

let upperCase = () => vegetables.map( vegetable => vegetable.toUpperCase())

console.log(upperCase());
about 4 years ago · Juan Pablo Isaza Relatório

0

  1. vegetables[i].toUpperCase() does not replace the array value, upperCase does not work "in place"
  2. You need to return the result if you choose to console.log(upperCase())
    instead of doing upperCase(); console.log(vegetables)
  3. You overrun the array with your <= - it should be < since arrays are zero based.

Here is your FIXED version

let vegetables = ["cucumbers", "carrots", "tomatoes"];
let upperCase = function() {
  for (let i = 0; i < vegetables.length; i++) {
    vegetables[i] = vegetables[i].toUpperCase();
  }
  return vegetables;
};
console.log(upperCase());

and here is a version for 2022

const upperCase = arr => arr.map(item => item && typeof item === 'string' ? item.toUpperCase() : item)
let vegetables = ["cucumbers", "carrots", "tomatoes"];


console.log(upperCase(vegetables));

Breakdown

const upperCase // name of method  
= arr // passing something we call arr  
=>    // arrow function - note we do not need "{ return ... }" if there is only one processing statement  
arr.map(   // return a map (array) of the passed array, e.g. do not modify the passed array   
item => // each element is processed as item  
item && typeof item === 'string' // if item is not falsy and is a string  
? item.toUpperCase() // return item uppercased  
: item) // else return the item (this construct is called a ternary)  
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda