Skip to content

Instantly share code, notes, and snippets.

@tianmingzuo
tianmingzuo / checkMixedGroupingChars.js
Created October 23, 2019 02:29
Check For Mixed Grouping of Characters: If you want to find either Penguin or Pumpkin in a string, you can use the following Regular Expression: /P(engu|umpk)in/g: let testStr = "Pumpkin"; let testRegex = /P(engu|umpk)in/g; testRegex.test(testStr); Fix the regex so that it checks for the names of Franklin Roosevelt or Eleanor Roosevelt in a case…
let myString = "Eleanor Roosevelt";
let myRegex = /(Franklin ([A-Z]\. )?|Eleanor ([A-Z]\. )?)Roosevelt/;
let result = myRegex.test(myString);
@tianmingzuo
tianmingzuo / pairwise.js
Created October 18, 2019 16:29
Pairwise: Given an array arr, find element pairs whose sum equal the second argument arg and return the sum of their indices. You may use multiple pairs that have the same numeric elements but different indices. Each pair should use the lowest possible available indices. Once an element has been used it cannot be reused to pair with another elem…
function pairwise(arr, arg) {
let sum = 0;
for(let i=0; i<arr.length-1; i++){
for(let j=i+1; j<arr.length; j++){
if(arr[i] + arr[j] === arg){
sum += (i+j);
arr[i] = arg + 1; // to avoid the arr[i] being reused
arr[j] = arg + 1; // to avoid the arr[j] being reused
}
}
@tianmingzuo
tianmingzuo / inventoryUpdate.js
Created October 17, 2019 17:05
Inventory Update: Compare and update the inventory stored in a 2D array against a second 2D array of a fresh delivery. Update the current existing inventory item quantities (in arr1). If an item cannot be found, add the new item and quantity into the inventory array. The returned inventory array should be in alphabetical order by item.
function updateInventory(arr1, arr2) {
let updatedArr = [];
if(arr1 === []){
updatedArr = arr2;
}else if(arr2 === []) {
updatedArr = arr1;
}else{
let newItemArr = [];
@tianmingzuo
tianmingzuo / cashRegister2.js
Created October 15, 2019 03:27
Cash Register: Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument. cid is a 2D array listing available currency. The checkCashRegister()function should always return an object with a status key …
function checkCashRegister(price, cash, cid) {
let billArr = [["ONE HUNDRED", 100], ["TWENTY", 20], ["TEN", 10], ["FIVE", 5],
["ONE", 1], ["QUARTER", 0.25], ["DIME", 0.1], ["NICKEL", 0.05],
["PENNY", 0.01]];
let changeDue = cash - price;
let sumCash = 0;
for(let i=0; i<cid.length; i++){
sumCash += cid[i][1];
@tianmingzuo
tianmingzuo / cashRegister.js
Created October 14, 2019 23:16
Cash Register: Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument. cid is a 2D array listing available currency. The checkCashRegister()function should always return an object with a status key …
function checkCashRegister(price, cash, cid) {
let billArr = [["ONE HUNDRED", 100], ["TWENTY", 20], ["TEN", 10], ["FIVE", 5],
["ONE", 1], ["QUARTER", 0.25], ["DIME", 0.1], ["NICKEL", 0.05],
["PENNY", 0.01]];
let changeDue = cash - price;
let sumCash = 0;
for(let i=0; i<cid.length; i++){
sumCash += cid[i][1];
@tianmingzuo
tianmingzuo / phoneNumValidator.js
Created October 14, 2019 20:27
Telephone Number Validator: Return true if the passed string looks like a valid US phone number. The user may fill out the form field any way they choose as long as it has the format of a valid US number. The following are examples of valid formats for US numbers (refer to the tests below for other variants): 555-555-5555 (555)555-5555 (555) 555…
function telephoneCheck(str) {
// Good luck!
let regex = /^([1]\s?)?(\([0-9]{3}\)|[0-9]{3})(\s|-)?[0-9]{3}(\s|-)?[0-9]{4}$/g;
return regex.test(str);
}
telephoneCheck("555-555-5555");
/*
telephoneCheck("1 555-555-5555")should return true.
@tianmingzuo
tianmingzuo / caesarsCipher.js
Created October 14, 2019 16:31
Caesars Cipher: One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipherthe meanings of the letters are shifted by some set amount. A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on. Write a function w…
function rot13(str) { // LBH QVQ VG!
let newStr = '';
for(let i=0; i<str.length; i++){
let asciiCode = str.charCodeAt(i);
if(asciiCode>=78 && asciiCode <=90){
asciiCode = asciiCode - 13;
}else if(asciiCode <78 && asciiCode >= 65){
asciiCode = asciiCode + 13;
}
newStr += String.fromCharCode(asciiCode);
@tianmingzuo
tianmingzuo / romanNumConverter.js
Created October 14, 2019 03:11
Roman Numeral Converter: Convert the given number into a roman numeral. All roman numerals answers should be provided in upper-case.
function convertToRoman(num) {
let roman = '';
let roman1 = '';
let roman10 = '';
let roman100 = '';
let roman1000 = '';
let numStr = '' + num;
if(num<10){
switch(numStr[0]){
case '1': roman1 = 'I'; break;
@tianmingzuo
tianmingzuo / palindromeChecker2.js
Created October 14, 2019 01:59
Palindrome Checker: Return true if the given string is a palindrome. Otherwise, return false. A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing. Note You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into the …
function palindrome(str) {
// Good luck!
let newStr = str.replace(/\W|_/g, '');
newStr = newStr.toLowerCase();
let revStr = newStr.split('').reverse().join('');
if(newStr === revStr){
return true;
}
return false;
}
@tianmingzuo
tianmingzuo / palindromeChecker.js
Created October 14, 2019 01:55
Palindrome Checker: Return true if the given string is a palindrome. Otherwise, return false. A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing. Note You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into the …
function palindrome(str) {
let newStr = str.replace(/\W/g, '');
newStr = newStr.replace('_', '').toLowerCase();
let revStr = newStr.split('').reverse().join('');
if(newStr === revStr){
return true;
}
return false;
}