Codewars JavaScript Kata #2
This kata asks you to find the missing letter in an array of letters. For example if the array provided is ["M", "N", "P"], the output should equal "O".
This was tougher than I though. The way I chose to solve it, which may not be the most efficient is by creating an array of letters in the alphabet.
Then I would take the array provided as an argument and for each item in the array, check to see what the index is in the array of letters I made. Add 1 to that number and check to see if the next index in the argument array was equal to the next index in the letter array. If they were the same, do nothing. If they are different, output that letter because that's the missing one.
First pass worked on lower case letters, because that's my letters array was done in lowercase. I suppose another way to do it would be to do a hash, but I think an array would work better.
The way I fixed the problem is by checking first to see if the array contained lowercase or uppercase letters and then returning the value that I needed in the proper format based on which if statement it was true for.
Here's the Kata in question:
https://www.codewars.com/kata/5839edaa6754d6fec10000a2/train/javascript
Here's my code:
function findMissingLetter(array){
const letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
for(var i = 0; i< array.length; i++) {
firstIndex = letters.indexOf(array[i].toLowerCase())
\\first index gets the matching value from the letters array
if(array[0] === array[0].toUpperCase()){
if(letters[firstIndex+1].toUpperCase() !== array[i+1]){
return letters[firstIndex+1].toUpperCase()
\\if the values don't match, that means you've found the missing letter.
}
} else if(array[0] === array[0].toLowerCase()) {
if(letters[firstIndex+1] !== array[i+1])
return letters[firstIndex+1].toLowerCase()
\\if the values don't match, that means you've found the missing letter.
}
}
}
This was tougher than I though. The way I chose to solve it, which may not be the most efficient is by creating an array of letters in the alphabet.
Then I would take the array provided as an argument and for each item in the array, check to see what the index is in the array of letters I made. Add 1 to that number and check to see if the next index in the argument array was equal to the next index in the letter array. If they were the same, do nothing. If they are different, output that letter because that's the missing one.
First pass worked on lower case letters, because that's my letters array was done in lowercase. I suppose another way to do it would be to do a hash, but I think an array would work better.
The way I fixed the problem is by checking first to see if the array contained lowercase or uppercase letters and then returning the value that I needed in the proper format based on which if statement it was true for.
Here's the Kata in question:
https://www.codewars.com/kata/5839edaa6754d6fec10000a2/train/javascript
Here's my code:
function findMissingLetter(array){
const letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
for(var i = 0; i< array.length; i++) {
firstIndex = letters.indexOf(array[i].toLowerCase())
\\first index gets the matching value from the letters array
if(array[0] === array[0].toUpperCase()){
if(letters[firstIndex+1].toUpperCase() !== array[i+1]){
return letters[firstIndex+1].toUpperCase()
\\if the values don't match, that means you've found the missing letter.
}
} else if(array[0] === array[0].toLowerCase()) {
if(letters[firstIndex+1] !== array[i+1])
return letters[firstIndex+1].toLowerCase()
\\if the values don't match, that means you've found the missing letter.
}
}
}
Comments
Post a Comment