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());Creo que el método toUpperCase() devuelve un valor de cadena, no cambia el tuyo, por lo que debes hacer lo siguiente vegetables[i] = vegetables[i].toUpperCase();
let vegetables = ["cucumbers", "carrots", "tomatoes"]; let upperCase = function(){ return vegetables.map( vegetable => vegetable.toUpperCase()) } console.log(upperCase()); // [ 'CUCUMBERS', 'CARROTS', 'TOMATOES' ]// O TAMBIÉN PUEDES HACER LO MISMO CON ESTO
let vegetables = ["cucumbers", "carrots", "tomatoes"]; let upperCase = () => vegetables.map( vegetable => vegetable.toUpperCase()) console.log(upperCase());vegetables[i].toUpperCase() no reemplaza el valor de la matriz, upperCase no funciona "en su lugar"console.log(upperCase())upperCase(); console.log(vegetables)<= - debería ser < ya que las matrices están basadas en cero.Aquí está su versión FIJA
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());y aquí hay una versión para 2022
const upperCase = arr => arr.map(item => item && typeof item === 'string' ? item.toUpperCase() : item) let vegetables = ["cucumbers", "carrots", "tomatoes"]; console.log(upperCase(vegetables));Desglose
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, eg 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)