skip to Main Content

We have to write a function that returns the given array switching every word in uppercase format. No loops are allowed (would make things much easier).

This is what I came up with so far but only half of the output seems to capitalize and I don’t really understand why. this is my code

function capitalize(word) {
  let newArr = word.map(word => {
    return word.toUpperCase();
  });
  return newArr;
}

this is another iteration I tried

function capitalize(word) {
  let newArr = word.map(el => {
    return el.toUpperCase();
  });
}

Here is the code we are given to test and display the function :

// Here is some code to test and display your function output:
// (you must modify it to test out your code but /! do not deliver it /! ). Only deliver your function
const result = ["CHANGE", "this", "array", "BY", "ThE", "RESULT", "oF", "youR", "FUNCTION", "TO", "test", "IT", "OUT"]
displayResult(result)

Once executed the words 1, 4, 6, 9, 10, 12 & 13 are correct but not the rest.

When I put the functions I created in Python Tutor it seems to work fine so I’m having a hard time understanding where the error is

Any and all suggestions are welcomed, thank you!

2

Answers


  1. "… We have to write a function that returns the given array switching every word in uppercase format. No loops are allowed (would make things much easier). …"

    Just return the given array after using the map method.

    function f(x) {
        return x.map(x => x.toUpperCase())
    }
    

    Here is an example.

    function f(x) {
        return x.map(x => x.toUpperCase())
    }
    var result = ["CHANGE", "this", "array", "BY", "ThE", "RESULT", "oF", "youR", "FUNCTION", "TO", "test", "IT", "OUT"]
    console.log(f(result))
    Login or Signup to reply.
  2. Seems you should transform the elements in-place, use forEach():

    const arr = ["CHANGE", "this", "array", "BY", "ThE", "RESULT", "oF", "youR", "FUNCTION", "TO", "test", "IT", "OUT"]
    
    arr.forEach((item, i, arr) => arr[i] = item.toUpperCase());
    
    console.log(arr);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search