“Control your external monitor brightness, contrast or volume directly from a menulet or with keyboard native keys.”
Month: February 2021
[JavaScript] Algorithms and Data Structures Projects
These are my notes while doing the course JavaScript Algorithms and Data Structures on https://www.freecodecamp.org. I highly recommend it if you prefer to try things directly rather than watching videos.
1. Palindrome Checker
function palindrome(str) {
// clear string
str = str.toLowerCase().replace(/[^a-z0-9]/g, "");
// reverse string
let strBack = str.split("").reverse().join("");
// palindrome?
return (str === strBack) ? true : false;
}
palindrome("_eye");
palindrome("five|\_/|four");
palindrome("1 eye for of 1 eye.");
2. Roman Numeral Converter
function convertToRoman(num) {
const lookup = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1};
let roman = '';
for ( let i in lookup ) {
while ( num >= lookup[i] ) {
roman += i;
num -= lookup[i];
}
}
return roman;
}
convertToRoman(6);
3. Caesars Cipher
function rot13(str) {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const length = alphabet.length;
let decode = "";
const getROT13 = index => {
return (index+13 >= length) ? alphabet[13-(length-index)] : alphabet[index+13];
}
for (let i=0; i<str.length; i++) {
let index = alphabet.indexOf(str[i]);
decode += (index<0) ? str[i] : getROT13(index);
}
return decode;
}
rot13("SERR PBQR PNZC")
rot13("SERR CVMMN!")
4. Telephone Number Validator
function telephoneCheck(str) {
let regexCheck = /^(1.*){0,1}(\d{3}|[(]\d{3}[)])[\s|-]*\d{3}.*\d{4}$/g;
let result = regexCheck.test(str);
if (result) {
let length = str.match(/\d/g).length;
result = (length == 7 || length == 10 || (length == 11 && str[0] == 1)) ? true : false;
}
console.log(str.match(regexCheck));
console.log(result);
return result;
}
telephoneCheck("555-555-5555");
telephoneCheck("1 555-555-5555");
telephoneCheck("1 (555) 555-5555");
telephoneCheck("5555555555");
telephoneCheck("555-555-5555")
telephoneCheck("(555)555-5555")
telephoneCheck("1(555)555-5555")
telephoneCheck("1 555)555-5555") //false
telephoneCheck("(6054756961)") //false
telephoneCheck("2 (757) 622-7382") //false
telephoneCheck("10 (757) 622-7382") //false
5. Cash Register
function checkCashRegister(price, cash, cid) {
let changeArr = [];
const currency = { "ONE HUNDRED": 100, "TWENTY": 20, "TEN": 10, "FIVE": 5, "ONE": 1, "QUARTER": 0.25, "DIME": 0.1, "NICKEL": 0.05, "PENNY": 0.01 }
const substract = (a, b) => parseFloat(a - b).toPrecision(4);
const calcAmount = (change, curr) => {
let rest = change % curr;
let div = substract(change, rest) / curr;
return div * curr;
}
let change = substract(cash, price);
//check if cid equals to change
//or if cid is already not enough
let sum = 0;
for (let k=0; k<cid.length;k++) {
sum += cid[k][1]
}
if (sum == change) {
return {status: "CLOSED", change: cid};
} else if (sum < change) {
return {status: "INSUFFICIENT_FUNDS", change: []};
}
//calculate change
for (let i in currency) {
if (change > currency[i]) {
for (let j=0; j<cid.length;j++) {
if (i == cid[j][0]) {
let amount = cid[j][1];
if (substract(change, amount) < 0) {
amount = calcAmount(change, currency[i]);
}
change = substract(change, amount);
changeArr.push([i, amount]);
}
}
}
}
if (change != 0) {
//not able to give the correct change
return {status: "INSUFFICIENT_FUNDS", change: []};
}
return {status: "OPEN", change: changeArr};
}
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])