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

88
Views
Function only returning value at zero index

So, I am learning functions and trying to create a function which takes an array as a parameter.

function printArray(arr){
    for (let i = 0; i < arr.length; i++){
        let upperCased = arr[i].toUpperCase();
        return upperCased;
    };
}
const testArray = ['o', 'p'];
printArray(testArray);

However, it returns only the first value 'O'

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

A return statement only gets executed once, then you exit the function.

about 4 years ago · Juan Pablo Isaza Report

0

You need to update the element on each iteration and, once the loop is complete, return the updated array.

function printArray(arr) {
  for (let i = 0; i < arr.length; i++) {
    arr[i] = arr[i].toUpperCase();
  }
  return arr;
}

const testArray = ['o', 'p', 'Bob from accounting'];

console.log(printArray(testArray));

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!