Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!