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
// Bonfire: Truncate a string | |
// Author: @serhiilihus | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-truncate-a-string?solution=function%20truncate(str%2C%20num)%20%7B%0A%20%20%2F%2F%20Clear%20out%20that%20junk%20in%20your%20trunk%0A%20%20if%20(num%20%3E%3D%20str.length%20)%20%7B%0A%20%20%20%20return%20str%3B%0A%20%20%7D%20else%20if%20(%20num%20%3E%203)%20%7B%0A%20%20%20%20str%20%3D%20str.substr(0%2Cnum%20-%203)%20%2B%20%22...%22%3B%0A%20%20%7D%20else%20%7B%0A%20%20%20%20str%20%3D%20str.substr(0%2Cnum)%20%2B%20%22...%22%3B%0A%20%20%7D%0A%20%20return%20str%3B%0A%7D%0A%0Atruncate(%22A-tisket%20a-tasket%20A%20green%20and%20yellow%20basket%22%2C%2011)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function truncate(str, num) { | |
if (num >= str.length ) { | |
return str; | |
} else if ( num > 3) { | |
str = str.substr(0,num - 3) + "..."; | |
} else { |
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
// Bonfire: Repeat a string repeat a string | |
// Author: @serhiilihus | |
// http://www.freecodecamp.com/challenges/bonfire-repeat-a-string-repeat-a-string?solution=function%20repeat%28str%2C%20num%29%20{%0A%20%20var%20newString%20%3D%20%22%22%3B%0A%20%20if%20%28%20num%20%3E%200%20%29%20{%0A%20%20for%20%28var%20i%20%3D%200%3B%20i%20%3C%20num%20%3B%20i%2B%2B%20%29%0A%20%20%20%20%20%20newString%20%2B%3D%20str%3B%0A%20%20return%20newString%3B%0A%20%20}%20else%20return%20%22%22%3B%0A}%0A%0Arepeat%28%22abc%22%2C%203%29%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function repeat(str, num) { | |
var newString = ""; | |
if ( num > 0 ) { | |
for (var i = 0; i < num ; i++ ) | |
newString += str; | |
return newString; |
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
// Bonfire: Confirm the Ending | |
// Author: @serhiilihus | |
// http://www.freecodecamp.com/challenges/bonfire-confirm-the-ending?solution=function%20end(str%2C%20target)%20%7B%0A%20%20return%20(str.substr(str.length%20-%20target.length%2C%20str.length)%20%3D%3D%3D%20target%20)%3F%20true%20%3A%20false%20%3B%0A%7D%0A%0Aend(%22Bastian%22%2C%20%22n%22)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function end(str, target) { | |
return (str.substr(str.length - target.length, str.length) === target )? true : false ; | |
} |
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
// Bonfire: Return Largest Numbers in Arrays | |
// Author: @serhiilihus | |
// http://www.freecodecamp.com/challenges/bonfire-return-largest-numbers-in-arrays#?solution=function%20largestOfFour(arr)%20%7B%0A%20%20var%20newArr%20%3D%20%5B%5D%3B%0A%20%20var%20max%3B%0A%20%20for%20(%20var%20i%20%3D%200%20%3B%20i%20%3C%20arr.length%20%3B%20i%2B%2B%20)%7B%0A%20%20%20%20max%20%3D%200%3B%0A%20%20%20%20for%20(%20var%20j%20%3D%200%20%3B%20j%20%3C%20arr.length%20%3B%20j%2B%2B)%0A%20%20%20%20%20%20if%20(arr%5Bi%5D%5Bj%5D%20%3E%20max)%20max%20%3D%20arr%5Bi%5D%5Bj%5D%3B%0A%20%20%20%20newArr.push(max)%3B%0A%20%20%7D%0A%20%20return%20newArr%3B%0A%7D%0A%0AlargestOfFour(%5B%5B4%2C%205%2C%201%2C%203%5D%2C%20%5B13%2C%2027%2C%2018%2C%2026%5D%2C%20%5B32%2C%2035%2C%2037%2C%2039%5D%2C%20%5B1000%2C%201001%2C%20857%2C%201%5D%5D)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function largestOfFour(arr) { | |
var newArr = []; | |
var max; | |
for ( var i = 0 ; i < arr.length ; i++ ){ | |
max = 0; | |
for ( var j = 0 ; j < arr.leng |
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
// Bonfire: Title Case a Sentence | |
// Author: @serhiilihus | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-title-case-a-sentence?solution=function%20titleCase(str)%20%7B%0A%20str%3Dstr.toLowerCase()%3B%0A%20var%20newString%20%3D%20str.charAt(0).toUpperCase()%3B%0A%20for(var%20i%20%3D%201%20%3B%20i%20%3C%20str.length%20%3B%20i%2B%2B%20)%0A%20%20%20%20%20if%20(%20str.charAt(i-1)%20%3D%3D%20%22%20%22)%20%0A%20%20%20%20%20%20%20%20%20newString%20%2B%3D%20%20%20%20%20str.charAt(i).toUpperCase()%3B%0A%20%20%20%20else%20%0A%20%20%20%20%20%20%20%20newString%20%2B%3D%20str.charAt(i)%3B%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20return%20newString%3B%0A%7D%0AtitleCase(%22I%27m%20a%20little%20tea%20pot%22)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function titleCase(str) { | |
str=str.toLowerCase(); | |
var newString = str.charAt(0).toUpperCase(); | |
for(var i = 1 ; i < str.length ; i++ ) | |
if ( str.charAt(i-1) == " ") |
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
// Bonfire: Find the Longest Word in a String separated with spaces(only) | |
// Author: @serhiilihus | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-find-the-longest-word-in-a-string?solution=function%20findLongestWord(str)%20%7B%0A%20%20str%20%2B%3D%20%22%20%22%3B%0A%20%20var%20maxLength%20%3D%200%3B%0A%20%20while%20(%20str.indexOf(%22%20%22)%20%3E%200%20)%20%7B%0A%20%20%20%20%20%0A%20%20%20%20%20%20str%20%3D%20str.substr(str.indexOf(%22%20%22)%2B1%2Cstr.length)%3B%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20if%20(%20str.indexOf(%22%20%22)%20%3E%20maxLength%20)%0A%20%20%20%20%20%20%20%20%20%20maxLength%20%3D%20str.indexOf(%22%20%22)%3B%0A%20%20%7D%20%20%0A%20%20return%20maxLength%3B%0A%7D%0A%0AfindLongestWord(%22The%20quick%20brown%20fox%20jumped%20over%20the%20lazy%20dog%22)%3B%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function findLongestWord(str) { | |
str += " "; | |
var maxLength = 0; | |
while ( str.indexOf(" ") > 0 ) { | |
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
// Bonfire: Check for Palindromes | |
// Author: @serhiilihus | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-check-for-palindromes?solution=function%20palindrome(str)%20%7B%0A%20%20var%20flag%20%3D%20true%3B%0A%20%20str%20%3D%20str.toLowerCase().replace(%2F%5B)(%3A%5C%5C%2F%5Cs.%2C_-%5D%2Fg%2C%27%27)%3B%0A%20%20for%20(var%20i%20%3D%200%20%3B%20%20i%20%3C%20Math.floor(str.length%20%2F%202)%20%3B%20i%2B%2B%20)%0A%20%20if%20(%20str.charAt(i)%20%3D%3D%3D%20str.charAt(%20str.length%20-%20i%20-%201%20)%20)%7B%0A%20%20%20%20%20%20flag%20%3D%20true%3B%0A%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20flag%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20break%3B%0A%20%20%7D%0A%20return%20flag%3B%0A%7D%0A%0A | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function palindrome(str) { | |
var flag = true; | |
str = str.toLowerCase().replace(/[)(:\\/\s.,_-]/g,''); | |
for (var i = 0 ; i < Math.floor(str.length / 2) ; i++ ) | |
if ( str.charAt(i) === str.charAt( str.length - i - 1 ) ){ |
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
// Bonfire: Reverse a String | |
// Author: @serhiilihus | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-reverse-a-string?solution=function%20reverseString(str)%20%7B%0A%20%20return%20str.split(%22%22).reverse().join(%22%22).toString()%3B%0A%7D%0A%0AreverseString(%22hello%22)%3B%0AreverseString(%22Howdy%22)%3B%0AreverseString(%22Greetings%20from%20Earth%22)%3B | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function reverseString(str) { | |
return str.split("").reverse().join("").toString(); | |
} |
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
// Bonfire: Factorialize a Number | |
// Author: @serhiilihus | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-factorialize-a-number | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function factorialize(num) { | |
return ( num === 1 || num === 0 ) ? 1 : num * factorialize(num - 1); | |
} |
NewerOlder