Cash Register

This cash register accepts an array (cid), which holds all available change. The code figures out what change needs to be given, checks if it’s available within the given change(cid) and returns change or relevant message. Change in cid array can be ordered in any fashion as long as inner array structure remains the same.

function checkCashRegister(price, cash, cid) {

  /* 
  price (int) - cost of item
  cash (int) - amount of cash paid with
  cid (array) - cash in drawer ("change");
  */

  // Initialize

  var changeAmount = cash - price;
  changeAmount = Math.round(changeAmount * 100) / 100;
  let totalChangeCash = 0;
  let changeLog = [];

  var cashMachine = {
    price: "£" + price,
    cash: "£" + cash,
    status: "",
    changeTotal: "£" + changeAmount,
    change: []
  }

  // All money in drawer sum up

  for (let i=0; i<cid.length; i++) {
    totalChangeCash += cid[i][1];
  }
  totalChangeCash = Math.round(totalChangeCash * 100) / 100;

  // Main Logic

  if (totalChangeCash < changeAmount) {
    cashMachine.status = "INSUFFICIENT_FUNDS";
  } else if (totalChangeCash == changeAmount) {
    cashMachine.status = "CLOSED";
    cashMachine.change = cid;
  } else {
    if (changeAmount > 0) {

      // £20 Note

      for (let i = 0; i < cid.length; i++) {
        if (cid[i][0] === "TWENTY") {
          if (changeAmount >= 20) {
            let twenties = Math.floor(changeAmount / 20);
    
            if (twenties > 0) {
              if (twenties * 20 <= cid[i][1]) {
                changeAmount -= twenties * 20;
                changeAmount = Math.round(changeAmount * 100) / 100;
                cid[i][1] -= twenties * 20;
                changeLog.push(["TWENTY",twenties * 20]);
              } else {
                if (cid[i][1] > 0) {
                  changeAmount -= cid[i][1];
                  changeLog.push(["TWENTY",cid[i][1]]);
                  cid[i][1] = 0;
                }
              }
            }
          }
        }
      }

      // £10 Note

      for (let i = 0; i < cid.length; i++) {
        if (cid[i][0] === "TEN") {
          if (changeAmount >= 10) {
            let tens = Math.floor(changeAmount / 10);
    
           if (tens > 0) {
              if (tens * 10 <= cid[i][1]) {
                changeAmount -= tens * 10;
                changeAmount = Math.round(changeAmount * 100) / 100;
                cid[i][1] -= tens * 10;
                changeLog.push(["TEN",tens * 10]);
              } else {
                if (cid[i][1] > 0) {
                 changeAmount -= cid[i][1];
                  changeLog.push(["TEN",cid[i][1]]);
                 cid[i][1] = 0;
                }
              }
            }
          }
        }
      }

      // £5 Note

      for (let i = 0; i < cid.length; i++) {
        if (cid[i][0] === "FIVE") {
          if (changeAmount >= 5) {
            let fives = Math.floor(changeAmount / 5);
    
            if (fives > 0) {
              if (fives * 5 <= cid[i][1]) {
                changeAmount -= fives * 5;
                changeAmount = Math.round(changeAmount * 100) / 100;
                cid[i][1] -= fives * 5;
                changeLog.push(["FIVE",fives * 5]);
              } else {
               if (cid[i][1] > 0) {
                  changeAmount -= cid[i][1];
                 changeLog.push(["FIVE",cid[i][1]]);
                  cid[i][1] = 0;
                } 
              }
            }
          }
        }
      }

      // £2 Coin

      for (let i = 0; i < cid.length; i++) { 
        if (cid[i][0] == "TWO") {     
          if (changeAmount >= 2) {
            let twos = Math.floor(changeAmount / 2);
    
            if (twos > 0) {
              twosTotal = twos * 2;
              if (twosTotal <= cid[i][1]) {
                changeAmount -= twosTotal;
                changeAmount = Math.round(changeAmount * 100) / 100;
                cid[i][1] -= twosTotal;
               changeLog.push(["TWO",twosTotal]); 
              } else {
                if (cid[i][1] > 0) {
                  if (changeAmount >= cid[i][1]) {
                    changeAmount -= cid[i][1];
                    changeLog.push(["TWO",cid[i][1]]);
                    cid[i][1] = 0;
                  }
                }
              }
            }
          }
        }
      }

      // £1 Coin

      for (let i = 0; i < cid.length; i++) {
        if (cid[i][0] == "ONE") {
          if (changeAmount >= 1) {
            let ones = Math.floor(changeAmount);

            if (ones > 0) {
              if (ones <= cid[i][1]) {
                changeAmount -= ones;
                changeAmount = Math.round(changeAmount * 100) / 100;
                cid[i][1] -= ones;
                changeLog.push(["ONE", ones]);
              } else {
               if (cid[i][1] > 0) {
                  changeAmount -= cid[i][1];
                  changeLog.push(["ONE",cid[i][1]]);
                  cid[i][1] = 0;
                }
              }
            }
          }
        }
      }

      // £0.5 Coin

      for (let i = 0; i < cid.length; i++) {
        if (cid[i][0] == "50 PENCE") {
          if (changeAmount >= 0.5) {
            let fiftyPence = Math.floor(changeAmount / 0.5);
    
            if (fiftyPence > 0) {
              fiftyPenceTotal = fiftyPence * 0.5;
              if (fiftyPenceTotal <= cid[i][1]) {
                changeAmount -= fiftyPenceTotal;
                changeAmount = Math.round(changeAmount * 100) / 100;
                cid[i][1] -= fiftyPenceTotal;
                changeLog.push(["50 PENCE",fiftyPenceTotal]);
              } else {
                if (cid[i][1] > 0) {
                  changeAmount -= cid[i][1];
                  changeAmount = Math.round(changeAmount * 100) / 100;
                  changeLog.push(["50 PENCE",cid[i][1]]);
                  cid[i][1] = 0;
                }
              }
            }
          }
        }
      }

      // £0.2 Coin

      for (let i = 0; i < cid.length; i++) {
        if (cid[i][0] == "20 PENCE") {
          if (changeAmount >= 0.2) {
            let twentyPence = Math.floor((changeAmount * 0.2));
    
            if (twentyPence > 0) {
              twentyPenceTotal = twentyPence * 0.2;
              if (twentyPenceTotal <= cid[i][1]) {
                changeAmount -= twentyPenceTotal;
                changeAmount = Math.round(changeAmount * 100) / 100;
                cid[i][1] -= twentyPenceTotal;
                changeLog.push(["20 PENCE",twentyPenceTotal]);
              } else {
                if (cid[i][1] > 0) {
                  if (changeAmount - cid[i][1] >= 0) {
                    changeAmount -= cid[i][1];
                    changeAmount = Math.round(changeAmount * 100) / 100;
                    changeLog.push(["20 PENCE",cid[i][1]]);
                    cid[i][1] = 0;
                  }
                }
              }
            }
          }
        }
      }

      // £0.1 Coin

      for (let i = 0; i < cid.length; i++) {
        if (cid[i][0] == "10 PENCE") {
          if (changeAmount >= 0.1) {
            let tenPence = Math.floor(changeAmount / 0.1);

            if (tenPence > 0) {
              let tenPenceTotal = tenPence * 0.1;
              if (tenPenceTotal <= cid[i][1]) {
                changeAmount -= tenPenceTotal;
                changeAmount = Math.round(changeAmount * 100) / 100;
                cid[i][1] -= tenPenceTotal;
                cid[i][1] = Math.round(cid[i][1] * 100) / 100;
                changeLog.push(["10 PENCE",((tenPence * 10) * (0.1 * 10) / (100))]);
              } else {
                if (cid[i][1] > 0) {
                  if (changeAmount - cid[i][1] >= 0) {
                    changeAmount -= cid[i][1];
                    changeAmount = Math.round(changeAmount * 100) / 100;
                    changeLog.push(["10 PENCE",cid[i][1]]);
                    cid[i][1] = 0;
                  }
                }    
              } 
            }
          }
        }
      }

      // £0.05 Coin

      for (let i = 0; i < cid.length; i++) {
        if (cid[i][0] == "5 PENCE") {      
          if (changeAmount >= 0.05) {
            let fivePence = Math.floor(changeAmount / 0.05);

            if (fivePence > 0) {
              let fivePenceTotal = fivePence * 0.05;
              if (fivePenceTotal <= cid[i][1]) {
                changeAmount -= fivePenceTotal;
                changeAmount = Math.round(changeAmount * 100) / 100;
                cid[i][1] -= fivePenceTotal;
                cid[i][1] = Math.round(cid[i][1] * 100) / 100;
                changeLog.push(["5 PENCE",((fivePence * 10) * (0.05 * 10) / (100))]);
              } else {
                if (cid[i][1] > 0) {
                  changeAmount -= cid[i][1];
                  changeAmount = Math.round(changeAmount * 100) / 100;
                  changeLog.push(["5 PENCE",cid[i][1]]);
                  cid[i][1] = 0;
                }
              }
            }
          }
        }
      }

      // £0.02 Coin

      for (let i = 0; i < cid.length; i++) {
        if (cid[i][0] == "2 PENCE") {      
          if (changeAmount >= 0.02) {
            let twoPence = Math.floor(changeAmount / 0.02);

            if (twoPence > 0) {
              let twoPenceTotal = twoPence * 0.02;
              if (twoPenceTotal <= cid[i][1]) {
                changeAmount -= twoPenceTotal;
                changeAmount = Math.round(changeAmount * 100) / 100;
                cid[i][1] -= twoPenceTotal;
                cid[i][1] = Math.round(cid[i][1] * 100) / 100;
                changeLog.push(["2 PENCE",((twoPence * 10) * (0.02 * 10) / (100))]);
              } else {
                if (cid[i][1] > 0) {
                  changeAmount -= cid[i][1];
                  changeAmount = Math.round(changeAmount * 100) / 100;
                  changeLog.push(["2 PENCE",cid[i][1]]);
                  cid[i][1] = 0;
                }
              }
            }
          }
        }
      }

      // £0.01 Coin

      for (let i = 0; i < cid.length; i++) {
        if (cid[i][0] == "1 PENCE") {      
          if (changeAmount >= 0.01) {
            let onePence = Math.floor(changeAmount / 0.01);

            if (onePence > 0) {
              let onePenceTotal = onePence * 0.01;
              if (onePenceTotal <= cid[i][1]) {
                changeAmount -= onePenceTotal;
                changeAmount = Math.round(changeAmount * 100) / 100;
                cid[i][1] -= onePenceTotal;
                cid[i][1] = Math.round(cid[i][1] * 100) / 100;
                changeLog.push(["1 PENCE",((onePence * 10) * (0.01 * 10) / (100))]);
              } else {
                if (cid[i][1] > 0) {
                  changeAmount -= cid[i][1];
                  changeAmount = Math.round(changeAmount * 100) / 100;
                  changeLog.push(["1 PENCE",cid[i][1]]);
                  cid[i][1] = 0;
                }
              }
            }
          }
        }
      }
    }

  
  if (changeAmount == 0) {
    cashMachine.status = "OPEN";
    cashMachine.change = changeLog;
  } else {
    cashMachine.status = "INSUFFICIENT FUNDS";
    cashMachine.change = [];
    console.log("Sorry but you didn't pay enough. You need to pay £" 
	+ (price - cash) + " more.")
    }
  }
  return cashMachine;
}

