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".
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");
Convert the given number into a roman numeral.
All roman numerals answers should be provided in upper-case.
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;
}
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.
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!");
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.
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");
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]
]
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]
]);