How Can I Use Caps In My Function? (Javascript)

Viewed 180

I'm not entirely sure how to make this function work with capital letters. Currently, an array of all capital characters returns undefined when passed and processed in the "for loop".

This function takes an array of lowercase string only characters and returns the first missing character, but I also need it to work with caps.

I was able to solve this issue with around 10 extra lines, but It seems like there must be a better approach!

const alph = "abcdefghijklmnopqrstuvwxyz";
const findMissingLetter = (arr) => {
  if(arr.length===0)
    return -1;
  let start = alph.indexOf(arr[0]);
  for(let i=0; i<arr.length; i++) {
    if(arr[i] !== alph[start+i]) {
      return alph[start+i];
    }
  }
  return -1
}

console.log(findMissingLetter([]),"-1?"); //ok
console.log(findMissingLetter(['a']),"-1?"); //ok
console.log(findMissingLetter(['b']),"-1?"); //ok
console.log(findMissingLetter(['a','b']),"-1?"); //ok
console.log(findMissingLetter(['a','c']),"b?"); //ok
console.log(findMissingLetter(['b','c','e','f']),"d?"); //ok
console.log(findMissingLetter(['B','C','E','F']),"D?"); //returns undefined
//console.log(findMissingLetter(['B','c','E','f']),"D?"); //not necessary for this solution!

Hope you can help, Thank you!

2 Answers

Try this

let alph = "abcdefghijklmnopqrstuvwxyz";
alph += alph.toUpperCase();

You can use toLowerCase() and toUpperCase() to modify the casing of a string. So in your case, you need to:

  1. lowercase arr[0] when initializing your start variable, in case it's uppercase (since your alph constant is all lowercase letters)
  2. do all the same logic you already did, but just check against the lowercased version of each array item instead of the initial version
  3. return the result in either upper or lowercase based on the casing of the array item

const alph = "abcdefghijklmnopqrstuvwxyz";
const findMissingLetter = (arr) => {
  if(arr.length===0)
    return -1;
  let start = alph.indexOf(arr[0].toLowerCase()); // 1.
  for(let i=0; i<arr.length; i++) {
    const letter = arr[i].toLowerCase(); // 2.
    const isUpperCase = arr[i] !== letter;
    if(letter !== alph[start+i]) {
      const result = alph[start+i];
      return isUpperCase ? result.toUpperCase() : result; // 3.
    }
  }
  return -1
}

console.log(findMissingLetter([]),"-1?"); //ok
console.log(findMissingLetter(['a']),"-1?"); //ok
console.log(findMissingLetter(['b']),"-1?"); //ok
console.log(findMissingLetter(['a','b']),"-1?"); //ok
console.log(findMissingLetter(['a','c']),"b?"); //ok
console.log(findMissingLetter(['b','c','e','f']),"d?"); //ok
console.log(findMissingLetter(['B','C','E','F']),"D?"); //returns undefined
//console.log(findMissingLetter(['B','c','E','f']),"D?"); //not necessary for this solution!

The alternative is to just add all the uppercase letters to the alph constant and keep the function the same.

const alph = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Or

let alph = 'abcdefghijklmnopqrstuvwxyz'
alph += alph.toUpperCase();

The latter version just concatenates an uppercase version of alph to its initial value.

Related