// Example cash-in-drawer array:
// [
// ["1 PENCE", 0.10],
// ["2 PENCE", 0.80],
// ["5 PENCE", 1.00],
// ["10 PENCE", 2.1],
// ["20 PENCE", 3.2],
// ["50 PENCE", 4.0],
// ["ONE", 90],
// ["TWO", 52],
// ["FIVE", 20],
// ["TEN", 60],
// ["TWENTY", 100]
// ]

checkCashRegister(119, 118, [["1 PENCE", 2.0],["2 PENCE", 2.0],
["5 PENCE", 2.0], ["10 PENCE", 10.0], ["20 PENCE", 5.0], ["50 PENCE", 10.0], 
["ONE", 20], ["TWO", 20], ["FIVE", 50], ["TEN", 50], ["TWENTY", 100]]);

American Telephone Number Validator

function telephoneCheck(str) {
  let flag = false;

  let regArr = [
    /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/,
    /^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$/,
    /^\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}$/,
    /^[0-9]{3}\s[0-9]{3}\s[0-9]{4}$/,
    /^[0-9]{10}$/,
    /^1\s[0-9]{3}\s[0-9]{3}\s[0-9]{4}$/,
    /^1\s[0-9]{3}-[0-9]{3}-[0-9]{4}$/,
    /^1\s\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}$/,
    /^1\([0-9]{3}\)[0-9]{3}-[0-9]{4}$/
    ];

  for (let i = 0; i < regArr.length; i++) {
    if (str.match(regArr[i])) {
      flag = true;
    }
  }
  return flag;
}

