Created
November 28, 2017 16:54
-
-
Save rob137/48147392f37343d62ff31faff6e7462f to your computer and use it in GitHub Desktop.
Thinkful 2.2.3 JS drill answers
This file contains 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
// https://repl.it/@robertaxelkirby/shouter-drill | |
function shouter(whatToShout) { | |
whatToShoutInCaps = whatToShout.toUpperCase(); | |
return `${whatToShoutInCaps}!!!` | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testShouter() { | |
const whatToShout = 'fee figh foe fum'; | |
const expected = 'FEE FIGH FOE FUM!!!'; | |
if (shouter(whatToShout) === expected) { | |
console.log('SUCCESS: `shouter` is working'); | |
} else { | |
console.log('FAILURE: `shouter` is not working'); | |
} | |
} | |
testShouter(); |
This file contains 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
// https://repl.it/@robertaxelkirby/text-normalizer-drill | |
function textNormalizer(text) { | |
// This function ends with slicing the text and converting the result to lowercase. | |
// These two variables will be used to remove opening/closing whitespace in the final slice. | |
let startIndex = 0; | |
let endIndex = text.length-1; | |
// These two functions assign startIndex and endIndex the correct index of the string. | |
function findFirstChar(index) { | |
if (text[index] === " ") { | |
index++; | |
findFirstChar(index) | |
} else { | |
startIndex = index; | |
} | |
} | |
function findLastChar(index) { | |
if (text[index] === " ") { | |
index--; | |
findLastChar(index) | |
} else { | |
endIndex = index+1; | |
} | |
} | |
findFirstChar(startIndex); | |
findLastChar(endIndex); | |
// The two 'index' variables now allow us to slice the text. The text is then converted to | |
// lower case. | |
return text.slice(startIndex, endIndex).toLowerCase(); | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testTextNormalizer() { | |
const text = " let's GO SURFING NOW everyone is learning how "; | |
const expected = "let's go surfing now everyone is learning how"; | |
if (textNormalizer(text) === expected) { | |
console.log('SUCCESS: `textNormalizer` is working'); | |
} else { | |
console.log('FAILURE: `textNormalizer` is not working'); | |
} | |
} | |
testTextNormalizer(); |
This file contains 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
// https://repl.it/@robertaxelkirby/Wiseperson-generator-drill | |
function wisePerson(wiseType, whatToSay) { | |
return `A wise ${wiseType} once said: \"${whatToSay}\".` | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testWisePerson() { | |
const wiseType = 'goat'; | |
const whatToSay = 'hello world'; | |
const expected = 'A wise ' + wiseType + ' once said: "' + whatToSay + '".'; | |
const actual = wisePerson(wiseType, whatToSay); | |
if (expected === actual) { | |
console.log('SUCCESS: `wisePerson` is working'); | |
} else { | |
console.log('FAILURE: `wisePerson` is not working'); | |
} | |
} | |
testWisePerson(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment