[Solved] Given an array of strings, convert each string into: uppercase if first letter is capital lower case if first letter is small

Introduction

This article will discuss how to convert an array of strings into either uppercase or lowercase depending on the first letter of each string. We will go through the steps of writing a function that takes an array of strings as an argument and returns an array of strings with the first letter of each string converted to either uppercase or lowercase. We will also discuss some of the potential issues that may arise when attempting to convert strings in this way.

Solution

function convertStrings(arr) {
let newArr = [];
for (let i = 0; i < arr.length; i++) { let firstLetter = arr[i][0]; if (firstLetter === firstLetter.toUpperCase()) { newArr.push(arr[i].toUpperCase()); } else { newArr.push(arr[i].toLowerCase()); } } return newArr; }


Given an array of strings, convert each string into: uppercase if first letter is capital lower case if first letter is small

solved Given an array of strings, convert each string into: uppercase if first letter is capital lower case if first letter is small


function convertStrings(arr) {
  let result = [];
  for (let i = 0; i < arr.length; i++) {
    let str = arr[i];
    if (str[0] === str[0].toUpperCase()) {
      result.push(str.toUpperCase());
    } else {
      result.push(str.toLowerCase());
    }
  }
  return result;
}