-
-
Save dasdachs/131c90897be2a228fcb68d1daa852d32 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/xoqajidofo
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/* | |
* The Daily Programmer: Condensing Sentences #319 | |
* =============================================== | |
* https://www.reddit.com/r/dailyprogrammer/comments/6grwny/20170612_challenge_319_easy_condensing_sentences/ | |
* | |
* My solution to the coding challenge presented by Cody | |
* and posted on FreeCodeCamp's Youtube channel with | |
* comments. | |
* [Video]: https://www.youtube.com/watch?v=bK0o-8GMRss | |
* | |
* Learn to code for free and get a developer job: https://www.freecodecamp.com | |
*/ | |
/* Create a function named condenseSentence. | |
* You can name you functions whatever you | |
* like as long as it is a valid JS name. | |
* (https://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names) | |
* | |
* The function takes one argument, a sentence | |
* and return a string. | |
*/ | |
var condenseSentence = function(sentence){ | |
/* First make an array by spliting the | |
* sentence where spaces occur (" "). | |
*/ | |
var words = sentence.split(" "); | |
/* Now the tricky part, the reduce method. | |
* | |
* Once you have an array you can reduce the | |
* elements with a function. | |
* | |
* What reduce does is it iterates through the | |
* array, allowing you to take the current or | |
* first element and the next element, apply any | |
* function on them and pass the result to the | |
* next element. | |
* | |
* Since Array.prototype.reduce() does not change | |
* the array, we pass the result to our return | |
* statememnt. We could've also bound the value to | |
* a variable and returned the variable. | |
* | |
* More on reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce | |
*/ | |
return words.reduce(function(left, right) { | |
// Left is the current word | |
// Right is the next word | |
for(var i = 0; i < left.length; i++) { | |
/* We iterate over the current word | |
* and create a substring, geting shorter | |
* with every iteration. | |
* | |
* Say we have a string "freeCodeCamp". | |
* The results would be: | |
* > FreeCodeCamp | |
* > reeCodeCamp | |
* > eeCodeCamp | |
* > eCodeCamp | |
* > CodeCamp | |
* > odeCamp | |
* > deCamp | |
* > eCamp | |
* > Camp | |
* > amp | |
* > mp | |
* > p | |
*/ | |
var subString = left.substring(i); | |
if(right.startsWith(subString)) { | |
/* If the beginign of the next word | |
* matches our substring we concatinate | |
* the first word with the remainder | |
* of the second word, minus the overlaping | |
* substring | |
*/ | |
return left + right.replace(subString, ""); | |
} | |
} | |
/* If no substring matches the second word | |
* we return both words as a string, seperated | |
*/ | |
return left + " " + right; | |
}) | |
}; | |
// Testing some example cases | |
console.log(condenseSentence("Hello world!")); | |
console.log(condenseSentence("I heard the pastor sing live verses easily.")); | |
console.log(condenseSentence("Deep episodes of Deep Space Nine came on the television only after the news.")); | |
console.log(condenseSentence("Digital alarm clocks scare area children.")); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/* | |
* The Daily Programmer: Condensing Sentences #319 | |
* =============================================== | |
* https://www.reddit.com/r/dailyprogrammer/comments/6grwny/20170612_challenge_319_easy_condensing_sentences/ | |
* | |
* My solution to the coding challenge presented by Cody | |
* and posted on FreeCodeCamp's Youtube channel with | |
* comments. | |
* [Video]: https://www.youtube.com/watch?v=bK0o-8GMRss | |
* | |
* Learn to code for free and get a developer job: https://www.freecodecamp.com | |
*/ | |
/* Create a function named condenseSentence. | |
* You can name you functions whatever you | |
* like as long as it is a valid JS name. | |
* (https://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names) | |
* | |
* The function takes one argument, a sentence | |
* and return a string. | |
*/ | |
var condenseSentence = function(sentence){ | |
/* First make an array by spliting the | |
* sentence where spaces occur (" "). | |
*/ | |
var words = sentence.split(" "); | |
/* Now the tricky part, the reduce method. | |
* | |
* Once you have an array you can reduce the | |
* elements with a function. | |
* | |
* What reduce does is it iterates through the | |
* array, allowing you to take the current or | |
* first element and the next element, apply any | |
* function on them and pass the result to the | |
* next element. | |
* | |
* Since Array.prototype.reduce() does not change | |
* the array, we pass the result to our return | |
* statememnt. We could've also bound the value to | |
* a variable and returned the variable. | |
* | |
* More on reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce | |
*/ | |
return words.reduce(function(left, right) { | |
// Left is the current word | |
// Right is the next word | |
for(var i = 0; i < left.length; i++) { | |
/* We iterate over the current word | |
* and create a substring, geting shorter | |
* with every iteration. | |
* | |
* Say we have a string "freeCodeCamp". | |
* The results would be: | |
* > FreeCodeCamp | |
* > reeCodeCamp | |
* > eeCodeCamp | |
* > eCodeCamp | |
* > CodeCamp | |
* > odeCamp | |
* > deCamp | |
* > eCamp | |
* > Camp | |
* > amp | |
* > mp | |
* > p | |
*/ | |
var subString = left.substring(i); | |
if(right.startsWith(subString)) { | |
/* If the beginign of the next word | |
* matches our substring we concatinate | |
* the first word with the remainder | |
* of the second word, minus the overlaping | |
* substring | |
*/ | |
return left + right.replace(subString, ""); | |
} | |
} | |
/* If no substring matches the second word | |
* we return both words as a string, seperated | |
*/ | |
return left + " " + right; | |
}) | |
}; | |
// Testing some example cases | |
console.log(condenseSentence("Hello world!")); | |
console.log(condenseSentence("I heard the pastor sing live verses easily.")); | |
console.log(condenseSentence("Deep episodes of Deep Space Nine came on the television only after the news.")); | |
console.log(condenseSentence("Digital alarm clocks scare area children."));</script></body> | |
</html> |
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
/* | |
* The Daily Programmer: Condensing Sentences #319 | |
* =============================================== | |
* https://www.reddit.com/r/dailyprogrammer/comments/6grwny/20170612_challenge_319_easy_condensing_sentences/ | |
* | |
* My solution to the coding challenge presented by Cody | |
* and posted on FreeCodeCamp's Youtube channel with | |
* comments. | |
* [Video]: https://www.youtube.com/watch?v=bK0o-8GMRss | |
* | |
* Learn to code for free and get a developer job: https://www.freecodecamp.com | |
*/ | |
/* Create a function named condenseSentence. | |
* You can name you functions whatever you | |
* like as long as it is a valid JS name. | |
* (https://stackoverflow.com/questions/1661197/what-characters-are-valid-for-javascript-variable-names) | |
* | |
* The function takes one argument, a sentence | |
* and return a string. | |
*/ | |
var condenseSentence = function(sentence){ | |
/* First make an array by spliting the | |
* sentence where spaces occur (" "). | |
*/ | |
var words = sentence.split(" "); | |
/* Now the tricky part, the reduce method. | |
* | |
* Once you have an array you can reduce the | |
* elements with a function. | |
* | |
* What reduce does is it iterates through the | |
* array, allowing you to take the current or | |
* first element and the next element, apply any | |
* function on them and pass the result to the | |
* next element. | |
* | |
* Since Array.prototype.reduce() does not change | |
* the array, we pass the result to our return | |
* statememnt. We could've also bound the value to | |
* a variable and returned the variable. | |
* | |
* More on reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce | |
*/ | |
return words.reduce(function(left, right) { | |
// Left is the current word | |
// Right is the next word | |
for(var i = 0; i < left.length; i++) { | |
/* We iterate over the current word | |
* and create a substring, geting shorter | |
* with every iteration. | |
* | |
* Say we have a string "freeCodeCamp". | |
* The results would be: | |
* > FreeCodeCamp | |
* > reeCodeCamp | |
* > eeCodeCamp | |
* > eCodeCamp | |
* > CodeCamp | |
* > odeCamp | |
* > deCamp | |
* > eCamp | |
* > Camp | |
* > amp | |
* > mp | |
* > p | |
*/ | |
var subString = left.substring(i); | |
if(right.startsWith(subString)) { | |
/* If the beginign of the next word | |
* matches our substring we concatinate | |
* the first word with the remainder | |
* of the second word, minus the overlaping | |
* substring | |
*/ | |
return left + right.replace(subString, ""); | |
} | |
} | |
/* If no substring matches the second word | |
* we return both words as a string, seperated | |
*/ | |
return left + " " + right; | |
}) | |
}; | |
// Testing some example cases | |
console.log(condenseSentence("Hello world!")); | |
console.log(condenseSentence("I heard the pastor sing live verses easily.")); | |
console.log(condenseSentence("Deep episodes of Deep Space Nine came on the television only after the news.")); | |
console.log(condenseSentence("Digital alarm clocks scare area children.")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment