Home > Enterprise >  creating a function that converts an array to uppercase
creating a function that converts an array to uppercase

Time:03-20

so lets say I created an array that is ..

let foods = ['oranges', 'pears', 'peaches]

now I want to create a function that allows me to select any of the elements and convert it to uppercase letters how would I go about doing that?

CodePudding user response:

In javascript you could do something like:

let foods = ["oranges", "pears", "peaches"];

function uppercase(array, index) {
  for (i = 0; i < array.length; i  ) {
    if (i == index) {
      console.log(array[i]);
      result = array[i].toUpperCase();
      return result;
    }
  }
}

result = uppercase(foods, 1);
console.log(result);

CodePudding user response:

I hope this can help you:

example 1: convert to uppercase using index of element

let foods = ['oranges', 'pears', 'peaches']

const convertToUppercase = (index) => {
    return foods[index].toUpperCase()
}

console.log(convertToUppercase(0))

example 2: convert to uppercase by element (text you want to convert)

let foods = ["oranges", "pears", "peaches"];

const convertToUppercase = (element) => {
  return element.toUpperCase();
};

foods.forEach((element) => {
  console.log(convertToUppercase(element));
});

example 3: convert the entire array to uppercase

et foods = ["oranges", "pears", "peaches"];

const convertToUppercase = (array) => {
    let converted =[]
    array.map((element) =>
        converted.push((element.toUpperCase()))
    );

    return converted;
};

console.log(convertToUppercase(foods));

I this that will help, thanks

  • Related