telephoneCheck("5434545432");

Caesar’s Cipher

Romans cipher was simply a shift by a certain number of letters. In case below, it’s a shift by 13 letters forward.

function rot13(str) {
  let encryptedArr = str.split("");
  let letters = /^[a-zA-Z]+$/;
  
  let alphabetArr =
  ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R",
  "S","T","U","V","W","X","Y","Z"];
  let shiftedAlphabetArr =
  ["N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E",
  "F","G","H","I","J","K","L","M"];

  let decypheredArr = [...encryptedArr];

  for (let i = 0; i < encryptedArr.length; i++) {
    if(encryptedArr[i].match(letters)) {
      decypheredArr[i] = shiftedAlphabetArr[alphabetArr.findIndex
	  (a => a == encryptedArr[i])];
    }
  }

  let result = decypheredArr.join("");
  console.log("Encrypted Message: " + str);
  console.log("Decrypted Message: " + result);

  return result;
}

rot13("SERR PBQR PNZC");

Roman Numeral Converter

function convertToRoman(num) {
    /*

    This program converts conventional number 
	supplied as parameter, into roman numeral.

    Examples:
        convertToRoman(891); -> 'DCCCXCI'
        convertToRoman(1); -> 'I'
        convertToRoman(4); -> 'IV'

    */

  let romanAlphabetArr = [
    ["I","II","III","IV","V","VI","VII","VIII","IX","X"],
    ["X","XX","XXX","XL","L","LX","LXX","LXXX","XC","C"],
    ["C","CC","CCC","CD","D","DC","DCC","DCCC","CM","M"],
    ["M","MM","MMM","MMMM"]
  ];

  let digits = num.toString().length;
  let romanNumArr = [];

  for (let i = 0; i < digits; i++) {
    
    let currentDigit = num % 10;

    if (currentDigit === 0) {
      num = Math.floor(num /= 10);
      continue;
    }

    romanNumArr.push(romanAlphabetArr[i][currentDigit - 1]);
    num = Math.floor(num /= 10);

  }
  romanNumArr = romanNumArr.reverse().join("");

 return romanNumArr;
}

convertToRoman(891);