-2

Is there a non backwards way to turn exactly one array index into an uppercase string? I don't want to split, loop or anything else, because I only need this one string.

colors = [ "red", "green", "blue" ];
red = colors[0];
redUpper = red.toUppercase();

This won't work, because toUppercase() is a string function. I know that. I'm only looking for the simplest way to achieve this.

Erazihel
  • 7,295
  • 6
  • 30
  • 53
so43459
  • 9
  • 1
  • 1
  • 1

4 Answers4

10

If you only want the first element upper case, you can use the String#toUpperCase function:

const colors = [ "red", "green", "blue" ];
const redUpper = colors[0].toUpperCase();
console.log(redUpper);

If you want them all, use Array#Map.

const colors = [ "red", "green", "blue" ];
console.log(colors.map(a => a.toUpperCase()));
Erazihel
  • 7,295
  • 6
  • 30
  • 53
3

You can do it using

const colors = [ "red", "green", "blue" ];
console.log(colors.map(function(x){
    return x.toUpperCase();
}));

If you want to change the first element to uppercase then use

const colors = [ "red", "green", "blue" ];
colors[0] = colors[0].toUpperCase();
console.log(colors);
marvel308
  • 10,288
  • 1
  • 21
  • 32
2

you need to assign your value to the array element you want to change

var arr = [ "red", "green", "blue" ];
arr[0] = arr[0].toUpperCase()
console.log(arr)
Littlee
  • 3,791
  • 6
  • 29
  • 61
1

colors = [ "red", "green", "blue" ];
console.log(colors[0].toUpperCase())

You have a typo error in your code: toUppercase != toUpperCase

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Krzysztof Raciniewski
  • 4,735
  • 3
  • 21
  • 42