Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Algorithm #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions algorithm_FreeCodeCamp/argument_Unite_Unique.js
Original file line number Diff line number Diff line change
@@ -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]);
23 changes: 23 additions & 0 deletions algorithm_FreeCodeCamp/binary_Agent.js
Original file line number Diff line number Diff line change
@@ -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<newString.length; i++){
sum += (newString[i]*(Math.pow(2,count)));
count-=1;
}
return sum;
}
binaryAgent(("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"));
69 changes: 69 additions & 0 deletions algorithm_FreeCodeCamp/cash_Register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
var changes = {
"ONE HUNDRED": 100.00,
TWENTY:20.00,
TEN:10.00,
FIVE:5.00,
ONE:1.00,
QUARTER:0.25,
DIME:0.10,
NICKEL:0.05,
PENNY:0.01
};

function checkCashRegister(price, cash, cid) {
var change = fpFix(cash - price);
var totalChange = readTotal(cid);
if(change == totalChange){
return "Closed";
} else if(change > 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]]);
25 changes: 25 additions & 0 deletions algorithm_FreeCodeCamp/check_For_Palindromes.js
Original file line number Diff line number Diff line change
@@ -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");
9 changes: 9 additions & 0 deletions algorithm_FreeCodeCamp/chunky_Monkey.js
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 6 additions & 0 deletions algorithm_FreeCodeCamp/confirm_the_Ending.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function confirmEnding(str, target) {
var subStr = str.substr(-target.length);
return (subStr === target);
}

confirmEnding("Bastian", "n");
18 changes: 18 additions & 0 deletions algorithm_FreeCodeCamp/convertHTML.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function convertHTML(str) {
// &colon;&rpar;
map = {'&':'&amp;',
'<':'&lt;',
'>':'&gt;',
'\"':'&quot;',
'\'':"&apos;"
};

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");
18 changes: 18 additions & 0 deletions algorithm_FreeCodeCamp/diff_two_Arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function diffArray(arr1, arr2) {
var newArr = [];
for(var i =0; i<arr2.length; i++){
if(arr1.indexOf(arr2[i]) === -1){
newArr.push(arr2[i]);
}
}

for(var j =0; j<arr1.length; j++){
if(arr2.indexOf(arr1[j]) === -1){
newArr.push(arr1[j]);
}
}

return newArr;
}

diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]);
13 changes: 13 additions & 0 deletions algorithm_FreeCodeCamp/drop_Element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function dropElements(arr, func) {
var len = arr.length;
for(var num = 0; num < len; num++){
if(func(arr[0])){
break;
}else{
arr.shift();
}
}
return arr;
}

dropElements([1, 2, 3, 4], function(n) {return n > 5;});
9 changes: 9 additions & 0 deletions algorithm_FreeCodeCamp/factorialize_Number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function factorialize(num) {
var result = 1;
for(var i=1;i<=num;i++){
result *= i;
}
return result;
}

factorialize(5);
7 changes: 7 additions & 0 deletions algorithm_FreeCodeCamp/falsy_Bouncer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function bouncer(arr) {
return arr.filter(function(num){
return Boolean(num) == true;
});
}

bouncer([7, "ate", "", false, 9]);
13 changes: 13 additions & 0 deletions algorithm_FreeCodeCamp/findLongest_in_String.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function findLongestWord(str) {

arrStr = str.split(" ");
for (var i=0;i<arrStr.length-1;i++){
if(arrStr[i].length > 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");

7 changes: 7 additions & 0 deletions algorithm_FreeCodeCamp/finder_Keepers.js
Original file line number Diff line number Diff line change
@@ -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; });
15 changes: 15 additions & 0 deletions algorithm_FreeCodeCamp/get_Index_to_Ins.js
Original file line number Diff line number Diff line change
@@ -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<arr.length; i++){
if(sortArr[i]>=num){
return i;
}
}
return arr.length;
}

getIndexToIns([2, 5, 10], 15);
50 changes: 50 additions & 0 deletions algorithm_FreeCodeCamp/inventery_Update.js
Original file line number Diff line number Diff line change
@@ -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"]]);
12 changes: 12 additions & 0 deletions algorithm_FreeCodeCamp/largest_Numbers_in_Arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function largestOfFour(arr) {
result = [];
for(var i=0; i<arr.length ; i++){
arr[i].sort(function(a, b){
return b-a;
});
result.push(arr[i][0]);
}
return result;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
11 changes: 11 additions & 0 deletions algorithm_FreeCodeCamp/map_the_Debris.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function orbitalPeriod(arr) {
var GM = 398600.4418;
var earthRadius = 6367.4447;
return arr.map(function(el){
return {name:el.name,
orbitalPeriod:Math.round((2*Math.PI)*Math.sqrt(Math.pow(earthRadius+el.avgAlt,3)/GM))
};
});
}

orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
19 changes: 19 additions & 0 deletions algorithm_FreeCodeCamp/merge_Bird.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function migratoryBirds(n, ar) {
var result = {};
for(var i = 0; i < ar.length; i++) {
if(!result[ar[i]]){
result[ar[i]] = 0;
}
result[ar[i]]++;
}

var ans = result[ar[0]];
for(var k = 0; k< ar.length-1; k++){
if (result[ar[k]] < result[ar[k+1]] ){
ans = ar[k+1];
}
}
return ans;
}

// use !result[ar[i]] to make sure if tehre is an object existed.
9 changes: 9 additions & 0 deletions algorithm_FreeCodeCamp/missing_Letter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function fearNotLetter(str) {
for(var i=0; i<str.length-1 ; i++){
if (str.charCodeAt(i+1) - str.charCodeAt(i)!== 1){
return String.fromCharCode(str.charCodeAt(i)+1);
}
}
}

fearNotLetter("abce");
10 changes: 10 additions & 0 deletions algorithm_FreeCodeCamp/mutations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function mutation(arr) {
var newArr = arr[1].toLowerCase().split("");
for (var i=0 ; i< arr[1].length; i++){
if (arr[0].toLowerCase().indexOf(newArr[i]) == -1){
return false;
}
}
return true;
}
mutation(["floor", "for"]);
Loading