JavaScript Algorithms & Data Structures Project Viewer

Outline of Test Cases

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 same case (lower or upper case) in order to check for palindromes.
We'll pass strings with varying formats, such as "racecar", "RaceCar", and "race CAR" among others.
We'll also pass strings with special symbols, such as "2A3*3a2", "2A3 3a2", and "2_A3*3#A2".

Test Cases

  • palindrome("eye") should return a boolean.
  • palindrome("eye") should return true.
  • palindrome("_eye") should return true.
  • palindrome("race car") should return true.
  • palindrome("not a palindrome") should return false.
  • palindrome("A man, a plan, a canal. Panama") should return true.
  • palindrome("never odd or even") should return true.
  • palindrome("nope") should return false.
  • palindrome("almostomla") should return false.
  • palindrome("My age is 0, 0 si ega ym.") should return true.
  • palindrome("1 eye for of 1 eye.") should return false.
  • palindrome("0_0 (: /-\ :) 0-0") should return true.
  • palindrome("five|\_/|four") should return false.
  • Code

    function palindrome(str)
    {
    var myRegex = /[\W_]/g; //match with any non-word character.
    str = str.toLowerCase(); //put all in lower case
    str = str.replace(myRegex, ''); //replace using myRegex
    for ( var i = 0; i < (str.length-1/2)/2; i++ ) //for loop to find the center of str
    {
    if (str[i] !== str[str.length-1 - i]) //if first half isn't equal to second half, its not a palindrome
    {
    return false;
    }
    }
    return true; //true if first is the same as second half
    }
    palindrome("eye");

    Outline of Test Cases

    Convert the given number into a roman numeral.
    All roman numerals answers should be provided in upper-case.

    Test Cases

  • ConvertToRoman(2) should return "II".
  • convertToRoman(3) should return "III".
  • convertToRoman(4) should return "IV".
  • convertToRoman(5) should return "V".
  • convertToRoman(9) should return "IX".
  • convertToRoman(12) should return "XII".
  • convertToRoman(16) should return "XVI".
  • convertToRoman(29) should return "XXIX".
  • convertToRoman(44) should return "XLIV".
  • convertToRoman(45) should return "XLV".
  • convertToRoman(68) should return "LXVIII".
  • convertToRoman(83) should return "LXXXIII".
  • convertToRoman(97) should return "XCVII".
  • convertToRoman(99) should return "XCIX".
  • convertToRoman(400) should return "CD".
  • convertToRoman(500) should return "D".
  • convertToRoman(501) should return "DI".
  • convertToRoman(649) should return "DCXLIX".
  • convertToRoman(798) should return "DCCXCVIII".
  • convertToRoman(891) should return "DCCCXCI".
  • convertToRoman(1000) should return "M".
  • convertToRoman(1004) should return "MIV".
  • convertToRoman(1006) should return "MVI".
  • convertToRoman(1023) should return "MXXIII".
  • convertToRoman(2014) should return "MMXIV".
  • convertToRoman(3999) should return "MMMCMXCIX".
  • Code

    function convertToRoman(num) {
    var numbers = {1: "I", 2: "II", 3: "III", 4 : "IV", 5: "V", 6: "VI", 7 : "VII", 8 : "VIII", 9 : "IX", 10 : "X", 20 : "XX", 30 : "XXX", 40 : "XL", 50 : "L", 60 : "LX", 70 : "LXX", 80 : "LXXX", 90 : "XC", 100 : "C", 200 : "CC", 300 : "CCC", 400 : "CD", 500 : "D", 600 : "DC", 700 : "DCC", 800 : "DCCC", 900 : "CM", 1000: "M", 2000: "MM", 3000: "MMM"};
    var result = ''; //initialise an empty result string
    var value = String(+num).split(""); //create an empty array
    for(var i =0; i <value.length; i++){
    var lookupKey = value[i]*Math.pow(10,(value.length-i)-1); //using the lookup table to find the key of the current index value that's multiplied by power of 10^i
    if(numbers[lookupKey]){
    result += numbers[lookupKey];
    }
    }
    return result;
    }

    Outline of Test Cases

    One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the 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 which takes a ROT13 encoded string as input and returns a decoded string.
    All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.

    Test Cases

  • rot13("SERR PBQR PNZC") should decode to FREE CODE CAMP
  • rot13("SERR CVMMN!") should decode to FREE PIZZA!
  • rot13("SERR YBIR?") should decode to FREE LOVE?
  • rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.") should decode to THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
  • Code

    function rot13(str) { //Create the alphabet array
    var alphabet =
    ['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'," ", "-", "_", ".", "&","?", "!", "/"]; //Create the coded alphabet array
    var alphabetSwitch =
    ['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', " ", "-", "_", ".", "&","?", "!", "/"]; //Code to do the caesar cipher and switch the string for each letter found
    var caesarsCipher = []; //Create an empty array for the output
    for(let i=0; i<str.length; i++) { //Create the for loop to iterate through all the strings in str
    for(let j=0; j<alphabet.length; j++) {//Create a loop to run through the alphabet array and then:
    if(str[i] === alphabet[j]) { //if the input is the same as the letter in the alphabet array

    caesarsCipher.push(alphabetSwitch[j]); //Then push the new switched string to the caesarsCipher array
    }
    }
    }
    return caesarsCipher.join(""); //return the cipher
    };

    rot13("SERR CVMMN!");

    Outline of Test Cases

    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-5555
    555 555 5555
    5555555555
    1 555 555 5555
    For this challenge you will be presented with a string such as 800-692-7753 or 8oo-six427676;laskdjf. Your job is to validate or reject the US phone number based on any combination of the formats provided above. The area code is required. If the country code is provided, you must confirm that the country code is 1. Return true if the string is a valid US phone number; otherwise return false.

    Test Cases

  • telephoneCheck("555-555-5555") should return a boolean.
  • telephoneCheck("1 555-555-5555") should return true.
  • telephoneCheck("1 (555) 555-5555") should return true.
  • telephoneCheck("5555555555") should return true.
  • telephoneCheck("555-555-5555") should return true.
  • telephoneCheck("(555)555-5555") should return true.
  • telephoneCheck("1(555)555-5555") should return true.
  • telephoneCheck("555-5555") should return false.
  • telephoneCheck("5555555") should return false.
  • telephoneCheck("1 555)555-5555") should return false.
  • telephoneCheck("1 555 555 5555") should return true.
  • telephoneCheck("1 456 789 4444") should return true.
  • telephoneCheck("123**&!!asdf#") should return false.
  • telephoneCheck("55555555") should return false.
  • telephoneCheck("(6054756961)") should return false.
  • telephoneCheck("2 (757) 622-7382") should return false.
  • telephoneCheck("0 (757) 622-7382") should return false.
  • telephoneCheck("-1 (757) 622-7382") should return false.
  • telephoneCheck("2 757 622-7382") should return false.
  • telephoneCheck("10 (757) 622-7382") should return false.
  • telephoneCheck("27576227382") should return false.
  • telephoneCheck("(275)76227382") should return false.
  • telephoneCheck("2(757)6227382") should return false.
  • telephoneCheck("2(757)622-7382") should return false.
  • telephoneCheck("555)-555-5555") should return false.
  • telephoneCheck("(555-555-5555") should return false.
  • telephoneCheck("(555)5(55?)-5555") should return false.
  • Code

    function telephoneCheck(str) { //Create a regex regular expression to validate number
    const myRegex = /^([+]?1[\s]?)?((?:[(](?:[2-9]1[02-9]|[2-9][02-8][0-9])[)][\s]?)|(?:(?:[2-9]1[02-9]|[2-9][02-8][0-9])[\s.-]?)){1}([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2}[\s.-]?){1}([0-9]{4}){1}$/;
    return myRegex.test(str); //Return true ff myRegex passes the test
    }

    telephoneCheck("555-555-5555");

    Outline of Test Cases

    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 and a change key.
    Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return the exact change.
    Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.
    Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order, as the value of the change key.
    Currency Unit Amount
    Penny $0.01 (PENNY)
    Nickel $0.05 (NICKEL)
    Dime $0.1 (DIME)
    Quarter $0.25 (QUARTER)
    Dollar $1 (ONE)
    Five Dollars $5 (FIVE)
    Ten Dollars $10 (TEN)
    Twenty Dollars $20 (TWENTY)
    One-hundred Dollars $100 (ONE HUNDRED)
    See below for an example of a cash-in-drawer array:
    [
    ["PENNY", 1.01],
    ["NICKEL", 2.05],
    ["DIME", 3.1],
    ["QUARTER", 4.25],
    ["ONE", 90],
    ["FIVE", 55],
    ["TEN", 20],
    ["TWENTY", 60],
    ["ONE HUNDRED", 100]
    ]

    Test Cases

  • 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]]) should return an object.
  • 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]]) should return {status: "OPEN", change: [["QUARTER", 0.5]]}.
  • 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]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}.
  • checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.
  • checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.
  • checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}.
  • Code

    function checkCashRegister(price, cash, cid) { //create an object to show base currency unit values all in $
    let cashValue = {
    "PENNY": 0.01, "NICKEL": 0.05, "DIME": 0.1, "QUARTER": 0.25, "ONE": 1, "FIVE": 5, "TEN": 10, "TWENTY": 20, "ONE HUNDRED": 100}
    var change = cash - price; //change due
    let due = {
    change: [], //empty change array
    status: '' //empty status string
    }

    let cashTotal = 0; //variable to hold total cash in register
    cid.forEach(element => { //get the total cash in register
    cashTotal += element[1]; //add the new total
    cashTotal = parseFloat(cashTotal.toFixed(2)) //parse to have a fixed 2 decimal value
    });

    //function to check if currency value can result in some change
    function valueOfCash(value, index) { //unit param-> reference valueOfCash obj and index param-> reference cid unit amount
    let valueTotal = cidr[index][1]; //get currency value total from cid
    let amount = Math.floor(change / cashValue[value]) * cashValue[value]; //check max possible amount that can be offset from change
    if (valueTotal > 0) { //check if some change can be paid from this value
    if (valueTotal >= amount) { //indicates there's enough valueTotal to offset max possible amount
    change -= amount; //subtract amount taken from change
    change = parseFloat(change.toFixed(2)); //parse to have a fixed 2 decimal value
    return amount; //amount taken from this value
    }
    else {
    change -= parseFloat(valueTotal.toFixed(2)); //subtract valueTotal from change since all values were used
    return valueTotal; //amount taken from this value which == full valueTotal
    }
    }
    else { //No change can be taken from this value
    return 0;
    }
    }
    let cidr = [...cid].reverse(); //reverse the cash in drawer

    function getChange() { //implement cashValue() for each case
    let changePayment = cash - price; //full amount of change due
    let arr1 = []; //arr1 to hold each change from currency value
    for (let [index, value] of cidr.entries()) {
    let moneyRecieved = valueOfCash(value[0], index);
    if (moneyRecieved > 0) { //add change amount gotten per value
    arr1.push([value[0], moneyRecieved]);
    }
    if (change === 0) {break;} //break the loop if change is found
    }
    if (cashTotal < changePayment || change !== 0) {
    due.status = "INSUFFICIENT_FUNDS"; //show the status of the cid as insufficient funds since it's less than the change due, or can't return the exact change
    }
    else if (cashTotal == changePayment) {
    due.status = "CLOSED"; //show the status of the cid as closed since it's equal to the change due
    due.change = [...cid];
    }
    else {
    due.status = "OPEN"; //show the status of the cid as open and store the change due in coins and bills
    due.change = [...arr1]; //get the change due from the temp array
    }
    }

    getChange(); //call the function to get the change
    return due; //return the change due, sorted in highest to lowest order, as the value of the change key.
    }
    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]
    ]);