├── .gitignore └── js-algos-and-ds ├── intermediate-algos ├── arguments-optional.js ├── binary-agents.js ├── convert-html-entities.js ├── diff-two-arrays.js ├── dna-pairing.js ├── drop-it.js ├── everything-be-true.js ├── make-a-person.js ├── map-the-debris.js ├── missing-letters.js ├── pig-latin.js ├── search-and-replace.js ├── seek-and-destroy.js ├── smallest-common-multiple.js ├── sorted-union.js ├── spinal-tap-case.js ├── steamroller.js ├── sum-all-numbers-range.js ├── sum-all-odd-fibonaci.js ├── sum-all-primes.js └── wherefor-art-thou.js └── projects ├── caesars-cipher.js ├── cash-register.js ├── palindrome-checker.js ├── roman-numeral-converter.js └── tel-num-validator.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .vscode 3 | 4 | -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/arguments-optional.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Arguments Optional 3 | 4 | Create a function that sums two arguments together. If only one argument is 5 | provided, then return a function that expects one argument and returns the sum. 6 | For example, addTogether(2, 3) should return 5, and addTogether(2) should 7 | return a function. Calling this returned function with a single argument will 8 | then return the sum: 9 | 10 | var sumTwoAnd = addTogether(2); 11 | sumTwoAnd(3) returns 5. 12 | 13 | If either argument isn't a valid number, return undefined. 14 | */ 15 | 16 | function addTogether() { 17 | let args = Array.from(arguments); 18 | if (args.some(elem => typeof elem !== 'number')) { 19 | return undefined; 20 | } else if (args.length == 1) { 21 | return x => addTogether(x, args[0]); 22 | } else { 23 | return args[0] + args[1]; 24 | } 25 | } 26 | 27 | console.log(addTogether(2, 3)); -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/binary-agents.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Binary Agents 3 | Return an English translated sentence of the passed binary string. 4 | The binary string will be space separated. 5 | */ 6 | 7 | function binaryAgent(str) { 8 | return str.split(' ').map(elem => String.fromCharCode(parseInt(elem, 2))).join(''); 9 | } 10 | 11 | console.log(binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111")); -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/convert-html-entities.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Convert HTML Entities 3 | 4 | Convert the characters &, <, >, " (double quote), and ' (apostrophe), 5 | in a string to their corresponding HTML entities. 6 | */ 7 | 8 | function convertHTML(str) { 9 | const entities = { 10 | "&": "&", 11 | "<": "<", 12 | ">": ">", 13 | "\"": """, 14 | "'": "'", 15 | }; 16 | let convertedArr = str.split("").map(function (elem) { 17 | if (entities.hasOwnProperty(elem)) { 18 | return entities[elem]; 19 | } else { 20 | return elem; 21 | } 22 | }); 23 | return convertedArr.join(''); 24 | } 25 | 26 | console.log(convertHTML("Hamburgers < Pizza < Tacos")); 27 | 28 | -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/diff-two-arrays.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Diff Two ArraysPassed 3 | 4 | Compare two arrays and return a new array with any items only found in one of the two given 5 | arrays, but not both. In other words, return the symmetric difference of the two arrays. 6 | 7 | Note: You can return the array with its elements in any order. 8 | */ 9 | 10 | function diffArray(arr1, arr2) { 11 | var newArr = arr1.concat(arr2); 12 | return newArr.filter(elem => !(arr1.includes(elem) && arr2.includes(elem))); 13 | } 14 | 15 | console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5])); -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/dna-pairing.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: DNA Pairing 3 | The DNA strand is missing the pairing element. Take each character, 4 | get its pair, and return the results as a 2d array. Base pairs are 5 | a pair of AT and CG. Match the missing element to the provided 6 | character. Return the provided character as the first element in 7 | each array. 8 | 9 | For example, for the input GCG, return [["G", "C"], 10 | ["C","G"],["G", "C"]] 11 | 12 | The character and its pair are paired up in an array, and all the 13 | arrays are grouped into one encapsulating array. 14 | */ 15 | 16 | function pairElement(str) { 17 | const pairings = { 18 | "A": "T", 19 | "T": "A", 20 | "G": "C", 21 | "C": "G" 22 | } 23 | 24 | return str.split("").map(elem => [elem, pairings[elem]]); 25 | } 26 | 27 | console.log(pairElement("GCG")); 28 | -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/drop-it.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Drop it 3 | 4 | Given the array arr, iterate through and remove each element starting 5 | from the first element (the 0 index) until the function func returns 6 | true when the iterated element is passed through it. 7 | 8 | Then return the rest of the array once the condition is satisfied, 9 | otherwise, arr should be returned as an empty array. 10 | */ 11 | 12 | 13 | function dropElements(arr, func) { 14 | while (arr.length > 0 && !func(arr[0])) { 15 | arr.shift(); 16 | } 17 | return arr; 18 | } 19 | 20 | console.log(dropElements([], function (n) { return n >= 3; })); 21 | -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/everything-be-true.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Everything Be True 3 | 4 | Check if the predicate (second argument) is truthy on all elements 5 | of a collection (first argument). In other words, you are given an 6 | array collection of objects. The predicate pre will be an object 7 | property and you need to return true if its value is truthy. Otherwise, 8 | return false. In JavaScript, truthy values are values that translate 9 | to true when evaluated in a Boolean context. Remember, you can access 10 | object properties through either dot notation or [] notation. 11 | */ 12 | 13 | function truthCheck(collection, pre) { 14 | return collection.every(elem => elem.hasOwnProperty(pre) && Boolean(elem[pre])); 15 | } 16 | 17 | console.log(truthCheck([{ "user": "Tinky-Winky", "sex": "male" }, { "user": "Dipsy", "sex": "male" }, { "user": "Laa-Laa", "sex": "female" }, { "user": "Po", "sex": "female" }], "sex")); -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/make-a-person.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Make a Person 3 | 4 | Fill in the object constructor with the following methods below: 5 | 6 | getFirstName() 7 | getLastName() 8 | getFullName() 9 | setFirstName(first) 10 | setLastName(last) 11 | setFullName(firstAndLast) 12 | 13 | Run the tests to see the expected output for each method. The methods that 14 | take an argument must accept only one argument and it has to be a string. 15 | These methods must be the only available means of interacting with the object. 16 | */ 17 | 18 | 19 | 20 | var Person = function (firstAndLast) { 21 | // Complete the method below and implement the others similarly 22 | this.getFullName = function () { 23 | return firstAndLast; 24 | }; 25 | this.getFirstName = function () { 26 | return firstAndLast.split(" ")[0];; 27 | }; 28 | this.getLastName = function () { 29 | return firstAndLast.split(" ")[1]; 30 | } 31 | this.setFirstName = function (first) { 32 | firstAndLast = first + ' ' + this.getLastName(); 33 | } 34 | this.setLastName = function (last) { 35 | firstAndLast = this.getFirstName() + ' ' + last; 36 | } 37 | this.setFullName = function (fullName) { 38 | firstAndLast = fullName; 39 | } 40 | }; 41 | 42 | var bob = new Person('Bob Ross'); 43 | console.log(bob.getFullName()); 44 | console.log(bob.getFirstName()); 45 | -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/map-the-debris.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Map the Debris 3 | 4 | Return a new array that transforms the elements' average altitude into 5 | their orbital periods (in seconds). The array will contain objects in 6 | the format {name: 'name', avgAlt: avgAlt}. You can read about orbital 7 | periods on Wikipedia. The values should be rounded to the nearest whole 8 | number. The body being orbited is Earth. The radius of the earth is 9 | 6367.4447 kilometers, and the GM value of earth is 398600.4418 km3s-2. 10 | */ 11 | 12 | 13 | function orbitalPeriod(arr) { 14 | var GM = 398600.4418; 15 | var earthRadius = 6367.4447; 16 | return arr.map(function (elem) { 17 | elem.orbitalPeriod = Math.round(2 * Math.PI * Math.sqrt(Math.pow(elem.avgAlt + earthRadius, 3) / GM)); 18 | delete elem.avgAlt; 19 | return elem; 20 | }); 21 | } 22 | 23 | console.log(orbitalPeriod([{ name: "sputnik", avgAlt: 35873.5553 }])); -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/missing-letters.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Missing letters 3 | Find the missing letter in the passed letter range and return it. 4 | 5 | If all letters are present in the range, return undefined. 6 | */ 7 | 8 | function fearNotLetter(str) { 9 | let start = str.charCodeAt(0); 10 | let finish = str.charCodeAt(str.length - 1); 11 | for (let i = start; i <= finish; i++) { 12 | let x = String.fromCharCode(i); 13 | let y = str[i - start]; 14 | if (x !== y) { 15 | return x 16 | } 17 | } 18 | } 19 | 20 | console.log(fearNotLetter("abce")); 21 | -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/pig-latin.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Pig Latin 3 | Pig Latin is a way of altering English Words. The rules are as follows: 4 | - If a word begins with a consonant, take the first consonant or consonant 5 | cluster, move it to the end of the word, and add "ay" to it. 6 | - If a word begins with a vowel, just add "way" at the end. 7 | 8 | Translate the provided string to Pig Latin. Input strings are guaranteed to be 9 | English words in all lowercase. 10 | */ 11 | 12 | 13 | let vowels = ['a', 'e', 'i', 'o', 'u']; 14 | 15 | function translatePigLatin(str) { 16 | let cluster = ''; 17 | 18 | for (let i = 0; i < str.length; i++) { 19 | if (vowels.some(vowel => vowel === str[i])) { 20 | break 21 | } else { 22 | cluster += str[i]; 23 | } 24 | } 25 | 26 | if (cluster === '') { 27 | return str + 'way'; 28 | } else { 29 | return str.slice(cluster.length,) + cluster + 'ay'; 30 | } 31 | } 32 | 33 | console.log(translatePigLatin("consonant")); 34 | 35 | -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/search-and-replace.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Search and Replace 3 | 4 | Perform a search and replace on the sentence using the arguments provided and 5 | return the new sentence. First argument is the sentence to perform the search 6 | and replace on. Second argument is the word that you will be replacing (before). 7 | Third argument is what you will be replacing the second argument with (after). 8 | 9 | Note 10 | Preserve the case of the first character in the original word when you are replacing 11 | it. For example if you mean to replace the word "Book" with the word "dog", it should 12 | be replaced as "Dog" 13 | */ 14 | 15 | function myReplace(str, before, after) { 16 | if (before[0] == before[0].toUpperCase()) { 17 | after = after[0].toUpperCase() + after.slice(1) 18 | } else { 19 | after = after[0].toLowerCase() + after.slice(1) 20 | } 21 | return str.replace(before, after); 22 | } 23 | 24 | console.log(myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped")); 25 | -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/seek-and-destroy.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Seek and Destroy 3 | 4 | You will be provided with an initial array (the first argument in the destroyer function), 5 | followed by one or more arguments. Remove all elements from the initial array that are of 6 | the same value as these arguments. 7 | 8 | Note: You have to use the arguments object. 9 | */ 10 | 11 | 12 | function destroyer(arr) { 13 | let rest; 14 | [, ...rest] = arguments; 15 | return arr.filter(elem => !(rest.includes(elem))); 16 | } 17 | 18 | console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3)); -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/smallest-common-multiple.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Smallest Common Multiple 3 | Find the smallest common multiple of the provided parameters that can 4 | be evenly divided by both, as well as by all sequential numbers in the 5 | range between these parameters. 6 | 7 | The range will be an array of two numbers that will not necessarily be 8 | in numerical order. For example, if given 1 and 3, find the smallest common 9 | multiple of both 1 and 3 that is also evenly divisible by all numbers between 10 | 1 and 3. The answer here would be 6. 11 | */ 12 | 13 | 14 | function computeSCM(num1, num2) { 15 | let a = Math.min(num1, num2); 16 | let b = Math.max(num1, num2); 17 | 18 | for (let m = b; m <= a * b; m += b) { 19 | if (m % a == 0) { 20 | return m; 21 | } 22 | } 23 | } 24 | 25 | function smallestCommons(arr) { 26 | let a = Math.min(...arr); 27 | let b = Math.max(...arr); 28 | let scm = 1; 29 | 30 | for (let i = a; i <= b; i++) { 31 | scm = computeSCM(scm, i); 32 | } 33 | return scm; 34 | } 35 | 36 | 37 | console.log(smallestCommons([2, 10])); 38 | -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/sorted-union.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Sorted Union 3 | 4 | Write a function that takes two or more arrays and returns a new 5 | array of unique values in the order of the original provided arrays. 6 | In other words, all values present from all arrays should be included 7 | in their original order, but with no duplicates in the final array. 8 | 9 | The unique numbers should be sorted by their original order, but the 10 | final array should not be sorted in numerical order.Check the assertion 11 | tests for examples. 12 | */ 13 | 14 | function uniteUnique(arr) { 15 | let allArr = Array.from(arguments).reduce((arr1, arr2) => arr1.concat(arr2)); 16 | let uniqueArr = []; 17 | allArr.forEach(function (elem) { 18 | if (!uniqueArr.includes(elem)) { 19 | uniqueArr.push(elem); 20 | } 21 | }); 22 | return uniqueArr; 23 | } 24 | 25 | console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])); 26 | -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/spinal-tap-case.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Spinal Tap CasePassed 3 | Convert a string to spinal case. Spinal case is 4 | all-lowercase-words-joined-by-dashes. 5 | */ 6 | 7 | function spinalCase(str) { 8 | newStr = str.replace(/([a-z])([A-Z])/g, "$1_$2"); 9 | let regexp = /[\s_]+/g; 10 | return newStr.split(regexp).join("-").toLowerCase(); 11 | } 12 | 13 | console.log(spinalCase("thisIsSpinalTap")); 14 | -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/steamroller.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Steamroller 3 | Flatten a nested array. You must account for varying levels of nesting. 4 | */ 5 | 6 | function steamrollArray(arr, flatArr = []) { 7 | for (let i = 0; i < arr.length; i++) { 8 | if (Array.isArray(arr[i])) { 9 | flatArr = steamrollArray(arr[i], flatArr); 10 | } else { 11 | flatArr.push(arr[i]); 12 | } 13 | } 14 | return flatArr; 15 | } 16 | 17 | console.log(steamrollArray([1, [2], [3, [[4]]]])); -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/sum-all-numbers-range.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Sum All Numbers in a Range 3 | 4 | We'll pass you an array of two numbers. Return the sum of those two numbers 5 | plus the sum of all the numbers between them. The lowest number will not 6 | always come first. 7 | 8 | For example, sumAll([4,1]) should return 10 because sum of all the numbers 9 | between 1 and 4 (both inclusive) is 10. 10 | */ 11 | 12 | 13 | function sumAll(arr) { 14 | let sum = 0; 15 | let a, b; 16 | arr[0] <= arr[1] ? [a, b] = arr : [b, a] = arr; 17 | 18 | for (let num = a; num <= b; num++) { 19 | sum += num 20 | } 21 | 22 | return sum; 23 | } 24 | 25 | console.log(sumAll([1, 4])); 26 | -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/sum-all-odd-fibonaci.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Sum All Odd Fibonacci Numbers 3 | 4 | Given a positive integer num, return the sum of all odd Fibonacci numbers 5 | that are less than or equal to num. The first two numbers in the Fibonacci 6 | sequence are 1 and 1. Every additional number in the sequence is the sum of 7 | the two previous numbers. The first six numbers of the Fibonacci sequence 8 | are 1, 1, 2, 3, 5 and 8. 9 | 10 | For example, sumFibs(10) should return 10 because all odd Fibonacci numbers 11 | less than or equal to 10 are 1, 1, 3, and 5. 12 | */ 13 | 14 | 15 | function sumFibs(num) { 16 | let a = 1 17 | let b = 1; 18 | let sum = a; 19 | 20 | while (b <= num) { 21 | if (b % 2 == 1) { 22 | sum += b; 23 | } 24 | 25 | let temp = b; 26 | b = a + b; 27 | a = temp; 28 | 29 | } 30 | return sum; 31 | } 32 | 33 | console.log(sumFibs(4)); -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/sum-all-primes.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Sum All Primes 3 | 4 | A prime number is a whole number greater than 1 with exactly two divisors: 5 | 1 and itself. For example, 2 is a prime number because it is only divisible 6 | by 1 and 2. In contrast, 4 is not prime since it is divisible by 1, 2 and 4. 7 | 8 | Rewrite sumPrimes so it returns the sum of all prime numbers that are less 9 | than or equal to num. 10 | */ 11 | 12 | 13 | function isPrime(x) { 14 | if (x == 2) { 15 | return true; 16 | } 17 | for (let i = 2; i < x; i++) { 18 | if (x % i === 0) { 19 | return false; 20 | } 21 | } 22 | return true; 23 | } 24 | 25 | 26 | function sumPrimes(num) { 27 | let sum = 0; 28 | 29 | for (let i = 2; i <= num; i++) { 30 | if (isPrime(i)) { 31 | sum += i; 32 | } 33 | } 34 | 35 | return sum; 36 | } 37 | 38 | console.log(sumPrimes(10)); -------------------------------------------------------------------------------- /js-algos-and-ds/intermediate-algos/wherefor-art-thou.js: -------------------------------------------------------------------------------- 1 | /* 2 | Intermediate Algorithm Scripting: Wherefore art thou 3 | Make a function that looks through an array of objects (first argument) and returns 4 | an array of all objects that have matching name and value pairs (second argument). 5 | Each name and value pair of the source object has to be present in the object from 6 | the collection if it is to be included in the returned array. 7 | 8 | For example, if the first argument is [{ first: "Romeo", last: "Montague" }, 9 | { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], 10 | and the second argument is { last: "Capulet" }, then you must return the third object 11 | from the array (the first argument), because it contains the name and its value, that 12 | was passed on as the second argument. 13 | */ 14 | 15 | function whatIsInAName(collection, source) { 16 | var arr = []; 17 | // Only change code below this line 18 | let props = Object.keys(source); 19 | arr = collection.filter(obj => 20 | props.every(prop => 21 | obj.hasOwnProperty(prop) && obj[prop] === source[prop])); 22 | // Only change code above this line 23 | return arr; 24 | } 25 | 26 | console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, 27 | { first: "Mercutio", last: null }, 28 | { first: "Tybalt", last: "Capulet" }], 29 | { last: "Capulet" })); -------------------------------------------------------------------------------- /js-algos-and-ds/projects/caesars-cipher.js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Algorithms and Data Structures Projects: Caesars Cipher 3 | One of the simplest and most widely known ciphers is a Caesar cipher, also 4 | known as a shift cipher. In a shift cipher the meanings of the letters are 5 | shifted by some set amount. A common modern use is the ROT13 cipher, where 6 | the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' 7 | and so on. Write a function which takes a ROT13 encoded string as input and 8 | returns a decoded string. 9 | 10 | All letters will be uppercase. Do not transform any non-alphabetic character 11 | (i.e. spaces, punctuation), but do pass them on. 12 | */ 13 | 14 | function rot13(str) { 15 | const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 16 | let newStr = ''; 17 | for (let i = 0; i < str.length; i++) { 18 | let letterIndex = letters.indexOf(str[i]); 19 | if (letterIndex == -1) { 20 | newStr += str[i]; 21 | } else { 22 | newStr += letters[(letterIndex + 13) % 26]; 23 | } 24 | } 25 | return newStr; 26 | } 27 | 28 | console.log(rot13("SERR PBQR PNZC")); -------------------------------------------------------------------------------- /js-algos-and-ds/projects/cash-register.js: -------------------------------------------------------------------------------- 1 | const currencyUnit = { 2 | "PENNY": 1, 3 | "NICKEL": 5, 4 | "DIME": 10, 5 | "QUARTER": 25, 6 | "ONE": 100, 7 | "FIVE": 500, 8 | "TEN": 1000, 9 | "TWENTY": 2000, 10 | "ONE HUNDRED": 10000 11 | } 12 | 13 | 14 | function checkCashRegister(price, cash, cid) { 15 | 16 | let changeSum = cash * 100 - price * 100; 17 | let changeSumCheck = changeSum; 18 | let change = []; 19 | let status = ''; 20 | 21 | let cidSum = 0; 22 | let filteredCid = cid.filter(elem => elem[1] !== 0).reverse(); 23 | 24 | filteredCid.forEach(elem => { 25 | let curr = elem[0]; 26 | let currSum = elem[1] * 100; 27 | cidSum += currSum; 28 | let amount = 0; 29 | while (changeSum >= currencyUnit[curr] && currSum > 0) { 30 | amount += currencyUnit[curr]; 31 | changeSum -= currencyUnit[curr]; 32 | currSum -= currencyUnit[curr]; 33 | } 34 | if (amount !== 0) { 35 | change.push([curr, amount / 100]); 36 | } 37 | }); 38 | 39 | if (changeSum > 0) { 40 | status = 'INSUFFICIENT_FUNDS'; 41 | change = []; 42 | } else if (changeSum == 0 && changeSumCheck == cidSum) { 43 | status = 'CLOSED'; 44 | change = cid; 45 | } else { 46 | status = 'OPEN'; 47 | } 48 | return { 'status': status, 'change': change }; 49 | } 50 | 51 | console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])); -------------------------------------------------------------------------------- /js-algos-and-ds/projects/palindrome-checker.js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Algorithms and Data Structures Projects: Palindrome Checker 3 | 4 | Return true if the given string is a palindrome. Otherwise, return false. 5 | A palindrome is a word or sentence that's spelled the same way both forward 6 | and backward, ignoring punctuation, case, and spacing. 7 | 8 | Note: You'll need to remove all non-alphanumeric characters (punctuation, 9 | spaces and symbols) and turn everything into the same case (lower or upper 10 | case) in order to check for palindromes. We'll pass strings with varying 11 | formats, such as "racecar", "RaceCar", and "race CAR" among others. We'll 12 | also pass strings with special symbols, such as "2A3*3a2", "2A3 3a2", and 13 | "2_A3*3#A2". 14 | */ 15 | 16 | 17 | function palindrome(str) { 18 | let newStr = str.toLowerCase().replace(/[^a-z\d]/g, ''); 19 | return newStr.split('').reverse().join('') === newStr; 20 | } 21 | 22 | console.log(palindrome("_eye__")); -------------------------------------------------------------------------------- /js-algos-and-ds/projects/roman-numeral-converter.js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Algorithms and Data Structures Projects: Roman Numeral Converter 3 | Convert the given number into a roman numeral. 4 | 5 | All roman numerals answers should be provided in upper-case. 6 | */ 7 | 8 | function convertToRoman(num) { 9 | 10 | const numerals = { 11 | 1: 'I', 12 | 4: 'IV', 13 | 5: 'V', 14 | 9: 'IX', 15 | 10: 'X', 16 | 40: 'XL', 17 | 50: 'L', 18 | 90: 'XC', 19 | 100: 'C', 20 | 400: 'CD', 21 | 500: 'D', 22 | 900: 'CM', 23 | 1000: 'M', 24 | }; 25 | 26 | let romanized = ''; 27 | const decimalKeys = Object.keys(numerals).reverse(); 28 | 29 | decimalKeys.forEach(decimal => { 30 | while (decimal <= num) { 31 | romanized += numerals[decimal]; 32 | num -= decimal; 33 | } 34 | }); 35 | 36 | return romanized; 37 | } 38 | 39 | console.log(convertToRoman(36)); 40 | -------------------------------------------------------------------------------- /js-algos-and-ds/projects/tel-num-validator.js: -------------------------------------------------------------------------------- 1 | /* 2 | JavaScript Algorithms and Data Structures Projects: Telephone Number Validator 3 | 4 | Return true if the passed string looks like a valid US phone number. The user 5 | may fill out the form field any way they choose as long as it has the 6 | format of a valid US number. The following are examples of valid formats for US 7 | numbers (refer to the tests below for other variants): 8 | 9 | 555-555-5555 10 | (555)555-5555 11 | (555) 555-5555 12 | 555 555 5555 13 | 5555555555 14 | 1 555 555 5555 15 | 16 | For this challenge you will be presented with a string such as 800-692-7753 or 17 | 8oo-six427676;laskdjf. Your job is to validate or reject the US phone number based 18 | on any combination of the formats provided above. The area code is required. If the 19 | country code is provided, you must confirm that the country code is 1. Return true 20 | if the string is a valid US phone number; otherwise return false. 21 | */ 22 | 23 | function telephoneCheck(str) { 24 | let regExp = /^(1\s?)?(\d{3}|\(\d{3}\))[\-\s]?\d{3}[\-\s]?\d{4}$/; 25 | return regExp.test(str); 26 | } 27 | 28 | console.log(telephoneCheck("555-555-5555")); 29 | --------------------------------------------------------------------------------