├── Easy ├── ABCheck.js ├── AdditivePersistence.js ├── AlphabetSoup.js ├── ArithGeo.js ├── ArrayAddiition.js ├── CheckNums.js ├── CountingMinutesI.js ├── DashInsert.js ├── DivisionStringified.js ├── ExOh.js ├── FirstFactorial.js ├── FirstReverse.js ├── LetterCapitalize.js ├── LetterChanges.js ├── LetterCountI.js ├── LongestWord.js ├── MeanMode.js ├── MultiplicativePersistence.js ├── NumberAddition.js ├── Offline_Minimum.js ├── Palindrome.js ├── PowersOfTwo.js ├── SecondGreatLow.js ├── SimpleAdding.js ├── SimpleSymbols.js ├── SwapCase.js ├── ThirdGreatest.js ├── TimeConvert.js ├── VowelCount.js └── WordCount.js ├── Medium ├── ArithGeoII.js ├── ArrayAddition.js ├── BinaryConverter.js ├── Division.js ├── LetterCount.js ├── PalindromeTwo.js ├── PrimeMover.js ├── PrimeTime.js ├── RunLength.js └── StringScramble.js └── README.md /Easy/ABCheck.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: AB Check (Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=AB%20Check 5 | //Returns true if string received contains 'a' and 'b' separated by exactly 3 places. 6 | function ABCheck(str) { 7 | //Using regex - 1 liner 8 | // return str.match(/a.{3}b/g)!==null; 9 | 10 | //Manual solve 11 | var abs = []; 12 | var pos = []; 13 | for (var i=0;i 2+7+1+2 = 12 --> 1+2 = 3. Therefore returns 2 (we added digits twice until we reached 3) 6 | var count = 0 7 | function AdditivePersistence(num) { 8 | if (String(num).length == 1) return count; 9 | return AdditivePersistence (eval(String(num).split("").join("+")),count++); 10 | } 11 | //Calls function, read line is input in text box on coderbyte 12 | AdditivePersistence(readline()); -------------------------------------------------------------------------------- /Easy/AlphabetSoup.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Alphabet Soup (Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=Alphabet%20Soup 5 | //Takes a string and outputs the string in alphabetical order 6 | function AlphabetSoup(str) { 7 | return str.split('').sort().join(''); 8 | } 9 | //Calls function, read line is input in text box on coderbyte 10 | AlphabetSoup(readline()); 11 | 12 | 13 | -------------------------------------------------------------------------------- /Easy/ArithGeo.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Palindrome (Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=Arith%20Geo 5 | //Takes an array of numbers, returns if the numbers form an arithmetic or geometric sequence (-1 if neither) 6 | function ArithGeo(arr) { 7 | //check if arithmetic 8 | if (arr.length > 1) { 9 | var arithmetic = true; 10 | var geometric = true; 11 | var diff = arr[1] - arr[0]; 12 | var ratio = arr[1]/arr[0]; 13 | var diff_2, ratio_2; 14 | for (var i =1;inum2?num1:num2>num1?num2:"-1"; 8 | } 9 | 10 | //Calls function, read line is input in text box on coderbyte 11 | CheckNums(readline()); 12 | -------------------------------------------------------------------------------- /Easy/CountingMinutesI.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Counting Minutes I (Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=Counting%20Minutes%20I 5 | //Takes two times in 12 hour format: 09:00am - 3:46pm and returns number of minutes passed between the two 6 | function CountingMinutesI(str) { 7 | var times = str.split('-'); 8 | var startHM = times[0].split(':'); 9 | var endHM = times [1].split(':'); 10 | var startHour = Number(startHM[0]); 11 | var endHour= Number(endHM[0]); 12 | var startMin = Number(startHM[1].substring(0,2)); 13 | var endMin = Number(endHM[1].substring(0,2)); 14 | //Convert hours to 24 hour format 15 | if (startHM[1].indexOf('pm') > -1 && startHM[0].indexOf('12')==-1 || startHM[1].indexOf('am')>-1 && startHM[0].indexOf('12')>-1) { 16 | startHour += 12; 17 | } 18 | if (endHM[1].indexOf('pm') > -1 && endHM[0].indexOf('12')==-1 || endHM[1].indexOf('am')>-1 && endHM[0].indexOf('12')>-1 ) { 19 | endHour += 12; 20 | } 21 | //Add hours to start hour until end hour is research 22 | var startHourMove = startHour; 23 | var count = 0; 24 | if (startHour == endHour) { 25 | if (startMin == endMin) return 0; 26 | else if (endMin > startMin) return endMin - startMin; 27 | } 28 | if (endHour == 1) endHour = 25; 29 | while (startHourMove!== endHour-1) { 30 | if (startHourMove==24) startHourMove = 1; 31 | else startHourMove++; 32 | count++; 33 | } 34 | return (count*60) + (60 - Number(startMin))+ Number(endMin); 35 | } 36 | //Calls function, read line is input in text box on coderbyte 37 | CountingMinutesI(readline()); 38 | 39 | -------------------------------------------------------------------------------- /Easy/DashInsert.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Dash Insert (Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=Dash%20Insert 5 | //Takes a string of digits, insert a dash between two odd numbers. 6 | function DashInsert(num) { 7 | var numStr = (num+"").split(""); 8 | for (var i=0;i-1;i--) { 12 | count++; 13 | if (count%3===0&&i!==0) strQA.splice(i,0,','); 14 | } 15 | return strQA.join(""); 16 | } 17 | 18 | //Calls function, read line is input in text box on coderbyte 19 | DivisionStringified(readline()); 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Easy/ExOh.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Ex Oh (Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=Ex%20Oh 5 | //Takes a string that contains x's and o's. Output true if equal amount of x's and o's. 6 | function ExOh(str) { 7 | var numx= str.match(/x/gi); 8 | var numy=str.match(/o/gi); 9 | if (numx!==null && numy!==null) return numx.length == numy.length; 10 | else return false; 11 | } 12 | //Calls function, read line is input in text box on coderbyte 13 | ExOh(readline()); 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Easy/FirstFactorial.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: FirstFactorial(Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=First%20Factorial 5 | 6 | //Calculates and returns factorial of a number 7 | function FirstFactorial(num) { 8 | var prod = 1; 9 | for (i=2;i<=num;i++) { 10 | prod = prod * i; 11 | } 12 | return prod; 13 | 14 | }; 15 | 16 | //Calls function, readline() reads in input from text box on coderbyte sandbox 17 | FirstFactorial(readline()); 18 | -------------------------------------------------------------------------------- /Easy/FirstReverse.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: FirstReverse(Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=First%20Reverse 5 | 6 | //This function simply receives a string and returns it reversed. 7 | function FirstReverse(str) { 8 | return str.split("").reverse().join(""); 9 | 10 | } 11 | 12 | //Calls function, read line is input in text box on coderbyte 13 | FirstReverse(readline()); 14 | -------------------------------------------------------------------------------- /Easy/LetterCapitalize.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Letter Capitalize(Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=Letter%20Capitalize 5 | //Capitalizes first letter of each word in a string 6 | function LetterCapitalize(str) { 7 | var words = str.split(" "); 8 | var newstr = ""; 9 | for (var i=0;i-1&&index<=alphabet.length-2) str2 += alphabet[index+1]; 16 | else if (index === alphabet.length-1) str2 += "a"; 17 | else str2 += str[i]; 18 | } 19 | str2= str2.replace(/[aeiou]/g, function (x) { return x.toUpperCase();}); 20 | return str2; 21 | } 22 | //Calls function, read line is input in text box on coderbyte 23 | LetterChanges(readline()); 24 | -------------------------------------------------------------------------------- /Easy/LetterCountI.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Letter Count I (Easy) 4 | //http://coderbyte.com/CodingArea/information.php?ct=Letter%20Count%20I 5 | //Takes a string an returns the leftmost (first) word that has any letter repeated the most 6 | function LetterCountI(str) { 7 | //This function receives a word string, and counts how many times each letter appears, and returns an object in the format of letter: number of times letter appears (key:value) 8 | function Word(str) { 9 | for (var i = 0; i highest) { 26 | highest = wObjs[j][letter]; 27 | highWordPos = j; 28 | } 29 | } 30 | } 31 | if (highest > 1) return words[highWordPos]; 32 | else return -1; 33 | 34 | } 35 | 36 | //Calls function, read line is input in text box on coderbyte 37 | LetterCountI(readline()); 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Easy/LongestWord.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Longest Word(Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=Longest%20Word 5 | 6 | //This function simply receives a sentence and returns its longest word. 7 | 8 | function LongestWord(sen) { 9 | 10 | var words = sen.split(" "); 11 | var longestLen = words[0].length; 12 | var longestWord = 0; 13 | for (i=1; ilongestLen) { 15 | longestLen = words[i].length; 16 | longestWord = i; 17 | } 18 | } 19 | return words[longestWord]; 20 | 21 | } 22 | //Calls function, read line is input in text box on coderbyte 23 | LongestWord(readline()); 24 | -------------------------------------------------------------------------------- /Easy/MeanMode.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: MeanMode (Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=Mean%20Mode 5 | //Takes a number array, calcs mean and mode - checks if they are equal. Returns 1 if true, 0 if false; 6 | 7 | function MeanMode(arr) { 8 | var sum = 0; 9 | var modes = []; 10 | for (var i = 0; imodes[modePos]) { 24 | modePos = i; 25 | } 26 | } 27 | var mode = arr[modePos]; 28 | if (mode === mean) { 29 | return 1; } 30 | else return 0; 31 | } 32 | 33 | //Calls function, read line is input in text box on coderbyte 34 | MeanMode(readline()); -------------------------------------------------------------------------------- /Easy/MultiplicativePersistence.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //http://www.coderbyte.com/CodingArea/information.php?ct=Multiplicative%20Persistence 4 | //Counts how many times we can multiply the digits in a number until single digit is reached 5 | //e.g. 2712 --> 2*7*1*2 = 28 --> 2*8 = 16 --> 1*6 = 6 Therefore returns 3 (we did three multiplications to get to a single digit 6 | var count = 0; 7 | function MultiplicativePersistence(num) { 8 | if (String(num).length == 1) return count; 9 | count++; 10 | return MultiplicativePersistence(eval(String(num).split("").join("*"))); 11 | } 12 | //Calls function, read line is input in text box on coderbyte 13 | MultiplicativePersistence(readline()); -------------------------------------------------------------------------------- /Easy/NumberAddition.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Number Addiction (Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=Number%20Addition 5 | //Takes a string, extracts the digits in it and returns the sum of them 6 | function NumberAddition(str) { 7 | var numbers = []; 8 | var currentNumber = ""; 9 | for (var i = 0; i1) { 10 | if (num%2 !== 0) return "false"; 11 | num /= 2; 12 | } 13 | return 'true'; 14 | } 15 | //Calls function, read line is input in text box on coderbyte 16 | PowersofTwo(readline()); 17 | -------------------------------------------------------------------------------- /Easy/SecondGreatLow.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: SecondGreatLow (Easy) 4 | //http://coderbyte.com/CodingArea/information.php?ct=Second%20GreatLow 5 | //Returns the second lowest and second higest number in an array 6 | function SecondGreatLow(arr) { 7 | //Sort lowest to highest 8 | var noDuplicates = arr.sort(function (a,b) {return a-b}); 9 | //Remove duplicates 10 | for (var i = 0;i1) return noDuplicates[1]+" "+noDuplicates[sorted.length-2] ; 16 | else return sorted[0] + " " + sorted[0]; 17 | } 18 | //Calls function, read line is input in text box on coderbyte 19 | SecondGreatLow(readline()); 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Easy/SimpleAdding.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Simple Adding(Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=Simple%20Adding 5 | 6 | //Finds the sum of all numbers from 1 to num 7 | function SimpleAdding(num) { 8 | //Alternate one-line solution: return num*(num+1)/2 9 | var sum = 0; 10 | for (var i=1;i<=num;i++) 11 | { 12 | sum += i; 13 | } 14 | return sum; 15 | 16 | } 17 | //Calls function, read line is input in text box on coderbyte 18 | SimpleAdding(readline()); 19 | -------------------------------------------------------------------------------- /Easy/SimpleSymbols.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Simple Symbols (Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=Simple%20Symbols 5 | //Check that each letter (a-z, case insensitive) is surrounded by '+' symbole i.e. +a+12+b+ 6 | function SimpleSymbols(str) { 7 | 8 | var len = str.length; 9 | if (len>0 && str[0].match(/[a-z]/i) === null && str[len-1].match(/[a-z]/i) === null) { 10 | for (var i = 1; i-1) count++; } 14 | return count; 15 | } 16 | //Calls function, read line is input in text box on coderbyte 17 | VowelCount(readline()); 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Easy/WordCount.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo 2014, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Word Count (Easy) 4 | //http://www.coderbyte.com/CodingArea/information.php?ct=Word%20Count 5 | //Takes a string (sentence) and outputs the number of words in it. 6 | function WordCount(str) { 7 | return str.split(' ').length; 8 | } 9 | //Calls function, read line is input in text box on coderbyte 10 | WordCount(readline()); 11 | 12 | 13 | -------------------------------------------------------------------------------- /Medium/ArithGeoII.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Arith Geo II(Medium) 4 | //http://coderbyte.com/CodingArea/information.php?ct=Arith%20Geo%20II 5 | //Determine if numbers in an array follow an arithmetic or geometric sequence 6 | function ArithGeoII(arr) { 7 | if (arr.length<2) return -1; 8 | var i=1, j=1; 9 | var diff = arr[1]-arr[0]; 10 | var ratio = arr[1]/arr[0]; 11 | while (i=0;i--) if (Number(str[i])) decimal+=Math.pow(2,str.length-1-i); 9 | return decimal+""; 10 | } 11 | //Calls function, read line is input in text box on coderbyte 12 | BinaryConverter(readline()); 13 | -------------------------------------------------------------------------------- /Medium/Division.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Division(Medium) 4 | //http://coderbyte.com/CodingArea/information.php?ct=Division 5 | //Takes two numbers and returns greatest common factor 6 | function Division(num1,num2) { 7 | var smallest = num2; 8 | if (num1 < num2) smallest = num1; 9 | for (var i=smallest;i>0;i--) if (num1%i===0 && num2%i===0) return i; 10 | } 11 | //Calls function, read line is input in text box on coderbyte 12 | Division(readline()); 13 | -------------------------------------------------------------------------------- /Medium/LetterCount.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: Letter Count(Medium) 4 | //http://coderbyte.com/CodingArea/information.php?ct=Letter%20Count 5 | //Takes a string (sentence) and find the word that contains highest repeating letter (in that word). 6 | function LetterCount(str) { 7 | var words = str.split(" "); 8 | var letters,letter_counts,regExp; 9 | var maxLetterCount=0, currentMaxLetterCount=0; 10 | //Loop through words 11 | for (var wordIndex=0;wordIndexmaxLetterCount) { 23 | maxLetterCount = currentMaxLetterCount; 24 | maxLetterWord = words[wordIndex]; 25 | } 26 | } 27 | return maxLetterCount>1?maxLetterWord:-1; 28 | } 29 | 30 | //Calls function, read line is input in text box on coderbyte 31 | LetterCount(readline()); 32 | -------------------------------------------------------------------------------- /Medium/PalindromeTwo.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: PalindromeTwo(Medium) 4 | //http://coderbyte.com/CodingArea/information.php?ct=Palindrome%20Two 5 | //Takes in a string and return true if it's a palindrome or false if not. Ignores punctuation or symbols! 6 | function PalindromeTwo(str) { 7 | var arr = str.toLowerCase().match(/[a-z,0-9]/gi); 8 | return arr.join("") === arr.reverse().join(""); } 9 | //Calls function, read line is input in text box on coderbyte 10 | PalindromeTwo(readline()); 11 | -------------------------------------------------------------------------------- /Medium/PrimeMover.js: -------------------------------------------------------------------------------- 1 | // Kimeshan Naidoo, www.kimeshan.com. 2 | //Coderbyte profile: http://www.coderbyte.com/CodingArea/Profile/?user=kimeshan 3 | //Solution for Coderbyte Challenge: PrimeMover(Medium) 4 | //http://coderbyte.com/CodingArea/information.php?ct=Prime%20Mover 5 | //Takes in parameter num (e.g. 5), and returns the num'th (e.g. 5th) prime number 6 | function PrimeMover(num) { 7 | //check if prime number 8 | var prime = true; 9 | var primes = [2]; 10 | for (var j=3;primes.length