├── AB Check ├── Additive Persistence ├── Alphabet Soup ├── Arith Geo ├── Array Addition I ├── Check Nums ├── Counting Minutes I ├── Dash Insert ├── Division Stringified ├── Ex Oh ├── First Factorial ├── First Reverse ├── Letter Capitalize ├── Letter Changes ├── Letter Count I ├── Longest Word ├── MeanMode ├── Multiplicative Persistence ├── Number Addition ├── Off Line Minimium ├── Palindrome ├── Powers Of Two ├── README.md ├── Second Great Low ├── Simple Adding ├── Simple Symbols ├── Swap Case ├── Third Greatest ├── Time Convert ├── Vowel Count └── Word Count /AB Check: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * AB Check * 6 | * Using the JavaScript language, have the function ABCheck(str) take the str * 7 | * parameter being passed and return the string true if the characters a and b are * 8 | * separated by exactly 3 places anywhere in the string at least once * 9 | * (ie. "lane borrowed" would result in true because there is exactly three characters * 10 | * between a and b). Otherwise return the string false. * 11 | * * 12 | * SOLUTION * 13 | * I am goint to use a RegExp to see if the patter [a...b] exists anywhere in the * 14 | * string. If it does then return true else return false. * 15 | * * 16 | * Steps for solution * 17 | * 1) Use RegExp pattern to search string for pattern a...b * 18 | * 2) If found return true * 19 | * 3) Else return false * 20 | * * 21 | ***************************************************************************************/ 22 | function ABCheck(str) { 23 | 24 | var match = str.search(/a...b/); 25 | if (match > -1) { 26 | return "true"; 27 | } 28 | else { 29 | return "false"; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Additive Persistence: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Additive Persistence * 6 | * Using the JavaScript language, have the function AdditivePersistence(num) take the * 7 | * num parameter being passed which will always be a positive integer and return its * 8 | * additive persistence which is the number of times you must add the digits in num * 9 | * until you reach a single digit. For example: if num is 2718 then your program * 10 | * should return 2 because 2 + 7 + 1 + 8 = 18 and 1 + 8 = 9 and you stop at 9. * * 11 | * * 12 | * SOLUTION * 13 | * The challenge passes a string but it expects us to do Math on it so it needs to * 14 | * be converted to numbers. I will use the base 10 parameter of the toString() * 15 | * function and the map() function to convert each entry in the array to a Number. * 16 | * Then I am going to sum each entry in the array to get a total. I will repeat this * 17 | * until my total is a single digit number. The number of times I sum is returned * 18 | * as the answer. * 19 | * * 20 | * Steps for solution * 21 | * 1) Initialize vars sum and loop * 22 | * 2) Convert str to an array of numbers * 23 | * 3) Loop until the sum of digits in array is a single digit number * 24 | * 4) Return number of loops as answer * 25 | * * 26 | ***************************************************************************************/ 27 | 28 | function AdditivePersistence(num) { 29 | 30 | var sum, loop = 0; 31 | var val1 = num.toString(10).split("").map(function(t){return parseInt(t)}); 32 | 33 | while (val1.length > 1) { 34 | sum = 0; 35 | val1.forEach( function(number) { 36 | sum = sum + number; 37 | }); 38 | 39 | val1 = sum.toString(10).split("").map(function(t){return parseInt(t)}); 40 | loop++; 41 | } 42 | 43 | return loop; 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Alphabet Soup: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Alphabet Soup * 6 | * Have the function AlphabetSoup(str) take the str string parameter being passed * 7 | * and return the string with the letters in alphabetical order * 8 | * (ie. hello becomes ehllo). Assume numbers and punctuation symbols will not be * 9 | * included in the string. * 10 | * * 11 | * SOLUTION * 12 | * The Array has a built-in sort function but String does not. The first step is to * 13 | * convert the string to an Array and sort it in alphabetical order. Then covert the * 14 | * Array back to a string with the join() function. * 15 | * * 16 | * Steps for solution * 17 | * 1) Convert string to an array * 18 | * 2) Sort array in alphabetical order * 19 | * 3) Convert array back to a string * 20 | * * 21 | ***************************************************************************************/ 22 | 23 | function AlphabetSoup(str) { 24 | 25 | return str.split("").sort().join(""); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Arith Geo: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Arith Geo * 6 | * Using the JavaScript language, have the function ArithGeo(arr) take the array of * 7 | * numbers stored in arr and return the string "Arithmetic" if the sequence follows * 8 | * an arithmetic pattern or return "Geometric" if it follows a geometric pattern. If * 9 | * the sequence doesn't follow either pattern return -1. An arithmetic sequence is * 10 | * one where the difference between each of the numbers is consistent, where as in a * 11 | * geometric sequence, each term after the first is multiplied by some constant or * 12 | * common ratio. Arithmetic example: [2, 4, 6, 8] and Geometric * 13 | * example: [2, 6, 18, 54]. Negative numbers may be entered as parameters, 0 will not * 14 | * be entered, and no array will contain all the same elements. * 15 | * * 16 | * SOLUTION * 17 | * To check for arithmetic pattern, start by getting the difference between the first * 18 | * two number. Then loop thru array starting in position 2 and subtract the previous * 19 | * number. If the difference is equal to the initial difference then you have an * 20 | * arithmetic pattern so return arithmetic. Next repeat by getting initial difference * 21 | * by dividing the first and second numbers. Loop through array starting in position * 22 | * 2 and compare the current number divided by previous number. If difference is * 23 | * equal to the initial number then you have a geometric pattern. Else return -1. * 24 | * * 25 | * Steps for solution * 26 | * 1) set flags for both patterns to True * 27 | * 2) Loop thru array starting in position 2 and compare difference between current * 28 | * number and the previous number to see if it is equal to initial difference * 29 | * 3) If aritimetic pattern exists return arithmetic else check for geometric * 30 | * 4) Get difference between second numvber divided by first number * 31 | * 5) Loop thru array starting in position 2 and compare difference between current * 32 | * number and the previous to see if it is equal to the intial difference * 33 | * 6) If geometric pattern exists return geometric * 34 | * 7) Else return -1 * 35 | * * 36 | ***************************************************************************************/ 37 | 38 | function ArithGeo(arr) { 39 | 40 | var arithFlag = true, geoFlag = true; 41 | var diff = arr[1] - arr[0]; 42 | 43 | for (var i = 2; i < arr.length; i++) { 44 | if ((arr[i] - arr[i-1]) !== diff) { 45 | arithFlag = false; 46 | } 47 | } 48 | if (arithFlag) { 49 | return "Arithmetic"; 50 | } 51 | else { // check for geometric pattern 52 | diff = arr[1] / arr[0]; 53 | for (var i = 2; i < arr.length; i++) { 54 | if ((arr[i] / arr[i-1]) !== diff) { 55 | geoFlag = false; 56 | } 57 | } 58 | if (geoFlag) { 59 | return "Geometric"; 60 | } 61 | else { 62 | return "-1"; 63 | } 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /Array Addition I: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Array Addition I * 6 | * Using the JavaScript language, have the function ArrayAdditionI(arr) take the array * 7 | * of numbers stored in arr and return the string true if any combination of numbers * 8 | * in the array can be added up to equal the largest number in the array, otherwise * 9 | * return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the * 10 | * output should return true because 4 + 6 + 10 + 3 = 23. The array will not be empty, * 11 | * will not contain all the same elements, and may contain negative numbers. * * 12 | * * 13 | * SOLUTION * 14 | * To get the largest number I am going to sort the array in ascending order which * 15 | * leaves the largest number at the end of the array. I can get largest number using * 16 | * pop() function. To get the total I am going to use two nested loops. The outer * 17 | * loop goes through every number in the array. The inner loop then adds all the other * 18 | * numbers in the array and then compares the total to the largest number. If a match * 19 | * is found then return true else return false. * 20 | * * 21 | * Steps for solution * 22 | * 1) Sort array in ascending order. * 23 | * 2) Remove largest number from array and store in largest * 24 | * 3) Loop through each number in array * 25 | * 4) Add every other number to this number and see if total matches largest * 26 | * 5) If match return True else return False 27 | * * 28 | ***************************************************************************************/ 29 | 30 | function ArrayAdditionI(arr) { 31 | 32 | arr.sort(function(a,b){return a - b}); 33 | var maxNum = arr.pop(); 34 | var tot = 0; 35 | 36 | for (var i = 0; i < arr.length; i++){ 37 | tot += arr[i]; 38 | for (var j = 0; j < arr.length; j++){ 39 | if (i != j) { 40 | tot += arr[j]; 41 | if (tot == maxNum) { 42 | return true; 43 | } 44 | } 45 | } 46 | 47 | for (var k = 0; k < arr.length; k++) { 48 | if (i != k) { 49 | tot -= arr[k]; 50 | if (tot == maxNum) { 51 | return true; 52 | } 53 | } 54 | } 55 | tot = 0; 56 | } 57 | 58 | return false; 59 | 60 | } 61 | -------------------------------------------------------------------------------- /Check Nums: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * First Factorial * 6 | * Using the JavaScript language, have the function CheckNums(num1,num2) take both * 7 | * parameters being passed and return the string true if num2 is greater than num1, * 8 | * otherwise return the string false. If the parameter values are equal to each other * 9 | * then return the string -1 * 10 | * * 11 | * SOLUTION * 12 | * This solution has to use an If...else if...else statement since you will be * 13 | * comparing for three different comparisons. 14 | * * 15 | * Steps for solution * 16 | * 1) If num1 and num2 are equal then return string -1 * 17 | * 2) Else If num2 is greater than num1 then return true * 18 | * 3) Else return false * 19 | * * 20 | ***************************************************************************************/ 21 | 22 | function CheckNums(num1,num2) { 23 | 24 | if (num1 === num2) { 25 | return "-1"; 26 | } else if (num2 > num1) { 27 | return true; 28 | } else { 29 | return false; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Counting Minutes I: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Counting Minutes I * 6 | * Using the JavaScript language, have the function CountingMinutesI(str) take the * 7 | * str parameter being passed which will be two times (each properly formatted with * 8 | * a colon and am or pm) separated by a hyphen and return the total number of minutes * 9 | * between the two times. The time will be in a 12 hour clock format. For example: * 10 | * if str is 9:00am-10:00am then the output should be 60. If str is 1:00pm-11:00am * 11 | * the output should be 1320. * 12 | * * 13 | * SOLUTION * 14 | * Sometimes it pays to spend more time mapping out all the possibilites you might * 15 | * face in solving a problem before you start programming. This challenge will have * 16 | * 4 possible scenarios. They are: * 17 | * a) both have same ampm values and first time is after second time * 18 | * b) both have same ampm values and the first time is before second time * 19 | * c) have different ampm times and time1 is am * 20 | * d) have different ampm times and time2 is pm * 21 | * * 22 | * I am going to use an object to represent both times simply because I like to use * 23 | * words like hours, mins, and ampm instead of referring to an array index. To parse * 24 | * the str into the two time objects I created a separate function. Now that I have * 25 | * my 2 time object I just need to create a series of IF statements to correspond to * 26 | * the 4 scenarios listed above to calculate the timeDiff. * 27 | * * 28 | * Steps for solution * 29 | * 1) Use RegExp pattern to search string for pattern a...b * 30 | * 2) If found return true * 31 | * 3) Else return false * 32 | * * 33 | ***************************************************************************************/ 34 | function CountingMinutesI(str) { 35 | var time1Obj = {}, time2Obj = {}, timeDiff; 36 | 37 | time1Obj = setTimeObject(str, 0); 38 | time2Obj = setTimeObject(str, 1); 39 | 40 | if (time1Obj.ampm == time2Obj.ampm && time1Obj.tot > time2Obj.tot) { 41 | timeDiff = (((12 - time1Obj.hours + 12) * 60) - (time1Obj.mins)) + ((time2Obj.hours * 60) + time2Obj.mins); 42 | } 43 | else if (time1Obj.ampm == time2Obj.ampm && time1Obj.tot < time2Obj.tot) { 44 | timeDiff = ((time2Obj.hours * 60) + time2Obj.mins) - ((time1Obj.hours * 60) + time1Obj.mins); 45 | } 46 | else if (time1Obj.ampm !== time2Obj.ampm && time1Obj.ampm === "am") { 47 | timeDiff = (((12 - time1Obj.hours) * 60) - time1Obj.mins) + ((time2Obj.hours * 60) + time2Obj.mins); 48 | } 49 | else { 50 | timeDiff = (((12 - time1Obj.hours) * 60) - time1Obj.mins) + ((time2Obj.hours * 60) + time2Obj.mins); 51 | } 52 | 53 | return timeDiff; 54 | } 55 | 56 | function setTimeObject(str, num) { 57 | var arr = str.split("-"); 58 | var tObject = {}; 59 | 60 | tObject.hours = Number(arr[num].slice(0,arr[num].length-2).split(":")[0]); 61 | tObject.mins = Number(arr[num].slice(0,arr[num].length-2).split(":")[1]); 62 | tObject.ampm = arr[num].slice(-2); 63 | tObject.tot = tObject.hours * 100 + tObject.mins; 64 | 65 | return tObject; 66 | } 67 | -------------------------------------------------------------------------------- /Dash Insert: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Dash Insert * 6 | * Using the JavaScript language, have the function DashInsert(str) insert dashes * 7 | * ('-') between each two odd numbers in str. For example: if str is 454793 the * 8 | * output should be 4547-9-3. Don't count zero as an odd number. * 9 | * * 10 | * SOLUTION * 11 | * I am going to loop through each number in the string. Test if number is odd using * 12 | * modulus. If odd then check if next number is also odd. I have two odd numbers then * 13 | * insert dash into the string. 14 | * * 15 | * Steps for solution * 16 | * 1) Initialize idx to zero since using this as our counter * 17 | * 2) Use While loop to loop thru string since we will be adding dashes to it * 18 | * 3) If currrent character is odd and so is next character then insert dash * 19 | * 4) return modified string as answer * 20 | * * 21 | ***************************************************************************************/ 22 | 23 | function DashInsert(str) { 24 | 25 | var idx = 0; 26 | while (idx < str.length-1) { 27 | if (Number(str[idx]) % 2 === 1 && Number(str[idx+1]) % 2 === 1) { 28 | str = str.slice(0,idx+1) + "-" + str.slice(idx+1); 29 | idx = idx + 2; 30 | } 31 | else { 32 | idx++; 33 | } 34 | } 35 | return str; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Division Stringified: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Division Stringified * 6 | * Using the JavaScript language, have the function DivisionStringified(num1,num2) * 7 | * take both parameters being passed, divide num1 by num2, and return the result as * 8 | * a string with properly formatted commas. If an answer is only 3 digits long, * 9 | * return the number with no commas (ie. 2 / 3 should output "1"). For example: * 10 | * if num1 is 123456789 and num2 is 10000 the output should be "12,345". * 11 | * * 12 | * SOLUTION * 13 | * The first step is to divide the two number and get a whole number that you can * 14 | * format according to the directions. I use RegExp to format the number. * 15 | * * 16 | * Steps for solution * 17 | * 1) Divide num1 by num2 and round to whole number * 18 | * 2) Use RegExp to format string as required * 19 | * * 20 | ***************************************************************************************/ 21 | function DivisionStringified(num1,num2) { 22 | 23 | var tot = Math.round(num1 / num2); 24 | var newNum = tot.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); 25 | 26 | return newNum; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Ex Oh: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Ex Oh * 6 | * Using the JavaScript language, have the function ExOh(str) take the str parameter * 7 | * being passed and return the string true if there is an equal number of x's and o's, * 8 | * otherwise return the string false. Only these two letters will be entered in the * 9 | * string, no punctuation or numbers. For example: if str is "xooxxxxooxo" then the * 10 | * output should return false because there are 6 x's and 5 o's. * 11 | * * 12 | * SOLUTION * 13 | * My solution accounts for the one outlier which is when the length of the string * 14 | * is not even. Since the count of Xs and Os have to be equal then the only way for * 15 | * this to happen is if the length of the string is an even number. I am going to * 16 | * loop through the string and count every X. Then I am going to compare this count * 17 | * to half the length of the string. If they are equal then you have equal number of * 18 | * Xs and Os. * 19 | * * 20 | * Steps for solution * 21 | * 1) Handle outlier first by seeing if str length is odd * 22 | * 2) Loop thru each letter and count the number of Xs * 23 | * 3) Compare count to 1/2 length of str and if same then return true * 24 | * 4) Else return false * 25 | * * 26 | ***************************************************************************************/ 27 | function ExOh(str) { 28 | 29 | if (str.length % 2 === 1) { 30 | return false; 31 | } 32 | else { 33 | var tot = 0; 34 | for (var i = 0; i < str.length; i++) { 35 | if (str[i] === "x") { 36 | tot++; 37 | } 38 | } 39 | 40 | if (tot === (str.length / 2)) { 41 | return true; 42 | } 43 | else { 44 | return false; 45 | } 46 | 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /First Factorial: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * First Factorial * 6 | * Using the JavaScript language, have the function FirstFactorial(num) take the num * 7 | * parameter being passed and return the factorial of it (ie. if num = 4, * 8 | * return (4 * 3 * 2 * 1)). For the test cases, the range will be between 1 and 18. * * 9 | * * 10 | * SOLUTION * 11 | * You can either use an iterative or recursive function to solve this challenge. * 12 | * I am going to use an interative function. I am going to start with a value of 1 * 13 | * for my total and then keep multiplying it by the next number until I reach num. * 14 | * * 15 | * This function needs to account for a possible outlier - One and Zero. * 16 | * If num is 1 or 0 then the answer is 1. By setting tot to value of 1 at * 17 | * initialization, then it guaranteees that 1 will be returned if num is ever 0 or 1. * 18 | * Steps for solution * 19 | * 1) Set var tot to 1. * 20 | * 2) Loop from 2 to num and multiple tot by num to get new tot. * 21 | * 3) Return tot for answer. * 22 | * * 23 | ***************************************************************************************/ 24 | 25 | function FirstFactorial(num) { 26 | 27 | var tot = 1; 28 | for (var i = 2; i <= num; i++) { 29 | tot *= i; 30 | } 31 | 32 | return tot; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /First Reverse: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * First Reverse * 6 | * Have the function FirstReverse() take the str parameter being passed and return * 7 | * the string in reversed order. * 8 | * * 9 | * SOLUTION * 10 | * There is no reverse function for Strings in JavaScript BUT there is a reverse() * 11 | * function for arrays. I will use this built-in function for my solution. * 12 | * 1) Convert the string to an array using the split() function. * 13 | * 2) Use Array Reverse() function. * 14 | * 3) Convert array back to a string using join() function. * 15 | * * 16 | ***************************************************************************************/ 17 | 18 | function FirstReverse(str) { 19 | 20 | return str.split("").reverse().join(""); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Letter Capitalize: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Letter Capitalize * 6 | * Using the JavaScript language, have the function LetterCapitalize(str) take the * 7 | * str parameter being passed and capitalize the first letter of each word. Words * 8 | * will be separated by only one space. * 9 | * * 10 | * SOLUTION * 11 | * I am going to convert the string to an array based on the space character that * 12 | * separates each word. Then I am going to loop through each word in the array and * 13 | * capitalize the first letter of the word. I am going to convert array back to a * 14 | * string to return as the answer. 15 | * * 16 | * Steps for solution * 17 | * 1) Convert string to arry of words by splitting on space. * 18 | * 2) Loop thru each word in the array and convert first letter to uppercase * 19 | * using the charAt(0) value and then appending the remainder of the word * 20 | * using the slice() function * 21 | * 3) Convert array to string with join() function. * 22 | * * 23 | ***************************************************************************************/ 24 | 25 | function LetterCapitalize(str) { 26 | 27 | var arr = str.split(" "); 28 | for (var i = 0; i < arr.length; i++) { 29 | arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1); 30 | } 31 | 32 | return arr.join(" "); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Letter Changes: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Letter Changes * 6 | * Using the JavaScript language, have the function LetterChanges(str) take the str * 7 | * parameter being passed and modify it using the following algorithm. Replace every * 8 | * letter in the string with the letter following it in the alphabet * 9 | * (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string * 10 | * (a, e, i, o, u) and finally return this modified string. * 11 | * * 12 | * SOLUTION * 13 | * You have to realize that the string passed in may contain items other than letters * 14 | * of the alphabet. If you find a character that is not a-z then just pass it along * 15 | * to the newStr as is without any modification. I am going to compare each letter * 16 | * in the string to the alphabet string. If the letter is found then I am going to * 17 | * return the next letter in the string unless the letter is z and them I am going * 18 | * to return a. When finished I am going to use a RegExp to replace all lower case * 19 | * vowels with upper case. 20 | * * 21 | * Steps for solution * 22 | * 1) Create alphabet string that contains all letters of the alphabet * 23 | * 2) Loop through each letter in the string * 24 | * 3) If letter is Z then return a * 25 | * 4) If letter is not a-z then return letter as is to newStr * 26 | * 5) If letter is a-z then return the next character in the alphabet string * 27 | * 6) Replace all vowels with upper case with a RegExp replace() function * 28 | * * 29 | ***************************************************************************************/ 30 | 31 | function LetterChanges(str) { 32 | 33 | var alphabet = "abcdefghijklmnopqrstuvwxyz"; 34 | var newStr = ""; 35 | var loc; 36 | 37 | for (var i = 0; i < str.length; i++) { 38 | loc = alphabet.indexOf(str[i]); 39 | if (loc === 25) { 40 | newStr = newStr + "a"; 41 | } else if (loc === -1) { 42 | newStr = newStr + str[i]; 43 | } else { 44 | newStr = newStr + alphabet[loc + 1]; 45 | } 46 | } 47 | 48 | return newStr.replace(/[aeiou]/g, function(letter) {return letter.toUpperCase()}); 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Letter Count I: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Letter Count I * 6 | * Have the function LetterCountI(str) take the str parameter being passed and return * 7 | * the first word with the greatest number of repeated letters. For example: * 8 | * "Today, is the greatest day ever!" should return greatest because it has 2 e's * 9 | * (and 2 t's) and it comes before ever which also has 2 e's. If there are no words * 10 | * with repeating letters return -1. Words will be separated by spaces. * 11 | * 12 | * * 13 | * SOLUTION * 14 | * The first step is to remove all punctuation from the string being passed in which * 15 | * I do with the replace function. I also convert string to LowerCase to account for * 16 | * any words in the string that might be ProperCase. I then convert string into an * 17 | * array of words breaking on space. Next I loop through each word in the array. * 18 | * I loop through each character in the word and count the number of times the letter * 19 | * repeats. Then I compare the max times a letter is repeated in that word to the * 20 | * current value of maxCt which is initalized with a value of 1. If the number of * 21 | * repeated characters is greater then I update the maxCt with the new value and the * 22 | * current word is the maxWord. When finished loop I check to see if any word has * 23 | * repeated characters and if not return -1 else return the word with max repeated * 24 | * characters. * 25 | * * 26 | * Steps for solution * 27 | * 1) Initialize variables * 28 | * 2) Convert string to lowerCase, remove all punctuation and store in array * 29 | * 3) Loop through each word in array and count the occurance of each letter * 30 | * 4) Compare occurance of repeated letter to maxCt * 31 | * 5) If greater update maxCt to new value and store word in maxWord * 32 | * 6) Return word with max repeated characters or -1 * 33 | * * 34 | ***************************************************************************************/ 35 | 36 | function LetterCountI(str) { 37 | var ctObj, tempWord, maxWord, maxCt = 1; 38 | var arr = str.toLowerCase().replace(/[^a-zA-Z ]/g,"").split(" "); 39 | 40 | for(var i = 0; i < arr.length; i++){ 41 | tempWord = arr[i]; 42 | ctObj = {} 43 | 44 | for(var j = 0; j maxCt) { 51 | maxCt = ctObj[key]; 52 | maxWord = tempWord; 53 | } 54 | } 55 | } 56 | } 57 | 58 | if (maxCt === 1) { 59 | return -1; 60 | } else { 61 | return maxWord; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Longest Word: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Longest Word * 6 | * Using the JavaScript language, have the function LongestWord(sen) take the sen * 7 | * parameter being passed and return the largest word in the string. If there are * 8 | * two or more words that are the same length, return the first word from the string * 9 | * with that length. Ignore punctuation and assume sen will not be empty. * 10 | * * 11 | * When working on the solution you have to be aware of the directions that says to * 12 | * ignore punctuation. That means we need to strip out everything in the string * 13 | * except for letters a-z and space. Once removed I am going to use the sort() * 14 | * function for Array to sort each word by length in descending order. The first * 15 | * entry in the array is the longest word. * 16 | * 1) Remove anything in string that is not a-z, A-Z or blank * 17 | * 2) Convert String to Array * 18 | * 3) Sort Array in descending order based on length of word * 19 | * 4) First entry in sorted array is solution * 20 | * * 21 | ***************************************************************************************/ 22 | 23 | function LongestWord(sen) { 24 | 25 | var arr = sen.replace(/[^a-zA-Z ]/g,"").split(" "); 26 | 27 | arr.sort(function(a,b) { return b.length - a.length } ); 28 | return arr.shift(); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /MeanMode: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * MeanMode * 6 | * Have the function MeanMode(arr) take the array of numbers stored in arr and * 7 | * return 1 if the mode equals the mean, 0 if they don't equal each other * 8 | * (ie. [5, 3, 3, 3, 1] should return 1 because the mode (3) equals the mean (3)). * 9 | * The array will not be empty, will only contain positive integers, and will not * 10 | * contain more than one mode. * 11 | * * 12 | * SOLUTION * 13 | * Since it is possible that I will want a function that will calculate the mean or * 14 | * mode in the future, I decided to create separate functions for each. The mean is * 15 | * calculated by the average of all values in the array. The mode is the number that * 16 | * exists the most in the array. My solution is to call my two functions and then * 17 | * compare to see if they are equal and if so return 1 else return 0. * 18 | * * 19 | * Steps for solution * 20 | * 1) Create separate functions for getMean and getMode * 21 | * 2) Compare the values returned from the two functions * 22 | * 3) If values are equal return 1 else return 0 * 23 | * * 24 | ***************************************************************************************/ 25 | function MeanMode(arr) { 26 | 27 | var myMean, myMode; 28 | 29 | myMode = getMode(arr); 30 | myMean = getMean(arr); 31 | 32 | if(myMode == myMean) { 33 | return 1; 34 | } else { 35 | return 0; 36 | } 37 | 38 | } 39 | 40 | function getMean(arr) { 41 | var sum = 0, mean; 42 | 43 | for (var i = 0; i < arr.length; i++){ 44 | sum = sum + arr[i]; 45 | } 46 | mean = sum / arr.length; 47 | 48 | return mean; 49 | } 50 | 51 | 52 | function getMode(arr) { 53 | var ctObj = {}, mode, maxCt = 1; 54 | 55 | arr.sort(function(a, b) {return a - b;}); 56 | 57 | for (var i = 0; i < arr.length; i++){ 58 | ctObj[arr[i]] = ctObj[arr[i]] || 0; 59 | ctObj[arr[i]]++; 60 | } 61 | 62 | for (var key in ctObj) { 63 | if (ctObj.hasOwnProperty(key)) { 64 | if (ctObj[key] > maxCt) { 65 | maxCt = ctObj[key]; 66 | mode = key; 67 | } 68 | } 69 | } 70 | 71 | return mode; 72 | } 73 | -------------------------------------------------------------------------------- /Multiplicative Persistence: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Multiplicative Persistence * 6 | * Using the JavaScript language, have the function FirstFactorial(num) take the num * 7 | * parameter being passed and return the factorial of it (ie. if num = 4, * 8 | * return (4 * 3 * 2 * 1)). For the test cases, the range will be between 1 and 18. * * 9 | * * 10 | * SOLUTION * 11 | * The challenge passes a string but it expects us to do Math on it so it needs to * 12 | * be converted to numbers. I will use the base 10 parameter of the toString() * 13 | * function to convert each entry in the array to a Number. Then I am going to * 14 | * multiply each entry in the array to get a total. I will repeat this until my * 15 | * total is a single digit number. The number of times I multiplied is returned * 16 | * as the answer. * 17 | * * 18 | * Steps for solution * 19 | * 1) Initialize vars sum and loop * 20 | * 2) Convert str to an array of numbers * 21 | * 3) Loop until the sum of digits in array is a single digit number * 22 | * 4) Return number of loops as answer * 23 | * * 24 | ***************************************************************************************/ 25 | 26 | function MultiplicativePersistence(num) { 27 | 28 | var sum, loop = 0; 29 | var val1 = num.toString(10).split(""); 30 | 31 | while( val1.length > 1 ) { 32 | sum = 1; 33 | val1.forEach( function(number) { 34 | sum = sum * number; 35 | }); 36 | val1 = sum.toString(10).split(""); 37 | loop++; 38 | } ; 39 | 40 | return loop; 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Number Addition: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Number Addition * 6 | * Using the JavaScript language, have the function NumberSearch(str) take the str * 7 | * parameter, search for all the numbers in the string, add them together, then * 8 | * return that final number. For example: if str is "88Hello 3World!" the output * 9 | * should be 91. You will have to differentiate between single digit numbers and * 10 | * multiple digit numbers like in the example above. So "55Hello" and "5Hello 5" * 11 | * should return two different answers. Each string will contain at least one letter * 12 | * or symbol. * 13 | * * 14 | * SOLUTION * 15 | * I only want numbers in the string so I am using RegExp to remove everything that * 16 | * is not a number. Then convert that to an array. Loop thru each number in the array * 17 | * and add tot to get the answer. * 18 | * * 19 | * Steps for solution * 20 | * 1) Initialize tot to zero * 21 | * 2) Remove everything but numbers from string and convert to array * 22 | * 3) Loop thru each number in array and add to tot * 23 | * 4) Return tot for answer * 24 | * * 25 | ***************************************************************************************/ 26 | 27 | function NumberAddition(str) { 28 | 29 | var tot = 0; 30 | 31 | str = str.replace(/[^0-9\.]+/g," ").split(" "); 32 | for (var i = 0; i < str.length; i++) { 33 | tot += Number(str[i]); 34 | } 35 | 36 | return tot; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Off Line Minimium: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * OffLine Minimum * 6 | * Using the JavaScript language, have the function OffLineMinimum(strArr) take the * 7 | * strArr parameter being passed which will be an array of integers ranging from * 8 | * 1...n and the letter "E" and return the correct subset based on the following * 9 | * rules. The input will be in the following format: ["I","I","E","I",...,"E",...,"I"] * 10 | * where the I's stand for integers and the E means take out the smallest integer * 11 | * currently in the whole set. When finished, your program should return that new set * 12 | * with integers separated by commas. For example: if strArr is * 13 | * ["5","4","6","E","1","7","E","E","3","2"] then your program should return 4,1,5. * * 14 | * * 15 | * SOLUTION * 16 | * This challenge requires you to repeatedly find the lowerst value in a subset of an * 17 | * array every time the letter E is found. So to handle this repettiion I am creating * 18 | * a function that will sort an array in ascending order and return the lowest value. * 19 | * I will loop through each entry in the array and when I encounter an E then I will * 20 | * pass a subset of my array starting from position 0 to the location of the E. I * 21 | * will send this subset to my function to get the lowest value. I will collect all * 22 | * of the lowest values in an array. When finished I will convert array to string. 23 | * * 24 | * Steps for solution * 25 | * 1) Initialize var subset and min * 26 | * 2) Create function that sorts an array in asc order and returns lowest value * 27 | * 3) Store lowest value in an array * 28 | * 4) Convert array into a string and return as answer * 29 | * * 30 | ***************************************************************************************/ 31 | 32 | function OffLineMinimum(strArr) { 33 | 34 | var subset = [], min = 0; 35 | function findMin (arr) { return arr.sort(function (a,b) {return Number(a)-Number(b);})[0]; } 36 | for (var i=0;i 2) { 39 | uniqueArray.sort(function(a, b){return a-b}); 40 | return uniqueArray[1] + " " + uniqueArray[uniqueArray.length - 2]; 41 | } 42 | else { 43 | return uniqueArray[1] + " " + uniqueArray[0]; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Simple Adding: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Simple Adding * 6 | * Using the JavaScript language, have the function SimpleAdding(num) add up all the * 7 | * numbers from 1 to num. For the test cases, the parameter num will be any number * 8 | * from 1 to 1000. * 9 | * * 10 | * SOLUTION * 11 | * This is a simple iterative function that add each number in sequence. * 12 | * * 13 | * Steps for solution * 14 | * 1) Set var tot to 0. * 15 | * 2) Loop from 1 to num and add i by tot to get new tot. * 16 | * 3) Return tot for answer. * 17 | * * 18 | ***************************************************************************************/ 19 | 20 | function SimpleAdding(num) { 21 | 22 | var tot = 0; 23 | for (var i = 1; i <= num; i++) { 24 | tot += i; 25 | } 26 | 27 | return tot; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Simple Symbols: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Simple Symbols * 6 | * Using the JavaScript language, have the function SimpleSymbols(str) take the str * 7 | * parameter being passed and determine if it is an acceptable sequence by either * 8 | * returning the string true or false. The str parameter will be composed of + and = * 9 | * symbols with several letters between them (ie. ++d+===+c++==a) and for the string * 10 | * to be true each letter must be surrounded by a + symbol. So the string to the left * 11 | * would be false. The string will not be empty and will have at least one letter. * * 12 | * * 13 | * SOLUTION * 14 | * When reading the directions for this challenge you have to be aware of the adage * 15 | * "Attention to detail, all the time!" The instructions immediately tell you that * 16 | * you will have two outliers that you have to account for in your solution. They are * 17 | * the first and last characters. If these are letters then you have to return false * 18 | * because there cannot be a + before or after these letters. 19 | * * 20 | * Steps for solution * 21 | * 1) Convert string to an Array. * 22 | * 2) Loop thru each letter in array * 23 | * 3) If first character or last character is letter a-z then return false (outlier)* 24 | * 4) If charcter is string then check if letter before and after is + and if not * 25 | * then return false * 26 | * 5) If looped through whole array and everything matches then return true * 27 | * * 28 | ***************************************************************************************/ 29 | 30 | function SimpleSymbols(str) { 31 | 32 | var arr = str.toLowerCase().split(""); 33 | for (var i = 0; i < arr.length; i++) { 34 | if (arr[i] >= "a" && arr[i] <= "z") { 35 | if (i === 0 || i === arr.length) { 36 | return false; 37 | } 38 | 39 | if (arr[i-1] !== "+" || arr[i+1] !== "+") { 40 | return false; 41 | } 42 | } 43 | } 44 | 45 | return true; 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Swap Case: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Swap Case * 6 | * Using the JavaScript language, have the function SwapCase(str) take the str * 7 | * parameter and swap the case of each character. For example: if str is "Hello World" * 8 | * the output should be hELLO wORLD. Let numbers and symbols stay the way they are. * * 9 | * * 10 | * SOLUTION * 11 | * I am going to be using a new string to store my modified results. I am going to * 12 | * loop thru every character and covert it to UpperCase. Then I am going to compare * 13 | * that to the character in the string. If they are the same - meaning both are * 14 | * uppercase then I am going to return the lowercase of the character. If they are not * 15 | * the same then return the Uppercase character. 16 | * * 17 | * Steps for solution * 18 | * 1) Create new string to hold my results * 19 | * 2) Loop thru each character in the string * 20 | * 3) Save the uppercase of this character in u * 21 | * 4) Compare uppercase to current character * 22 | * 5) If they are the same then return the lowercase * 23 | * 6) Else return uppercase * 24 | * * 25 | ***************************************************************************************/ 26 | 27 | function SwapCase(str) { 28 | 29 | var result = ''; 30 | 31 | for (var i = 0; i < str.length; i++) { 32 | var c = str[i]; 33 | var u = c.toUpperCase(); 34 | 35 | result += u === c ? c.toLowerCase() : u; 36 | } 37 | 38 | return result; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Third Greatest: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Third Greatest * 6 | * Using the JavaScript language, have the function ThirdGreatest(strArr) take the * 7 | * array of strings stored in strArr and return the third largest word within in. * 8 | * So for example: if strArr is ["hello", "world", "before", "all"] your output should * 9 | * be world because "before" is 6 letters long, and "hello" and "world" are both 5, * 10 | * but the output should be world because it appeared as the last 5 letter word in * 11 | * the array. If strArr was ["hello", "world", "after", "all"] the output should be * 12 | * after because the first three words are all 5 letters long, so return the last one. * 13 | * The array will have at least three strings and each string will only contain * 14 | * letters. * 15 | * * 16 | * SOLUTION * 17 | * I am going to sort the array in descending order using the length of each entry. * 18 | * Once sorted the third greatest number is in array position 2. * 19 | * * 20 | * Steps for solution * 21 | * 1) Sort array in decending order based on length of entry * 22 | * 2) Return the entry in the 3rd spot * 23 | * * 24 | ***************************************************************************************/ 25 | 26 | function ThirdGreatest(strArr) { 27 | 28 | strArr.sort(function(a, b){ 29 | return b.length - a.length; 30 | }); 31 | 32 | return strArr[2]; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Time Convert: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Time Convert * 6 | * Using the JavaScript language, have the function TimeConvert(num) take the num * 7 | * parameter being passed and return the number of hours and minutes the parameter * 8 | * converts to (ie. if num = 63 then the output should be 1:3). Separate the number * 9 | * of hours and minutes with a colon. * 10 | * * 11 | * SOLUTION * 12 | * There are 60 minutes in an hour. To find the number of hours divide the num by * 13 | * 60 and use the Math.floor to convert to whole hours. Next use the modulus * 14 | * function to get the minutes. 15 | * * 16 | * Steps for solution * 17 | * 1) Get hours by dividing num by 60 to get whole number of hours * 18 | * 2) Get minutes by using modulus function * 19 | * 3) Combine hours and minutes in required format and return as answer * 20 | * * 21 | ***************************************************************************************/ 22 | 23 | function TimeConvert(num) { 24 | 25 | var hours = Math.floor(num / 60); 26 | var minutes = num % 60; 27 | return hours + ":" + minutes; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Vowel Count: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Vowel Count * 6 | * Using the JavaScript language, have the function VowelCount(str) take the str * 7 | * string parameter being passed and return the number of vowels the string contains * 8 | * (ie. "All cows eat grass" would return 5). Do not count y as a vowel for this * 9 | * challenge. * 10 | * * 11 | * SOLUTION * 12 | * I will be using two for loops that will be nested. I will be searching thru all * 13 | * entries in my vowel array in the outter loop and searching through each letter * 14 | * in the string in the inner loop. I will be comparing each vowel to every letter * 15 | * in the string. If it matches then I will be incrememting by total by 1. 16 | * * 17 | * Steps for solution * 18 | * 1) create my vowel arrray. * 19 | * 2) Initialize tot to zero. * 20 | * 3) Loop through each vowel and compare to each letter in string * 21 | * 4) If match then increment tot by 1 * 22 | * 5) Return tot as answer * 23 | * * 24 | ***************************************************************************************/ 25 | function VowelCount(str) { 26 | 27 | var vowelArr = ["a","e","i","o","u"]; 28 | var tot = 0; 29 | 30 | for (var i = 0; i < vowelArr.length; i++) { 31 | for (var j = 0; j < str.length; j++) { 32 | if (str[j] === vowelArr[i]) { 33 | tot++; 34 | } 35 | } 36 | } 37 | return tot; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Word Count: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * * 3 | * CODERBYTE BEGINNER CHALLENGE * 4 | * * 5 | * Word Count * 6 | * Using the JavaScript language, have the function WordCount(str) take the str * 7 | * string parameter being passed and return the number of words the string contains * 8 | * (ie. "Never eat shredded wheat" would return 4). Words will be separated by single * 9 | * spaces. * 10 | * * 11 | * SOLUTION * 12 | * I am going to convert str to an Array breaking each word into an Array entry * 13 | * when there is a space. Then return the length of the Array which is the number * 14 | * of words in the string. * 15 | * * 16 | * Steps for solution * 17 | * 1) Convert string to an array breaking on space. * 18 | * 2) Return array length since this is the number of words * 19 | * * 20 | ***************************************************************************************/ 21 | function WordCount(str) { 22 | 23 | return str.split(" ").length; 24 | 25 | } 26 | --------------------------------------------------------------------------------