Created
May 30, 2019 07:08
-
-
Save indianajone/03515460b7d200cbb62243b80aae3845 to your computer and use it in GitHub Desktop.
Code challenge: validate given string has contains all english alphabet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Test if a string uses all letters of the English alphabet. | |
// "The quick brown fox jumps over the lazy dog." | |
// Complete a function that tests if a string uses all letters of the alphabet. | |
const alphabet = [ | |
"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", | |
]; | |
// Works but too complex | |
function solution1(str) { | |
const letters = str.toLowerCase().split(''); | |
const uniqueLetter = Array.from(new Set(letters)).filter(i => alphabet.includes(i)).sort(); | |
return alphabet.join('') === uniqueLetter.join(''); | |
} | |
function solution2(str) { | |
return alphabet.every(i => str.toLowerCase().indexOf(i) > -1); | |
} | |
function testString(str) { | |
// Write your code here! | |
} | |
/** | |
Test code below: | |
*/ | |
const tests = [ | |
["This one should fail", false], | |
["The quick brown fox jumps over The lazy dog.", true], | |
["The five boxing üüü wizards jump quickly", true] | |
]; | |
const correct = tests.reduce((sum, testCase) => { | |
const [input, expectation] = testCase; | |
const actual = testString(input); | |
if (actual === expectation) { | |
return sum && true; | |
} | |
document.write( | |
`With input '${input}', I have expected: '${expectation}', got '${actual}'<br/>` | |
); | |
return false; | |
}, true); | |
if(correct){ | |
document.write('<b>Everything is correct, thank you</b>') | |
} else { | |
document.write('<b>Please see errors above</b>') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment