0

I'm stuck with this problem where I need to eliminate a letter from each of the array values and change the first letter if it is in index 0. My current approach is this:

function capital(arr, char){
    let str = "";
    let result = "";
for (let i = 0; i < arr.length; i++){
    str = arr[i] + (i < arr.length - 1 ? ",": "");;

    for (let j = 0; j < str.length; j++){
        if (str[j] === char){
            result += "";
             if (str[j] === char){
            result += (j === 0? "A": "");
        }
        
        else {
            result += str[j];
        }
    }
}
    console.log(result);
}
capital(["doritos","sandking","bandana", "demand"], "d");

The program should eliminate each occurrence of letter d found in the strings and change the index 0 to letter A if the d is in index 0.

The current result is

Aoritos,sanking,banana,Aeman

but it should look like

Aritos,sanking,banana,Aman

The requirement does not allow any use of built in functions, and the actual program requires the letters to be case insensitive, but I can work on it by substituting codes and adding couple if elses, I just need help with the changing the index 0 condition. Any help would be appreciated, thank you!

1
  • your example seems off to me, please correct it. Commented Aug 6, 2021 at 5:58

3 Answers 3

1

You can check if your input char is at first index str.indexOf(char) then simply add prefix "A" to your string without first char i.e str.substring(1)

function capital(arr, char) {
  let str = "";
  let result = "";
  for (let i = 0; i < arr.length; i++) {
    str = arr[i];
    if(str.indexOf(char) === 0) {
      result = 'A' + str.substring(1);
    }else {
      result = str;
    }

    console.log(result);
  }
}
capital(["doritos", "sandking", "bandana", "demand"], "d");

Sign up to request clarification or add additional context in comments.

Comments

0

this logic may help you

function capital(arr, char) {

  return arr.map(e => {

    let isFirst = false;
    if (e[0] == "d") isFirst = true;
    e = e.replace(new RegExp(char, 'ig'), '');
    if (isFirst)
      e = e.replace(e.charAt(0), "A");
    return e;
  });

}
console.log(capital(["doritos", "sandking", "bandana", "demand"], 'd'))

Comments

0

You can iterate through each letter of your word and update the first letter if it is matched with passed letter and at 0 index, for other index if it doesn't match with passed letter add to your result.

const capitalize = ch => {
 const letter = ch.charCodeAt(0);
 if(letter >= 97 && letter <= 122) {
    return String.fromCharCode(letter - 32);
 }
 return ch;
}

const capital = (words, ch) => {
  let result = '';
  for(let i = 0; i < words.length; i++) {
    let newWord = '';
    for(let j = 0; j < words[i].length; j++) {
      if(capitalize(words[i][j]) === capitalize(ch) && j === 0) {
        newWord = 'A';
      }
      if(capitalize(words[i][j]) !== capitalize(ch)) {
        newWord += words[i][j];
      }
    }
    result += newWord;
    if(i < words.length - 1 ) {
      result += ',';
    }
  }
  return result;
}

const result = capital(["doritos","sandking","bandana", "demand", "sandDking"], "d");
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.