This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Challenge: Rearrange Array Elements so as to form two number such that their sum is maximum. The number of digits in both the numbers cannot differ by more than 1. You're not allowed to use any sorting function. | |
| for e.g. [1, 2, 3, 4, 5] | |
| The expected answer would be [531, 42]. Another expected answer can be [542, 31]. In scenarios such as these when there are more than one possible answers, return any one. | |
| * | |
| ******************************************************************* | |
| Breakdown of my solution: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getIndicesOfItemWeights(arr, limit) { | |
| for (let num in arr) { | |
| var difference = (limit - arr[num]) | |
| //Check if a sum is possible | |
| if (arr[num] <= limit && arr.includes(difference)) { | |
| //Sort index in descending order | |
| if (arr.indexOf(arr[num]) < arr.indexOf(difference)) { | |
| return [arr.indexOf(difference), arr.indexOf(arr[num])] | |
| } | |
| return [arr.indexOf(arr[num]), arr.indexOf(difference)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Desc: 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. | |
| The checkCashRegister() function should always return an object with a status key and a change key. | |
| Full description at => https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/ | |
| */ | |
| function checkCashRegister(price, cash, cid) { | |
| var change = cash - price; //change due | |
| let totalCash = 0; //variable to hold total cash in register |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Return true if the passed string looks like a valid US phone number. | |
| //The area code is required. If the country code is provided, confirm that the country code is 1 | |
| function telephoneCheck(str) { | |
| //^(1\s?)? - checks if country code is provided. Checks for possible spacing | |
| //(\(\d{3}\)|\d{3})[\s\-]? - checks that area code is 3 numbers possibly enclosed in parenthesis and also matches space or hyphen after | |
| //\d{3}[\s\-]?\d{4}$ //checks 7 digit number possibly broken into 3 - 4 format. Ensures str ends in number | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //A function which takes a ROT13 encoded string as input and returns a decoded string. | |
| function rot13(str) { | |
| //declare both alphabets halfs in order | |
| const rotFirstHalf = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"]; | |
| const rotSecondHalf = [ "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ]; | |
| //function to check which half elements lies in | |
| //and return equivalent element |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Convert number to roman numerals | |
| //Create object mapping relevant numbers to roman symbol equivalent | |
| function convertToRoman(num) { | |
| const romanSymbols = { | |
| 1000: 'M', | |
| 900: 'CM', | |
| 500: 'D', | |
| 400: 'CD', | |
| 100: 'C', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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. | |
| */ | |
| function palindrome(str) { | |
| // create new array holding lowercase of str argument | |
| //match all alphanumeric characters (this returns an array of matched characters) | |
| //join array to convert to string | |
| let filtered = str.toLowerCase().match(/[a-z0-9]/gi).join(""); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*Create a function that sums two arguments together. If only one argument is provided, | |
| then return a function that expects one argument and returns the sum. | |
| For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function. | |
| */ | |
| function addTogether() { | |
| let firstNum, secondNum; //numbers to be added | |
| //function to check if argument is a number | |
| function validNumber(num) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Check if the predicate (second argument) is truthy on all elements of a collection (first argument). | |
| In other words, you are given an array collection of objects. | |
| The predicate pre will be an object property and you need to return true if its value is truthy. Otherwise, return false. | |
| In JavaScript, truthy values are values that translate to true when evaluated in a Boolean context. | |
| */ | |
| function truthCheck(collection, pre) { | |
| // Is everyone being true? | |
| for (let obj in collection) { | |
| if (!collection[obj][pre]) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Return an English translated sentence of the passed binary string. The binary string will be space separated. | |
| function binaryAgent(str) { | |
| //split str separated by space then map each binary string to dec | |
| let arr = str.split(" ").map(item => parseInt(item, 2)); | |
| //map each dec to string equivalent and join characters | |
| return arr.map(item => String.fromCharCode(item)).join(""); | |
| } | |
NewerOlder