├── .gitignore ├── 01firstreverse.js ├── 02firstfactorial.js ├── 03longestword.js ├── 04letterchanges.js ├── 05simpleadding.js ├── 06lettercapitalize.js ├── 07simplesymbols.js ├── 08checknums.js ├── 09timeconvert.js ├── 10alphabetsoup.js ├── 11abcheck.js ├── 12vowelcount.js ├── 13wordcount.js ├── 14exoh.js ├── 15palindrome.js ├── 16arithgeo.js ├── 17arrayadditioni.js ├── 18lettercounti.js ├── 19secondgreatlow.js ├── 20divisionstringified.js ├── 21countingminutesi.js ├── 22meanmode.js ├── 23dashinsert.js ├── 24swapcase.js ├── 25numberaddition.js ├── 26thirdgreatest.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /01firstreverse.js: -------------------------------------------------------------------------------- 1 | function FirstReverse(str) { 2 | return str.split("") //splits the string into an array of characters 3 | .reverse() //reverses the array 4 | .join(""); //joins the array back into a string, with no space between the characters 5 | } 6 | -------------------------------------------------------------------------------- /02firstfactorial.js: -------------------------------------------------------------------------------- 1 | //welcome to recursion! 2 | //recursion is simply calling a function from within itself to rerun the function 3 | //it's like a complex for loop a lot of the time, but is also super useful for binary searchs 4 | 5 | function FirstFactorial(num) { 6 | if( num === 1 ) { //recursion needs a base case, otherwise it will go on forever 7 | return 1; //if num is ever 1 we break out of the recursion 8 | } 9 | return num * FirstFactorial(num-1); //recursion magic! for factorials (num!) the answer is num * num-1 ... * 1 10 | } //so we need to call FirstFactorial on all the numbers below num and collect the results 11 | 12 | -------------------------------------------------------------------------------- /03longestword.js: -------------------------------------------------------------------------------- 1 | function LongestWord(sen) { 2 | var sentence = sen.split(" ") //split the sentence into an array of words 3 | .sort(function(a,b){ //sort the array with our own homebaked sort algorithm 4 | return b.replace(/[^a-zA-Z]/g, "").length - a.replace(/[^a-zA-Z]/g, "").length; //MAGIC!! *(see note below) 5 | }); 6 | return sentence.shift(); //return the first value (first largest value) from our sorted list 7 | } 8 | 9 | //NOTE ABOUT MAGIC: 10 | //our sorting algorithm compares each position against the position next to it 11 | //because we are only checking letters and not special characters, we use regex (for more study: http://regexone.com/) 12 | //to strip out the nonletters, we then compare lengths of each word to sort in a descending order (largest first)s -------------------------------------------------------------------------------- /04letterchanges.js: -------------------------------------------------------------------------------- 1 | function LetterChanges(str) { 2 | var vowels = "aeiou"; 3 | var alpha = "abcdefghijklmnopqrstuvwxyz"; 4 | var shifted = str.split(""); 5 | var str = ""; 6 | // code goes here 7 | for(var i = 0; i < shifted.length; i++) { 8 | if (shifted[i] == "z"){ 9 | str += "A"; 10 | } else if (alpha.indexOf(shifted[i].toLowerCase()) != -1) { 11 | if (vowels.indexOf(alpha[alpha.indexOf(shifted[i].toLowerCase()) + 1]) != -1){ 12 | str += alpha[alpha.indexOf(shifted[i].toLowerCase()) + 1].toUpperCase(); 13 | } else { 14 | str += alpha[alpha.indexOf(shifted[i].toLowerCase()) + 1]; 15 | } 16 | } else { 17 | str += shifted[i]; 18 | } 19 | } 20 | return str; 21 | 22 | } 23 | 24 | // keep this function call here 25 | // to see how to enter arguments in JavaScript scroll down 26 | print(LetterChanges(readline())); -------------------------------------------------------------------------------- /05simpleadding.js: -------------------------------------------------------------------------------- 1 | function SimpleAdding(num) { 2 | var sum = num; 3 | for (var i = 1; i < num; i++) { 4 | sum += i; 5 | } 6 | // code goes here 7 | return sum; 8 | 9 | } 10 | 11 | // keep this function call here 12 | // to see how to enter arguments in JavaScript scroll down 13 | print(SimpleAdding(readline())); -------------------------------------------------------------------------------- /06lettercapitalize.js: -------------------------------------------------------------------------------- 1 | function LetterCapitalize(str) { 2 | var words = str.split(" "); 3 | // code goes here 4 | for (var i = 0; i < words.length; i++) { 5 | var word = words[i].split(""); 6 | word[0] = word[0].toUpperCase(); 7 | words[i] = word.join(""); 8 | } 9 | return words.join(" "); 10 | 11 | } 12 | 13 | // keep this function call here 14 | // to see how to enter arguments in JavaScript scroll down 15 | print(LetterCapitalize(readline())); -------------------------------------------------------------------------------- /07simplesymbols.js: -------------------------------------------------------------------------------- 1 | function SimpleSymbols(str) { 2 | var strs = str.split(""); 3 | var letters = "abcdefghijklmnopqrstuvwxyz" 4 | for (var i = 0; i < strs.length; i++) { 5 | if (letters.indexOf(strs[i]) != -1 && (strs[i-1] != "+" || strs[i+1] != "+")) { 6 | return false; 7 | } 8 | } 9 | return true; 10 | 11 | } 12 | 13 | // keep this function call here 14 | // to see how to enter arguments in JavaScript scroll down 15 | print(SimpleSymbols(readline())); -------------------------------------------------------------------------------- /08checknums.js: -------------------------------------------------------------------------------- 1 | function CheckNums(num1,num2) { 2 | if (num1 == num2) { 3 | return "-1"; 4 | } else { 5 | return num2 > num1; 6 | } 7 | 8 | } 9 | 10 | // keep this function call here 11 | // to see how to enter arguments in JavaScript scroll down 12 | print(CheckNums(readline())); -------------------------------------------------------------------------------- /09timeconvert.js: -------------------------------------------------------------------------------- 1 | function TimeConvert(num) { 2 | var minutes = num % 60; 3 | var hours = parseInt(num/60); 4 | return "" + hours + ":" + minutes; 5 | 6 | } 7 | 8 | // keep this function call here 9 | // to see how to enter arguments in JavaScript scroll down 10 | print(TimeConvert(readline())); -------------------------------------------------------------------------------- /10alphabetsoup.js: -------------------------------------------------------------------------------- 1 | function AlphabetSoup(str) { 2 | var sort = str.split(""); 3 | // code goes here 4 | return sort.sort().join(""); 5 | 6 | } 7 | 8 | // keep this function call here 9 | // to see how to enter arguments in JavaScript scroll down 10 | print(AlphabetSoup(readline())); -------------------------------------------------------------------------------- /11abcheck.js: -------------------------------------------------------------------------------- 1 | function ABCheck(str) { 2 | // code goes here 3 | for(var i = 0; i < str.length; i++){ 4 | if (str[i] == "a") { 5 | if (str[i+4] == "b") { 6 | return true; 7 | } 8 | } 9 | } 10 | return false; 11 | } 12 | 13 | // keep this function call here 14 | // to see how to enter arguments in JavaScript scroll down 15 | print(ABCheck(readline())); -------------------------------------------------------------------------------- /12vowelcount.js: -------------------------------------------------------------------------------- 1 | function VowelCount(str) { 2 | var vowels = "aeiouAEIOU"; 3 | var count = 0; 4 | // code goes here 5 | for (var i = 0; i < str.length; i++) { 6 | if (vowels.indexOf(str[i]) != -1) { 7 | count += 1; 8 | } 9 | } 10 | return count; 11 | } 12 | 13 | // keep this function call here 14 | // to see how to enter arguments in JavaScript scroll down 15 | print(VowelCount(readline())); -------------------------------------------------------------------------------- /13wordcount.js: -------------------------------------------------------------------------------- 1 | function WordCount(str) { 2 | 3 | // code goes here 4 | return str.split(" ").length; 5 | 6 | } 7 | 8 | // keep this function call here 9 | // to see how to enter arguments in JavaScript scroll down 10 | print(WordCount(readline())); -------------------------------------------------------------------------------- /14exoh.js: -------------------------------------------------------------------------------- 1 | function ExOh(str) { 2 | var Xs = 0; 3 | var Os = 0; 4 | // code goes here 5 | for (var i = 0; i < str.length; i++) { 6 | if (str[i] == "x") { 7 | Xs++; 8 | } else if (str[i] == "o") { 9 | Os++; 10 | } 11 | } 12 | return Xs == Os; 13 | 14 | } 15 | 16 | // keep this function call here 17 | // to see how to enter arguments in JavaScript scroll down 18 | print(ExOh(readline())); -------------------------------------------------------------------------------- /15palindrome.js: -------------------------------------------------------------------------------- 1 | function Palindrome(str) { 2 | str = str.split(" ").join(""); 3 | // code goes here 4 | for(var i = 0; i < str.length/2; i++) { 5 | if (str[i] != str[str.length - 1 - i]) { 6 | return false; 7 | } 8 | } 9 | return true; 10 | 11 | } 12 | 13 | // keep this function call here 14 | // to see how to enter arguments in JavaScript scroll down 15 | print(Palindrome(readline())); 16 | -------------------------------------------------------------------------------- /16arithgeo.js: -------------------------------------------------------------------------------- 1 | function ArithGeo(arr) { 2 | if ((arr[1] - arr[0]) === (arr[arr.length-1] - arr[arr.length-2])) { 3 | return "Arithmetic"; 4 | } else if ((arr[1] / arr[0]) === (arr[arr.length-1] / arr[arr.length-2])) { 5 | return "Geometric"; 6 | } else { 7 | return "-1"; 8 | } 9 | } 10 | 11 | // keep this function call here 12 | // to see how to enter arguments in JavaScript scroll down 13 | print(ArithGeo(readline())); -------------------------------------------------------------------------------- /17arrayadditioni.js: -------------------------------------------------------------------------------- 1 | function ArrayAdditionI(arr) { 2 | arr.sort(function(a,b){return a - b}) 3 | var largest = arr.pop(); 4 | var sum = 0; 5 | for (var i = 0; i < arr.length; i++){ 6 | sum += arr[i]; 7 | for (var j = 0; j < arr.length; j++){ 8 | if (i != j) { 9 | sum += arr[j]; 10 | if (sum == largest) { 11 | return true; 12 | } 13 | } 14 | } 15 | for (var k = 0; k < arr.length; k++) { 16 | if (i != k) { 17 | sum -= arr[k]; 18 | if (sum == largest) { 19 | return true; 20 | } 21 | } 22 | } 23 | sum = 0; 24 | } 25 | // code goes here 26 | return false; 27 | 28 | } 29 | 30 | // keep this function call here 31 | // to see how to enter arguments in JavaScript scroll down 32 | print(ArrayAdditionI(readline())); -------------------------------------------------------------------------------- /18lettercounti.js: -------------------------------------------------------------------------------- 1 | function LetterCountI(str) { 2 | var words = str.split(" "); 3 | var count = 0; 4 | var word = ""; 5 | // code goes here 6 | for (var i = 0; i < words.length; i++) { 7 | var wordx = words[i]; 8 | var sum = 0; 9 | for (var j = 0; j < words[i].length; j++) { 10 | var letter = wordx[j]; 11 | for (var k = 0; k < wordx.length; k++) { 12 | if ((j != k) && (letter === wordx[k])){ 13 | sum += 1; 14 | } 15 | } 16 | } 17 | if (sum > count) { 18 | count = sum; 19 | word = wordx; 20 | } 21 | } 22 | if (count > 0) { 23 | return word; 24 | } else{ 25 | return "-1"; 26 | } 27 | } 28 | 29 | // keep this function call here 30 | // to see how to enter arguments in JavaScript scroll down 31 | print(LetterCountI(readline())); -------------------------------------------------------------------------------- /19secondgreatlow.js: -------------------------------------------------------------------------------- 1 | function SecondGreatLow(arr) { 2 | var unique = [arr[0]]; 3 | for(var i = 1; i < arr.length; i++) { 4 | if (unique.indexOf(arr[i]) == -1) { 5 | unique.push(arr[i]); 6 | } 7 | } 8 | unique.sort(function(a,b){return a - b}); 9 | var smallest = unique[1].toString(); 10 | unique.reverse(); 11 | var largest = unique[1].toString(); 12 | // code goes here 13 | return smallest + " " + largest; 14 | 15 | } 16 | 17 | // keep this function call here 18 | // to see how to enter arguments in JavaScript scroll down 19 | print(SecondGreatLow(readline())); -------------------------------------------------------------------------------- /20divisionstringified.js: -------------------------------------------------------------------------------- 1 | function DivisionStringified(num1,num2) { 2 | var number = parseInt(num1/num2); 3 | var thousands = parseInt(number/1000); 4 | var hundreds = number % 1000; 5 | // code goes here 6 | if (thousands > 0) { 7 | return thousands.toString() + "," + hundreds.toString(); 8 | } else { 9 | return hundreds.toString(); 10 | } 11 | 12 | } 13 | 14 | // keep this function call here 15 | // to see how to enter arguments in JavaScript scroll down 16 | print(DivisionStringified(readline())); -------------------------------------------------------------------------------- /21countingminutesi.js: -------------------------------------------------------------------------------- 1 | function CountingMinutesI(str) { 2 | var times = str.split("-"); 3 | var time1 = times[0].slice(0,times[0].length-2).split(":"); 4 | var time1ap = times[0][times[0].length-2]; 5 | var time2 = times[1].slice(0,times[1].length-2).split(":"); 6 | var time2ap = times[1][times[1].length-2]; 7 | var time1min = parseInt(time1[0]) * 60 + parseInt(time1[1]); 8 | var time2min = parseInt(time2[0]) * 60 + parseInt(time2[1]); 9 | 10 | if (time1ap === "p" && time1[0] !== "12") { 11 | time1min += 12 * 60; 12 | } 13 | if (time2ap === "p" && time2[0] !== "12") { 14 | time2min += 12 * 60; 15 | } 16 | 17 | if (time1ap === "a" && time1[0] === "12") { 18 | time1min -= (12 * 60); 19 | } 20 | if (time2ap === "a" && time2[0] === "12") { 21 | time2min -= (12 * 60); 22 | } 23 | 24 | if (time1min > time2min) { 25 | return ((24 * 60) - time1min) + time2min; 26 | } else { 27 | return time2min - time1min; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /22meanmode.js: -------------------------------------------------------------------------------- 1 | function MeanMode(arr) { 2 | var modeCount = {}; //object to hold numbers and their counts 3 | var mode; //creating a variable for future mode 4 | var mean; //creating a variable for future mean 5 | var sum = 0; //initialzing a sum count 6 | 7 | for ( var i = 0; i < arr.length; i++ ) { //cycling through the array of numbers 8 | sum += arr[i]; //adding numbers together for the full sum 9 | modeCount[arr[i]] = modeCount[arr[i] || 0; //initialzing a key value pair or using the current one if we've seen the number before 10 | modeCount[arr[i]] += 1; //adding for the count of the number 11 | } 12 | mean = sum/arr.length; //calculating the mean value 13 | for( var key in modeCount ){ //looping through the modeCount 14 | if( modeCount[key] > count){ //check the number of times a number has been seen 15 | mode = parseInt(key); //if its a new highest, convert the key back into a number and set it as the current mode 16 | count = modeCount[key]; //set a new high count 17 | } 18 | } 19 | return mode === mean; //check to see if the mode and mean are equal 20 | } -------------------------------------------------------------------------------- /23dashinsert.js: -------------------------------------------------------------------------------- 1 | function DashInsert(num) { 2 | var num = num.toString() //convert the number to a string 3 | .split(""); //split the string into an array of characters 4 | var result = []; //create an empty array to hold our modified values 5 | for(var i = 0; i < num.length; i++) { //loop through our number array 6 | result.push(num[i]); //add each number to our result array 7 | if(num[i] % 2 !== 0 && num[i+1] % 2 === 1){ //check to see if the current number and then next number are odd 8 | result.push("-"); //push a dash if they are both odd so there is a dash between the two 9 | } 10 | } 11 | return result.join(""); //join the result array together into a string and return it 12 | } -------------------------------------------------------------------------------- /24swapcase.js: -------------------------------------------------------------------------------- 1 | function SwapCase(str) { 2 | var result = []; //our results array to hold transformed values 3 | for(var i = 0; i < str.length; i++) { //for loop to look through the whole string 4 | if(str[i] === str[i].toUpperCase()){ //check to see if the character is uppercase 5 | result.push(str[i].toLowerCase()); //if so, make it lowercase and add it to results array 6 | } else if(str[i] === str[i].toLowerCase()){ //check to see if the character is lowercase 7 | result.push(str[i].toUpperCase()); //if so, make it upperrcase and add it to results array 8 | } else { 9 | result.push(str[i]); //default case for nonalpha characters 10 | } 11 | } 12 | return result.join(""); //join the array back into a string and return it 13 | } -------------------------------------------------------------------------------- /25numberaddition.js: -------------------------------------------------------------------------------- 1 | function NumberAddition(str) { 2 | var numbers = str.replace(/[^0-9]/g, " ") //replace everything but letters with spaces 3 | .split(" "); //split numbers into an array of numbers by the spaces we inserted 4 | var sum = 0; //a starting point to sum the numbers 5 | 6 | for( var i = 0; i < numbers.length; i++){ //simple for loop through all the number values 7 | numbers[i] && (sum += parseInt(numbers[i])); //short hand check that numbers[i] exists, then convert strings to number values 8 | } 9 | return sum; 10 | } 11 | -------------------------------------------------------------------------------- /26thirdgreatest.js: -------------------------------------------------------------------------------- 1 | function ThirdGreatest(strArr) { 2 | strArr.sort(function(a,b){ //sort the array with our own homebaked sort algorithm that sorts from largest to smallest 3 | return b.replace(/[^a-zA-Z]/g, "").length - a.replace(/[^a-zA-Z]/g, "").length; //MAGIC!! *(see note from #3 LongestWord) 4 | }); 5 | return strArr[2]; //returns the third value in the array, which is the 3rd largest when we sorted in decending order above 6 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Coderbyte Challenges 2 | =================== 3 | 4 | In order, in JavaScript, currently just running through the easy ones. Not worrying about refactoring, mostly just worried about completion and speed. 5 | 6 | 11/30 - Refactoring now for the fun of it! I'm in Hack Reactor which was the main reason I did all of these in the first place so now I feel like I have an opportunity to make them awesome and with comment explanations for newbies. 7 | --------------------------------------------------------------------------------