From 5ddb05edf3a47c407c4debd3d979144cb273e842 Mon Sep 17 00:00:00 2001 From: WeiChien Hsu Date: Thu, 19 Oct 2017 13:51:09 -0500 Subject: [PATCH] add Algorithm --- .../argument_Unite_Unique.js | 25 +++++++ algorithm_FreeCodeCamp/binary_Agent.js | 23 +++++++ algorithm_FreeCodeCamp/cash_Register.js | 69 +++++++++++++++++++ .../check_For_Palindromes.js | 25 +++++++ algorithm_FreeCodeCamp/chunky_Monkey.js | 9 +++ algorithm_FreeCodeCamp/confirm_the_Ending.js | 6 ++ algorithm_FreeCodeCamp/convertHTML.js | 18 +++++ algorithm_FreeCodeCamp/diff_two_Arrays.js | 18 +++++ algorithm_FreeCodeCamp/drop_Element.js | 13 ++++ algorithm_FreeCodeCamp/factorialize_Number.js | 9 +++ algorithm_FreeCodeCamp/falsy_Bouncer.js | 7 ++ .../findLongest_in_String.js | 13 ++++ algorithm_FreeCodeCamp/finder_Keepers.js | 7 ++ algorithm_FreeCodeCamp/get_Index_to_Ins.js | 15 ++++ algorithm_FreeCodeCamp/inventery_Update.js | 50 ++++++++++++++ .../largest_Numbers_in_Arrays.js | 12 ++++ algorithm_FreeCodeCamp/map_the_Debris.js | 11 +++ algorithm_FreeCodeCamp/merge_Bird.js | 19 +++++ algorithm_FreeCodeCamp/missing_Letter.js | 9 +++ algorithm_FreeCodeCamp/mutations.js | 10 +++ algorithm_FreeCodeCamp/no_Repeat.js | 26 +++++++ algorithm_FreeCodeCamp/object_Model.js | 26 +++++++ algorithm_FreeCodeCamp/object_Search**.js | 14 ++++ algorithm_FreeCodeCamp/oddFib.Sum.js | 24 +++++++ algorithm_FreeCodeCamp/pairDNA.js | 43 ++++++++++++ algorithm_FreeCodeCamp/pair_Wise.js | 20 ++++++ algorithm_FreeCodeCamp/repeat_String.js | 9 +++ algorithm_FreeCodeCamp/reserve_String.js | 16 +++++ .../roman_Numeral_Converter.js | 19 +++++ algorithm_FreeCodeCamp/search_n_Replace.js | 11 +++ algorithm_FreeCodeCamp/slasher_Flick.js | 4 ++ .../smallest_Common_Multiple.js | 23 +++++++ algorithm_FreeCodeCamp/spinal_Case.js | 15 ++++ algorithm_FreeCodeCamp/steam_Roller.js | 16 +++++ algorithm_FreeCodeCamp/sum_All_Number.js | 7 ++ algorithm_FreeCodeCamp/sum_All_Prime.js | 27 ++++++++ .../symmetirc_Difference.js | 22 ++++++ .../title_Case_a_Sentence.js | 12 ++++ algorithm_FreeCodeCamp/truncate_Strings.js | 12 ++++ .../validate_US_Telephone_Num.js | 8 +++ 40 files changed, 722 insertions(+) create mode 100644 algorithm_FreeCodeCamp/argument_Unite_Unique.js create mode 100644 algorithm_FreeCodeCamp/binary_Agent.js create mode 100644 algorithm_FreeCodeCamp/cash_Register.js create mode 100644 algorithm_FreeCodeCamp/check_For_Palindromes.js create mode 100644 algorithm_FreeCodeCamp/chunky_Monkey.js create mode 100644 algorithm_FreeCodeCamp/confirm_the_Ending.js create mode 100644 algorithm_FreeCodeCamp/convertHTML.js create mode 100644 algorithm_FreeCodeCamp/diff_two_Arrays.js create mode 100644 algorithm_FreeCodeCamp/drop_Element.js create mode 100644 algorithm_FreeCodeCamp/factorialize_Number.js create mode 100644 algorithm_FreeCodeCamp/falsy_Bouncer.js create mode 100644 algorithm_FreeCodeCamp/findLongest_in_String.js create mode 100644 algorithm_FreeCodeCamp/finder_Keepers.js create mode 100644 algorithm_FreeCodeCamp/get_Index_to_Ins.js create mode 100644 algorithm_FreeCodeCamp/inventery_Update.js create mode 100644 algorithm_FreeCodeCamp/largest_Numbers_in_Arrays.js create mode 100644 algorithm_FreeCodeCamp/map_the_Debris.js create mode 100644 algorithm_FreeCodeCamp/merge_Bird.js create mode 100644 algorithm_FreeCodeCamp/missing_Letter.js create mode 100644 algorithm_FreeCodeCamp/mutations.js create mode 100644 algorithm_FreeCodeCamp/no_Repeat.js create mode 100644 algorithm_FreeCodeCamp/object_Model.js create mode 100644 algorithm_FreeCodeCamp/object_Search**.js create mode 100644 algorithm_FreeCodeCamp/oddFib.Sum.js create mode 100644 algorithm_FreeCodeCamp/pairDNA.js create mode 100644 algorithm_FreeCodeCamp/pair_Wise.js create mode 100644 algorithm_FreeCodeCamp/repeat_String.js create mode 100644 algorithm_FreeCodeCamp/reserve_String.js create mode 100644 algorithm_FreeCodeCamp/roman_Numeral_Converter.js create mode 100644 algorithm_FreeCodeCamp/search_n_Replace.js create mode 100644 algorithm_FreeCodeCamp/slasher_Flick.js create mode 100644 algorithm_FreeCodeCamp/smallest_Common_Multiple.js create mode 100644 algorithm_FreeCodeCamp/spinal_Case.js create mode 100644 algorithm_FreeCodeCamp/steam_Roller.js create mode 100644 algorithm_FreeCodeCamp/sum_All_Number.js create mode 100644 algorithm_FreeCodeCamp/sum_All_Prime.js create mode 100644 algorithm_FreeCodeCamp/symmetirc_Difference.js create mode 100644 algorithm_FreeCodeCamp/title_Case_a_Sentence.js create mode 100644 algorithm_FreeCodeCamp/truncate_Strings.js create mode 100644 algorithm_FreeCodeCamp/validate_US_Telephone_Num.js diff --git a/algorithm_FreeCodeCamp/argument_Unite_Unique.js b/algorithm_FreeCodeCamp/argument_Unite_Unique.js new file mode 100644 index 0000000..c3bab69 --- /dev/null +++ b/algorithm_FreeCodeCamp/argument_Unite_Unique.js @@ -0,0 +1,25 @@ +function uniteUnique(arr) { + // Creates an empty array to store our final result. + var finalArray = []; + + // Loop through the arguments object to truly made the program work with two or more arrays + // instead of 3. + for (var i = 0; i < arguments.length; i++) { + var arrayArguments = arguments[i]; + + // Loops through the array at hand + for (var j = 0; j < arrayArguments.length; j++) { + var indexValue = arrayArguments[j]; + + // Checks if the value is already on the final array. + if (finalArray.indexOf(indexValue) < 0) { + finalArray.push(indexValue); + } + } + } + + console.log(finalArray) + ; +} + +uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]); diff --git a/algorithm_FreeCodeCamp/binary_Agent.js b/algorithm_FreeCodeCamp/binary_Agent.js new file mode 100644 index 0000000..17bef88 --- /dev/null +++ b/algorithm_FreeCodeCamp/binary_Agent.js @@ -0,0 +1,23 @@ +function binaryAgent(str) { + var wordArr = str.split(" "); + var newWordArr = []; + for (var i = 0; i < wordArr.length; i++){ + newWord = String.fromCharCode(readCode(wordArr[i])); + newWordArr.push(newWord); + } + + return newWordArr.join(""); +} + +function readCode(string){ + var newString = string.split(""); + var sum = 0; + var count = newString.length-1; + var a = []; + for(var i=0; i totalChange){ + return "Insufficient Funds"; + } else{ + return changeCash(change,cid); + } +} + +function readTotal(arr){ + var total = 0; + for(var j = 0; j < arr.length; j++ ){ + total = fpFix(total + arr[j][1]); + } + return total; +} + +function changeCash(total,arr){ + var change = ["ONE HUNDRED","TWENTY","TEN","FIVE","ONE","QUARTER","DIME","NICKEL","PENNY"]; + var result = []; + var num =0; + var cashNum = 0; + var c; + var output; + for(var i=0; i < arr.length; i++){ + c = changes[change[i]]; + if( c <= total){ + num = Math.floor(total/c); //2 + cashNum = arr[arr.length -1 - i][1]; //60 + if( fpFix(num*c) > cashNum ){ + total = fpFix(total - cashNum); + output = parseFloat(Math.round(cashNum * 100) / 100).toFixed(2); + result.push([change[i],Number(output)]); + } else{ + total = fpFix(total - num*c); + output = parseFloat(Math.round(num*c * 100) / 100).toFixed(2); + result.push([change[i],Number(output)]); + } + }//if + } //for + + if(total == 0){ + return result; + } else{ + return "Insufficient Funds"; + } +} // function + +var fpFix = function (n) { + return Math.round(n * 100)/100; +}; + + +changeCash(42.10, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]); \ No newline at end of file diff --git a/algorithm_FreeCodeCamp/check_For_Palindromes.js b/algorithm_FreeCodeCamp/check_For_Palindromes.js new file mode 100644 index 0000000..25c94cd --- /dev/null +++ b/algorithm_FreeCodeCamp/check_For_Palindromes.js @@ -0,0 +1,25 @@ +function palindrome(str) { + // Good luck! + // var newStr = str.replace(/[^a-zA-Z0-9]+/g, "").toLowerCase(); + // var checkNum = newStr.length/2; + // var checkStr =""; + // var oriStr =""; + + // if(newStr.length%2==0){ + // checkStr = newStr.slice(checkNum).split("").reverse().join(""); + // oriStr = newStr.slice(0,checkNum).split("").join(""); + // return (checkStr === oriStr); + // } else{ + // checkStr = newStr.slice(checkNum+0.5).split("").reverse().join(""); + // oriStr = newStr.slice(0,checkNum-0.5).split("").join(""); + // return (checkStr === oriStr); + // } + + var newStr = str.replace(/[^a-zA-Z0-9]+/g, "").toLowerCase(); + var revStr = newStr.split("").reverse().join(""); + return (revStr === newStr); + +} + + +palindrome("never odd or even"); diff --git a/algorithm_FreeCodeCamp/chunky_Monkey.js b/algorithm_FreeCodeCamp/chunky_Monkey.js new file mode 100644 index 0000000..f29a71d --- /dev/null +++ b/algorithm_FreeCodeCamp/chunky_Monkey.js @@ -0,0 +1,9 @@ +function chunkArrayInGroups(arr, size) { + var result = []; + for ( var i=0; i< arr.length / size ; i++){ + result.push(arr.slice(size*i,size*(i+1))); + } + return result; +} + +chunkArrayInGroups([0, 1, 2, 3, 4], 3); diff --git a/algorithm_FreeCodeCamp/confirm_the_Ending.js b/algorithm_FreeCodeCamp/confirm_the_Ending.js new file mode 100644 index 0000000..c690be8 --- /dev/null +++ b/algorithm_FreeCodeCamp/confirm_the_Ending.js @@ -0,0 +1,6 @@ +function confirmEnding(str, target) { + var subStr = str.substr(-target.length); + return (subStr === target); +} + +confirmEnding("Bastian", "n"); \ No newline at end of file diff --git a/algorithm_FreeCodeCamp/convertHTML.js b/algorithm_FreeCodeCamp/convertHTML.js new file mode 100644 index 0000000..7d888b9 --- /dev/null +++ b/algorithm_FreeCodeCamp/convertHTML.js @@ -0,0 +1,18 @@ +function convertHTML(str) { + // :) + map = {'&':'&', + '<':'<', + '>':'>', + '\"':'"', + '\'':"'" + }; + + mapArr = Object.keys(map); + for(var n in mapArr){ + word = mapArr[n]; + str = str.replace(RegExp(word, 'g'),map[word]); + } + return str; +} + +convertHTML("Hamburgers < Pizza < Tacos"); diff --git a/algorithm_FreeCodeCamp/diff_two_Arrays.js b/algorithm_FreeCodeCamp/diff_two_Arrays.js new file mode 100644 index 0000000..717a5b9 --- /dev/null +++ b/algorithm_FreeCodeCamp/diff_two_Arrays.js @@ -0,0 +1,18 @@ +function diffArray(arr1, arr2) { + var newArr = []; + for(var i =0; i 5;}); diff --git a/algorithm_FreeCodeCamp/factorialize_Number.js b/algorithm_FreeCodeCamp/factorialize_Number.js new file mode 100644 index 0000000..7934616 --- /dev/null +++ b/algorithm_FreeCodeCamp/factorialize_Number.js @@ -0,0 +1,9 @@ +function factorialize(num) { + var result = 1; + for(var i=1;i<=num;i++){ + result *= i; + } + return result; +} + +factorialize(5); \ No newline at end of file diff --git a/algorithm_FreeCodeCamp/falsy_Bouncer.js b/algorithm_FreeCodeCamp/falsy_Bouncer.js new file mode 100644 index 0000000..fc7c592 --- /dev/null +++ b/algorithm_FreeCodeCamp/falsy_Bouncer.js @@ -0,0 +1,7 @@ +function bouncer(arr) { + return arr.filter(function(num){ + return Boolean(num) == true; + }); +} + +bouncer([7, "ate", "", false, 9]); diff --git a/algorithm_FreeCodeCamp/findLongest_in_String.js b/algorithm_FreeCodeCamp/findLongest_in_String.js new file mode 100644 index 0000000..67bc8b5 --- /dev/null +++ b/algorithm_FreeCodeCamp/findLongest_in_String.js @@ -0,0 +1,13 @@ +function findLongestWord(str) { + + arrStr = str.split(" "); + for (var i=0;i arrStr[i+1].length){ + arrStr[i+1] = arrStr[i]; + } + } + return arrStr[arrStr.length-1].length; + } + +findLongestWord("The quick brown fox jumped over the lazy dog"); + diff --git a/algorithm_FreeCodeCamp/finder_Keepers.js b/algorithm_FreeCodeCamp/finder_Keepers.js new file mode 100644 index 0000000..bea0a8c --- /dev/null +++ b/algorithm_FreeCodeCamp/finder_Keepers.js @@ -0,0 +1,7 @@ +function findElement(arr, func) { + filterArr = arr.filter(func); + + return filterArr[0]; +} + +findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; }); diff --git a/algorithm_FreeCodeCamp/get_Index_to_Ins.js b/algorithm_FreeCodeCamp/get_Index_to_Ins.js new file mode 100644 index 0000000..a539a85 --- /dev/null +++ b/algorithm_FreeCodeCamp/get_Index_to_Ins.js @@ -0,0 +1,15 @@ +function getIndexToIns(arr, num) { + // Find my place in this sorted array. + sortArr = arr.sort(function(a,b){ + return a-b; + }); + + for(var i=0; i=num){ + return i; + } + } + return arr.length; +} + +getIndexToIns([2, 5, 10], 15); diff --git a/algorithm_FreeCodeCamp/inventery_Update.js b/algorithm_FreeCodeCamp/inventery_Update.js new file mode 100644 index 0000000..e14c148 --- /dev/null +++ b/algorithm_FreeCodeCamp/inventery_Update.js @@ -0,0 +1,50 @@ + +function updateInventory(arr1, arr2) { + for (var i = 0; i < arr2.length; i++) { + var foundMatch = false; + // use each arr2's property to test if it's also on arr1 + for (var j = 0; j < arr1.length; j ++) { + // if yes, add the value + if (arr1[j][1].indexOf(arr2[i][1]) !== -1) { + arr1[j][0] += arr2[i][0]; + foundMatch = true; + } + } + // end of testing, the one didn't be found need to be push into arr1 + if(foundMatch == false){ + arr1.push(arr2[i]); + } + + } + return sortArr(arr1); +} + +// Example inventory lists +var curInv = [ + [21, "Bowling Ball"], + [2, "Dirty Sock"], + [1, "Hair Pin"], + [5, "Microphone"] +]; + +var newInv = [ + [2, "Hair Pin"], + [3, "Half-Eaten Apple"], + [67, "Bowling Ball"], + [7, "Toothpaste"] +]; + + + +function sortArr(arr){ + return arr.sort(function(a,b){ + if(a[1]>b[1]){ + return 1; + } else { + return -1; + } + + }); +} + +updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]); diff --git a/algorithm_FreeCodeCamp/largest_Numbers_in_Arrays.js b/algorithm_FreeCodeCamp/largest_Numbers_in_Arrays.js new file mode 100644 index 0000000..99cee86 --- /dev/null +++ b/algorithm_FreeCodeCamp/largest_Numbers_in_Arrays.js @@ -0,0 +1,12 @@ +function largestOfFour(arr) { + result = []; + for(var i=0; i=0; i--){ +// result.push(rev_str[i]); +// } +// return result.join(""); +// } + +function reverseString(str) { + var result = []; + var rev_str = str.split(""); + return rev_str.reverse().join(""); +} + +reverseString("hello"); diff --git a/algorithm_FreeCodeCamp/roman_Numeral_Converter.js b/algorithm_FreeCodeCamp/roman_Numeral_Converter.js new file mode 100644 index 0000000..cb6a7e1 --- /dev/null +++ b/algorithm_FreeCodeCamp/roman_Numeral_Converter.js @@ -0,0 +1,19 @@ +function convertToRoman(num) { + + var decimalValue = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ]; + var romanNumeral = [ 'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I' ]; + + var romanized = ''; + + for (var i = 0; i < decimalValue.length; i++) { + while (decimalValue[i] <= num) { + romanized += romanNumeral[i]; + num -= decimalValue[i]; + } + } + + return romanized; +} + + +convertToRoman(36); \ No newline at end of file diff --git a/algorithm_FreeCodeCamp/search_n_Replace.js b/algorithm_FreeCodeCamp/search_n_Replace.js new file mode 100644 index 0000000..664cd77 --- /dev/null +++ b/algorithm_FreeCodeCamp/search_n_Replace.js @@ -0,0 +1,11 @@ +function myReplace(str, before, after) { + + if(before.charAt(0) == before.charAt(0).toUpperCase()){ + return str.replace(before,after.charAt(0).toUpperCase()+after.slice(1)); + }else{ + return str.replace(before,after); + } + +} + +myReplace("He is Sleeping on the couch", "Sleeping", "sitting"); diff --git a/algorithm_FreeCodeCamp/slasher_Flick.js b/algorithm_FreeCodeCamp/slasher_Flick.js new file mode 100644 index 0000000..215a65e --- /dev/null +++ b/algorithm_FreeCodeCamp/slasher_Flick.js @@ -0,0 +1,4 @@ +function slasher(arr, howMany) { + return arr.slice(howMany); +} +slasher([1, 2, 3], 2); \ No newline at end of file diff --git a/algorithm_FreeCodeCamp/smallest_Common_Multiple.js b/algorithm_FreeCodeCamp/smallest_Common_Multiple.js new file mode 100644 index 0000000..012657f --- /dev/null +++ b/algorithm_FreeCodeCamp/smallest_Common_Multiple.js @@ -0,0 +1,23 @@ +function smallestCommons(arr) { + var range = []; + for (var i = Math.max(arr[0], arr[1]); i >= Math.min(arr[0], arr[1]); i--) { + range.push(i); + } + + var lcm = range[0]; + for (var i = 1; i