├── abc.html ├── 20_addTwoNumbers.js ├── 46_ANeddleInTheHaystack.js ├── 4_convertTimeInput.js ├── 42_sumOfLowestPositiveIntegers.js ├── 49_isThisATriangle.js ├── 44_countTheDivisorsOfNumber.js ├── 45_findTheParityQutlier.js ├── 31_FactorialNumber.js ├── 1_randomNumber.js ├── 50_gettheMiddleChar.js ├── 2_reverse.js ├── 16_askTheBob.js ├── 7_expressionToValidInput.js ├── 11_N-thValueOfFibonacci.js ├── 9_objectIsEmpty.js ├── 37_itsALeapYear.js ├── 29_mumbling.js ├── 35_nthLargestElementFromUnsortedArray.js ├── 47_Isograms.js ├── 6_truncateString.js ├── 26_findTheOddInt.js ├── 33_uniqueValueFromAnArray.js ├── 25_stopGninnipsMysdrow.js ├── 41_CategorizeNewMember.js ├── 22_UniqueInOrder.js ├── 5_findCharbetweenStartEnd.js ├── 18_calGrainsOnASquareOnChessboard.js ├── 27_vowelCount.js ├── 43_highestScoringWord.js ├── 8_diffBtentwoValidDate.js ├── 28_welrDStringCase.js ├── 32_gcdTwoPositiveNumbers.js ├── 36_RnaTranscriptionInstr.js ├── 3_reverseInt.js ├── 10_removeArrayBaseOnObject.js ├── 21_UnionOfTwoArrays.js ├── 23_EqualSidesOfAnArray.js ├── 30_MexicanWave.js ├── 24_numberInExpandedForm.js ├── 40_vehiclePurchaseInstructions.js ├── 38_LuhnAlgoInstructions.js ├── 17_longestConsecutiveSequence.js ├── 19_resistorColorMap.js ├── 14_convertArrayDigitBaseToAnotherBase.js ├── 34_mostFreqItemOfArray.js ├── 48_HumanReadableDurationFormat.js ├── 13_covertSecondsToSpaceAge.js ├── 39_MixedJuicesInstructions.js ├── 15_determinesentenceIsAPangram.js └── 12_givenNumberSpellOut.js /abc.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /20_addTwoNumbers.js: -------------------------------------------------------------------------------- 1 | const addTwoNumbers = (a, b) => { 2 | // code here 3 | 4 | return a + b; 5 | 6 | } 7 | -------------------------------------------------------------------------------- /46_ANeddleInTheHaystack.js: -------------------------------------------------------------------------------- 1 | function findNeedle(haystack) { 2 | // your code here 3 | return "found the needle at position " + haystack.indexOf("needle"); 4 | } 5 | -------------------------------------------------------------------------------- /4_convertTimeInput.js: -------------------------------------------------------------------------------- 1 | const time = '12:10AM'; 2 | function convertTo24HrsFormat(time) { 3 | 4 | } 5 | 6 | console.log(`Converted time: ${convertTo24HrsFormat(time)}`) 7 | -------------------------------------------------------------------------------- /42_sumOfLowestPositiveIntegers.js: -------------------------------------------------------------------------------- 1 | function sumTwoSmallestNumbers(numbers) { 2 | // code goes here 3 | numbers.sort((a, b) => a - b); 4 | return numbers[0] + numbers[1]; 5 | } 6 | -------------------------------------------------------------------------------- /49_isThisATriangle.js: -------------------------------------------------------------------------------- 1 | function isTriangle(a, b, c) { 2 | // your code here 3 | if (a + b > c && a + c > b && b + c > a) { 4 | return true; 5 | } else { 6 | return false; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /44_countTheDivisorsOfNumber.js: -------------------------------------------------------------------------------- 1 | function getDivisorsCnt(num) { 2 | // code below 3 | let cnt = 0; 4 | for (let i = 1; i <= num; i++) { 5 | if (num % i === 0) { 6 | cnt++; 7 | } 8 | } 9 | return cnt; 10 | } 11 | -------------------------------------------------------------------------------- /45_findTheParityQutlier.js: -------------------------------------------------------------------------------- 1 | function findOutlier(integers) { 2 | //your code here 3 | let even = integers.filter((x) => x % 2 === 0); 4 | let odd = integers.filter((x) => x % 2 !== 0); 5 | return even.length === 1 ? even[0] : odd[0]; 6 | } 7 | -------------------------------------------------------------------------------- /31_FactorialNumber.js: -------------------------------------------------------------------------------- 1 | function factorial(n) { 2 | // write your code here 3 | if (n === 0) { 4 | return 1; 5 | } 6 | return n * factorial(n - 1); 7 | } 8 | 9 | let n = 4; 10 | console.log("The factorial of " + n + " is " + factorial(n)); 11 | -------------------------------------------------------------------------------- /1_randomNumber.js: -------------------------------------------------------------------------------- 1 | function randomNumberGeneratorInRange(rangeStart, rangeEnd) { 2 | // write your solution here 3 | 4 | return Math.random() * (rangeEnd - rangeStart) + rangeStart; 5 | } 6 | 7 | console.log(`My random number: ${randomNumberGeneratorInRange(5, 100)}`); 8 | -------------------------------------------------------------------------------- /50_gettheMiddleChar.js: -------------------------------------------------------------------------------- 1 | function getMiddle(s) { 2 | // your code here 3 | var middle = s.length / 2; 4 | if (s.length % 2 === 0) { 5 | return s.substring(middle - 1, middle + 1); 6 | } else { 7 | return s.substring(middle, middle + 1); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /2_reverse.js: -------------------------------------------------------------------------------- 1 | const str = "JavaScript is awesome" 2 | 3 | function reverseAString(str) { 4 | // write your solution here 5 | 6 | return str.split("").reverse().join(""); 7 | 8 | } 9 | 10 | console.log(`Reversed string is: ${reverseAString(str)}`) 11 | -------------------------------------------------------------------------------- /16_askTheBob.js: -------------------------------------------------------------------------------- 1 | function hey(message) { 2 | if (message.endsWith('?')) { 3 | return 'Sure.'; 4 | } 5 | 6 | if (message.endsWith('!')) { 7 | return 'Whoa, chill out!'; 8 | } 9 | 10 | if (message.trim() === '') { 11 | return 'Fine. Be that way!'; 12 | } 13 | 14 | return 'Whatever.'; 15 | } 16 | -------------------------------------------------------------------------------- /7_expressionToValidInput.js: -------------------------------------------------------------------------------- 1 | const number = '+919876543210'; 2 | 3 | function validateMobile(number) { 4 | let regex = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im; 5 | return regex.test(number); 6 | } 7 | 8 | console.log(`is a valid Indian mobile number: ${validateMobile(number)}`) 9 | -------------------------------------------------------------------------------- /11_N-thValueOfFibonacci.js: -------------------------------------------------------------------------------- 1 | function fibonacci(n) { 2 | // write your solution here 3 | if (n === 0) { 4 | return 0; 5 | } 6 | if (n === 1) { 7 | return 1; 8 | } 9 | return fibonacci(n - 1) + fibonacci(n - 2); 10 | } 11 | 12 | console.log(`fibonacci value at position 5: ${fibonacci(5)}`) 13 | -------------------------------------------------------------------------------- /9_objectIsEmpty.js: -------------------------------------------------------------------------------- 1 | const obj = { key: 1 }; 2 | 3 | function isEmpty(obj) { 4 | // write your solution here 5 | for (let key in obj) { 6 | if (obj.hasOwnProperty(key)) { 7 | return false; 8 | } 9 | } 10 | return true; 11 | } 12 | 13 | console.log(`is empty object: ${isEmpty(obj)}`) 14 | -------------------------------------------------------------------------------- /37_itsALeapYear.js: -------------------------------------------------------------------------------- 1 | const isLeap = (year) => { 2 | // code here 3 | if (year % 4 === 0) { 4 | if (year % 100 === 0) { 5 | if (year % 400 === 0) { 6 | return true; 7 | } else { 8 | return false; 9 | } 10 | } else { 11 | return true; 12 | } 13 | } else { 14 | return false; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /29_mumbling.js: -------------------------------------------------------------------------------- 1 | function accum(s) { 2 | // your code goes below 3 | var result = ""; 4 | for (var i = 0; i < s.length; i++) { 5 | result += s[i].toUpperCase(); 6 | for (var j = 0; j < i; j++) { 7 | result += s[i].toLowerCase(); 8 | } 9 | if (i < s.length - 1) { 10 | result += "-"; 11 | } 12 | } 13 | return result; 14 | } 15 | -------------------------------------------------------------------------------- /35_nthLargestElementFromUnsortedArray.js: -------------------------------------------------------------------------------- 1 | function nthlargest(arr, highest) { 2 | // write your code here 3 | var sorted = arr.sort(function (a, b) { 4 | return a - b; 5 | }); 6 | return sorted[sorted.length - highest]; 7 | } 8 | 9 | const arr = [43, 56, 23, 89, 88, 90, 99, 652]; 10 | const highest = 4; 11 | 12 | console.log(nthlargest(arr, highest)); 13 | -------------------------------------------------------------------------------- /47_Isograms.js: -------------------------------------------------------------------------------- 1 | function isIsogram(str) { 2 | // your code here 3 | var str = str.toLowerCase(); 4 | var arr = str.split(""); 5 | var result = true; 6 | for (var i = 0; i < arr.length; i++) { 7 | for (var j = i + 1; j < arr.length; j++) { 8 | if (arr[i] === arr[j]) { 9 | result = false; 10 | } 11 | } 12 | } 13 | return result; 14 | } 15 | -------------------------------------------------------------------------------- /6_truncateString.js: -------------------------------------------------------------------------------- 1 | const str = 'JavaScript is simple but not easy to master'; 2 | const wordLimit = 3 3 | 4 | function truncateWithWordLimit(str, wordLimit) { 5 | const words = str.split(' '); 6 | const newWords = words.slice(0, wordLimit); 7 | return newWords.join(' '); 8 | } 9 | 10 | console.log(`Truncated string: ${truncateWithWordLimit(str, wordLimit)}`) 11 | -------------------------------------------------------------------------------- /26_findTheOddInt.js: -------------------------------------------------------------------------------- 1 | function findOdd(arr) { 2 | //happy coding! 3 | 4 | let count = {}; 5 | for (let i = 0; i < arr.length; i++) { 6 | if (count[arr[i]]) { 7 | count[arr[i]]++; 8 | } else { 9 | count[arr[i]] = 1; 10 | } 11 | } 12 | for (let key in count) { 13 | if (count[key] % 2 !== 0) { 14 | return Number(key); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /33_uniqueValueFromAnArray.js: -------------------------------------------------------------------------------- 1 | function set(arrOfNum) { 2 | // write your code here 3 | let unique = []; 4 | for (let i = 0; i < arrOfNum.length; i++) { 5 | if (unique.indexOf(arrOfNum[i]) === -1) { 6 | unique.push(arrOfNum[i]); 7 | } 8 | } 9 | return unique; 10 | } 11 | 12 | const arrOfNum = [1, 2, 2, 4, 5, 6, 6]; 13 | 14 | console.log("result is + " + set(arrOfNum)); 15 | -------------------------------------------------------------------------------- /25_stopGninnipsMysdrow.js: -------------------------------------------------------------------------------- 1 | function spinWords(string) { 2 | //TODO Have fun :) 3 | var words = string.split(" "); 4 | var newWords = []; 5 | for (var i = 0; i < words.length; i++) { 6 | if (words[i].length >= 5) { 7 | newWords.push(words[i].split("").reverse().join("")); 8 | } else { 9 | newWords.push(words[i]); 10 | } 11 | } 12 | return newWords.join(" "); 13 | } 14 | -------------------------------------------------------------------------------- /41_CategorizeNewMember.js: -------------------------------------------------------------------------------- 1 | function openOrSenior(data) { 2 | // your code goes below 3 | return data.map(function (item) { 4 | if (item[0] >= 55 && item[1] > 7) { 5 | return "Senior"; 6 | } else { 7 | return "Open"; 8 | } 9 | }); 10 | } 11 | 12 | let output = openOrSenior([ 13 | [45, 12], 14 | [55, 21], 15 | [19, -2], 16 | [104, 20], 17 | ]); 18 | 19 | console.log(output); 20 | -------------------------------------------------------------------------------- /22_UniqueInOrder.js: -------------------------------------------------------------------------------- 1 | let uniqueInOrder = (iterable) => { 2 | //your code here - remember iterable can be a string or an array 3 | let result = []; 4 | let prev = null; 5 | for (let i = 0; i < iterable.length; i++) { 6 | 7 | if (iterable[i] !== prev) { 8 | result.push(iterable[i]); 9 | prev = iterable[i]; 10 | } 11 | } 12 | return result; 13 | }; 14 | -------------------------------------------------------------------------------- /5_findCharbetweenStartEnd.js: -------------------------------------------------------------------------------- 1 | const str = 'XeroX'; 2 | 3 | function getTheGapX(str) { 4 | 5 | let startIndex = str.indexOf('X'); 6 | let endIndex = str.lastIndexOf('X'); 7 | 8 | if (startIndex > endIndex) { 9 | return -1; 10 | } 11 | return endIndex - startIndex; 12 | } 13 | 14 | 15 | 16 | console.log(`Gap between the X's: ${getTheGapX(str)}`) 17 | 18 | 19 | -------------------------------------------------------------------------------- /18_calGrainsOnASquareOnChessboard.js: -------------------------------------------------------------------------------- 1 | const totalGrains = () => { 2 | // Code here 3 | 4 | var ans = BigInt(2 ** 64); 5 | return ans - 1n; 6 | }; 7 | 8 | const grainsOn = (input) => { 9 | // Code here 10 | 11 | var ans = BigInt(2 ** (input - 1)); 12 | return ans; 13 | }; 14 | 15 | console.log(`Grains on 5th square: ${grainsOn(62)}`); 16 | console.log(`Total grains on the Chess Board: ${totalGrains()}`); 17 | -------------------------------------------------------------------------------- /27_vowelCount.js: -------------------------------------------------------------------------------- 1 | function getCount(str) { 2 | let vowelsCount = 0; 3 | // enter your magic here 4 | const vowels = ["a", "e", "i", "o", "u"]; 5 | const strArray = str.toLowerCase().split(""); 6 | for (let i = 0; i < strArray.length; i++) { 7 | if (vowels.includes(strArray[i])) { 8 | vowelsCount++; 9 | } 10 | } 11 | return vowelsCount; 12 | } 13 | 14 | console.log(getCount("abracadabra")); 15 | -------------------------------------------------------------------------------- /43_highestScoringWord.js: -------------------------------------------------------------------------------- 1 | function high(x) { 2 | //code your magic here 3 | let arr = x.split(" "); 4 | let max = 0; 5 | let maxWord = ""; 6 | for (let i = 0; i < arr.length; i++) { 7 | let sum = 0; 8 | for (let j = 0; j < arr[i].length; j++) { 9 | sum += arr[i].charCodeAt(j) - 96; 10 | } 11 | if (sum > max) { 12 | max = sum; 13 | maxWord = arr[i]; 14 | } 15 | } 16 | return maxWord; 17 | } 18 | -------------------------------------------------------------------------------- /8_diffBtentwoValidDate.js: -------------------------------------------------------------------------------- 1 | const DAY_IN_MILLISECONDS = 1000 * 60 * 60 * 24; 2 | 3 | function getDaysBetweenDates(dateText1, dateText2) { 4 | const date1 = new Date(dateText1); 5 | const date2 = new Date(dateText2); 6 | const timeDiff = Math.abs(date2.getTime() - date1.getTime()); 7 | return Math.ceil(timeDiff / DAY_IN_MILLISECONDS); 8 | } 9 | 10 | 11 | console.log(`Days difference: ${getDaysBetweenDates('10/15/2020', '12/1/2020')}`) 12 | -------------------------------------------------------------------------------- /28_welrDStringCase.js: -------------------------------------------------------------------------------- 1 | function toWeirdCase(string) { 2 | // Your code goes here 3 | var newString = ""; 4 | for (var i = 0; i < string.length; i++) { 5 | if (i % 2 === 0) { 6 | newString += string[i].toUpperCase(); 7 | } else { 8 | newString += string[i].toLowerCase(); 9 | } 10 | } 11 | return newString; 12 | } 13 | 14 | console.log( 15 | `The weird case of ${"A test case"} is ${toWeirdCase("A test case")}` 16 | ); 17 | -------------------------------------------------------------------------------- /32_gcdTwoPositiveNumbers.js: -------------------------------------------------------------------------------- 1 | function gcd(a, b) { 2 | // write your code here 3 | if (a === 0) { 4 | return b; 5 | } 6 | if (b === 0) { 7 | return a; 8 | } 9 | if (a === b) { 10 | return a; 11 | } 12 | if (a > b) { 13 | return gcd(a - b, b); 14 | } else { 15 | return gcd(a, b - a); 16 | } 17 | } 18 | 19 | const a = 2154; 20 | const b = 458; 21 | 22 | console.log("The GCD of " + a + " ", b + " is " + gcd(a, b)); 23 | -------------------------------------------------------------------------------- /36_RnaTranscriptionInstr.js: -------------------------------------------------------------------------------- 1 | const transcription = (dna) => { 2 | // code here 3 | let rna = ""; 4 | for (let i = 0; i < dna.length; i++) { 5 | switch (dna[i]) { 6 | case "G": 7 | rna += "C"; 8 | break; 9 | case "C": 10 | rna += "G"; 11 | break; 12 | case "T": 13 | rna += "A"; 14 | break; 15 | case "A": 16 | rna += "U"; 17 | break; 18 | } 19 | } 20 | return rna; 21 | }; 22 | -------------------------------------------------------------------------------- /3_reverseInt.js: -------------------------------------------------------------------------------- 1 | const num = 3849; 2 | 3 | function reverseGivenInteger(num) { 4 | // write your solution here 5 | let reversedNum = 0; 6 | while (num > 0) { 7 | reversedNum = reversedNum * 10 + num % 10; 8 | num = Math.floor(num / 10); 9 | } 10 | return reversedNum; 11 | 12 | // return num.toString().split('').reverse().join(''); 13 | 14 | 15 | } 16 | 17 | console.log(`Reversed integer is: ${reverseGivenInteger(num)}`) 18 | -------------------------------------------------------------------------------- /10_removeArrayBaseOnObject.js: -------------------------------------------------------------------------------- 1 | const array = [ 2 | { field: "id", operator: "eq" }, 3 | { field: "cStatus", operator: "eq" }, 4 | { field: "money", operator: "eq" }, 5 | ]; 6 | 7 | const filterField = "money" 8 | 9 | function removeArrayElement(filterField) { 10 | // write your solution here 11 | const newArray = array.filter(element => element.field !== filterField); 12 | return newArray; 13 | } 14 | 15 | 16 | console.log(`filtered array: ${removeArrayElement(filterField)}`) 17 | -------------------------------------------------------------------------------- /21_UnionOfTwoArrays.js: -------------------------------------------------------------------------------- 1 | const unionOfArrays = (arr1, arr2) => { 2 | // code below here 3 | let newArr = []; 4 | for (let i = 0; i < arr1.length; i++) { 5 | newArr.push(arr1[i]); 6 | } 7 | for (let i = 0; i < arr2.length; i++) { 8 | if (!newArr.includes(arr2[i])) { 9 | newArr.push(arr2[i]); 10 | } 11 | } 12 | return newArr; 13 | // code above here 14 | }; 15 | 16 | 17 | console.log(`The union is ${unionOfArrays([1, 2, 34, 45, 3], [3, 24, 21])}`); 18 | -------------------------------------------------------------------------------- /23_EqualSidesOfAnArray.js: -------------------------------------------------------------------------------- 1 | function findEvenIndex(arr) { 2 | //Code goes here! 3 | for (var i = 0; i < arr.length; i++) { 4 | var leftSum = 0; 5 | var rightSum = 0; 6 | for (var j = 0; j < i; j++) { 7 | leftSum += arr[j]; 8 | } 9 | for (var k = i + 1; k < arr.length; k++) { 10 | rightSum += arr[k]; 11 | } 12 | if (leftSum === rightSum) { 13 | return i; 14 | } 15 | } 16 | return -1; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /30_MexicanWave.js: -------------------------------------------------------------------------------- 1 | function wave(str) { 2 | // Your Code goes below 3 | var len = str.length; 4 | var arr = []; 5 | if (len == 0) return arr; 6 | 7 | var count = 0; 8 | 9 | while (count < len) { 10 | if (str.charAt(count).match(/[a-zA-Z]/)) 11 | arr.push( 12 | str.substring(0, count) + 13 | str.charAt(count).toUpperCase() + 14 | str.substring(count + 1, len) 15 | ); 16 | 17 | count++; 18 | } 19 | 20 | return arr; 21 | } 22 | 23 | console.log(wave("hello")); 24 | -------------------------------------------------------------------------------- /24_numberInExpandedForm.js: -------------------------------------------------------------------------------- 1 | function expandedForm(num) { 2 | // Your code here 3 | const currentNumArray = num.toString().split(""); 4 | let currentMultiplyingFactor = 1; 5 | let currentPos = currentNumArray.length - 1; 6 | 7 | while (currentPos >= 0) { 8 | currentNumArray[currentPos] = ( 9 | parseInt(currentNumArray[currentPos]) * currentMultiplyingFactor 10 | ).toString(); 11 | currentMultiplyingFactor *= 10; 12 | currentPos--; 13 | } 14 | 15 | return currentNumArray.join("+").trim(); 16 | } 17 | -------------------------------------------------------------------------------- /40_vehiclePurchaseInstructions.js: -------------------------------------------------------------------------------- 1 | const needsLicense = (kind) => { 2 | // code here 3 | 4 | return ["car", "truck"].includes(kind); 5 | }; 6 | 7 | const chooseVehicle = (option1, option2) => { 8 | // code here 9 | 10 | return `${ 11 | option1.localeCompare(option2) === 1 ? option2 : option1 12 | } is clearly the better choice.`; 13 | }; 14 | 15 | const calculateResellPrice = (originalPrice, age) => { 16 | // code here 17 | 18 | let factor = 0; 19 | 20 | if (age > 10) factor = 0.5; 21 | else if (age < 3) factor = 0.8; 22 | else factor = 0.7; 23 | 24 | return originalPrice * factor; 25 | }; 26 | -------------------------------------------------------------------------------- /38_LuhnAlgoInstructions.js: -------------------------------------------------------------------------------- 1 | const valid = (string) => { 2 | // code here 3 | let p = string 4 | .trim() 5 | .split("") 6 | .filter((val) => val !== " "); 7 | for (let i of p) if (isNaN(i)) return false; 8 | 9 | let s = []; 10 | p.reverse(); 11 | for (let i = 0; i < p.length; i++) { 12 | if (i % 2 !== 0) 13 | s += ( 14 | Number(p[i]) * 2 > 9 ? Number(p[i]) * 2 - 9 : Number(p[i]) * 2 15 | ).toString(); 16 | else s += p[i]; 17 | } 18 | 19 | let sum = 0; 20 | s = s.split(""); 21 | s.forEach((v) => { 22 | sum += Number(v); 23 | }); 24 | 25 | return sum % 10 === 0; 26 | }; 27 | -------------------------------------------------------------------------------- /17_longestConsecutiveSequence.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * @param {number[]} inputArray Array of numbers 4 | */ 5 | const longestConsecutiveSequence = (inputArray) => { 6 | 7 | const set = new Set(inputArray); 8 | let maxLength = 0; 9 | for (let i = 0; i < inputArray.length; i++) { 10 | if (!set.has(inputArray[i] - 1)) { 11 | let currentNum = inputArray[i]; 12 | let currentLength = 1; 13 | while (set.has(currentNum + 1)) { 14 | currentNum += 1; 15 | currentLength += 1; 16 | } 17 | maxLength = Math.max(maxLength, currentLength); 18 | } 19 | } 20 | return maxLength; 21 | } -------------------------------------------------------------------------------- /19_resistorColorMap.js: -------------------------------------------------------------------------------- 1 | const colorCode = (color) => { 2 | // Code here 3 | switch (color) { 4 | case 'black': 5 | return 0; 6 | case 'brown': 7 | return 1; 8 | case 'red': 9 | return 2; 10 | case 'orange': 11 | return 3; 12 | case 'yellow': 13 | return 4; 14 | case 'green': 15 | return 5; 16 | case 'blue': 17 | return 6; 18 | case 'violet': 19 | return 7; 20 | case 'grey': 21 | return 8; 22 | case 'white': 23 | return 9; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /14_convertArrayDigitBaseToAnotherBase.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * @param {number[]} digits Array of valid digits of baseA 4 | * @param {number} baseA base a 5 | * @param {number} baseB base b in which digits are to be converted 6 | * @returns {number[]} Array of valid digits of baseB 7 | */ 8 | const convertDigitsToAskedBase = (digits, baseA, baseB) => { 9 | 10 | 11 | // convert digits to base 10 12 | const base10 = digits.reduce((acc, digit, i) => { 13 | return acc + digit * Math.pow(baseA, digits.length - i - 1); 14 | }, 0); 15 | 16 | // convert base 10 to base b 17 | let base10_b = base10; 18 | let baseB_digits = []; 19 | do { 20 | baseB_digits.push(base10_b % baseB); 21 | base10_b = Math.floor(base10_b / baseB); 22 | } while (base10_b > 0); 23 | 24 | return baseB_digits.reverse(); 25 | } -------------------------------------------------------------------------------- /34_mostFreqItemOfArray.js: -------------------------------------------------------------------------------- 1 | function mostFreq(arr) { 2 | // write your code here 3 | if (arr.length === 0) return -1; 4 | 5 | if (arr.length === 1) return [arr[0], 1]; 6 | 7 | const numMap = new Map(); 8 | 9 | for (let num of arr) { 10 | let currentNum = numMap.get(num); 11 | 12 | if (currentNum === undefined) numMap.set(num, 1); 13 | else numMap.set(num, currentNum + 1); 14 | } 15 | 16 | const numArray = [undefined, undefined]; 17 | 18 | for (let [key, value] of numMap) { 19 | if (numArray[1] === undefined || numArray[1] < value) { 20 | numArray[0] = key; 21 | numArray[1] = value; 22 | } 23 | } 24 | 25 | return numArray[0].toString() + " " + numArray[1].toString(); 26 | } 27 | 28 | const arr = [1, 2, 2, 4, 5, 6, 6]; 29 | 30 | console.log(mostFreq(arr)); 31 | -------------------------------------------------------------------------------- /48_HumanReadableDurationFormat.js: -------------------------------------------------------------------------------- 1 | function formatDuration(seconds) { 2 | // your code here 3 | var result = ""; 4 | var arr = []; 5 | var hour = Math.floor(seconds / 3600); 6 | var min = Math.floor((seconds % 3600) / 60); 7 | var sec = (seconds % 3600) % 60; 8 | if (hour > 0) { 9 | arr.push(hour + " hour" + (hour > 1 ? "s" : "")); 10 | } 11 | if (min > 0) { 12 | arr.push(min + " minute" + (min > 1 ? "s" : "")); 13 | } 14 | if (sec > 0) { 15 | arr.push(sec + " second" + (sec > 1 ? "s" : "")); 16 | } 17 | for (var i = 0; i < arr.length; i++) { 18 | if (i === 0) { 19 | result = arr[i]; 20 | } else if (i === arr.length - 1) { 21 | result += " and " + arr[i]; 22 | } else { 23 | result += ", " + arr[i]; 24 | } 25 | } 26 | return result; 27 | } 28 | -------------------------------------------------------------------------------- /13_covertSecondsToSpaceAge.js: -------------------------------------------------------------------------------- 1 | const spaceAge = (seconds) => { 2 | const yearsInAllPlanets = { 3 | Mercury: 0, 4 | Venus: 0, 5 | Earth: 0, 6 | Mars: 0, 7 | Jupiter: 0, 8 | Saturn: 0, 9 | Uranus: 0, 10 | Neptune: 0, 11 | }; 12 | 13 | const secondsInYear = 31557600; 14 | const years = seconds / secondsInYear; 15 | 16 | for (let planet in yearsInAllPlanets) { 17 | yearsInAllPlanets[planet] = 18 | parseFloat((years / 19 | (planet === "Earth" 20 | ? 1 21 | : planet === "Mercury" 22 | ? 0.2408467 23 | : planet === "Venus" 24 | ? 0.61519726 25 | : planet === "Mars" 26 | ? 1.8808158 27 | : planet === "Jupiter" 28 | ? 11.862615 29 | : planet === "Saturn" 30 | ? 29.447498 31 | : planet === "Uranus" 32 | ? 84.016846 33 | : planet === "Neptune" 34 | ? 164.79132 35 | : 0)).toFixed(2)); 36 | } 37 | 38 | return yearsInAllPlanets; 39 | }; 40 | 41 | console.log(spaceAge(Math.round(Math.random() * 99999999))); 42 | -------------------------------------------------------------------------------- /39_MixedJuicesInstructions.js: -------------------------------------------------------------------------------- 1 | const timeToMixJuice = (name) => { 2 | // code here 3 | switch (name) { 4 | case "Pure Strawberry Joy": 5 | return 0.5; 6 | case "Energizer": 7 | case "Green Garden": 8 | return 1.5; 9 | case "Tropical Island": 10 | return 3; 11 | case "All or Nothing": 12 | return 5; 13 | default: 14 | return 2.5; 15 | } 16 | }; 17 | 18 | const limesToCut = (wedgesNeeded, limes) => { 19 | // code here 20 | let wedges = 0; 21 | for (let i = 0; i < limes.length; i++) { 22 | if (limes[i] === "small") wedges += 6; 23 | else if (limes[i] === "medium") wedges += 8; 24 | else wedges += 10; 25 | 26 | if (wedges >= wedgesNeeded) return i + 1; 27 | } 28 | return limes.length; 29 | }; 30 | 31 | const remainingOrders = (timeLeft, orders) => { 32 | // code here 33 | let timetaken = 0; 34 | for (let i = 0; i < orders.length; i++) { 35 | timetaken += timeToMixJuice(orders[i]); 36 | orders.splice(0, 1); 37 | if (timetaken >= timeLeft) break; 38 | } 39 | return orders; 40 | }; 41 | -------------------------------------------------------------------------------- /15_determinesentenceIsAPangram.js: -------------------------------------------------------------------------------- 1 | const isPangram = (input) => { 2 | const alphabet = 'abcdefghijklmnopqrstuvwxyz'; 3 | const inputLowerCase = input.toLowerCase(); 4 | const inputLowerCaseArray = inputLowerCase.split(''); 5 | const inputLowerCaseArrayWithoutSpaces = inputLowerCaseArray.filter(char => char !== ' '); 6 | const inputLowerCaseArrayWithoutSpacesAndPunctuation = inputLowerCaseArrayWithoutSpaces.filter(char => alphabet.includes(char)); 7 | const inputLowerCaseArrayWithoutSpacesAndPunctuationSet = new Set(inputLowerCaseArrayWithoutSpacesAndPunctuation); 8 | const inputLowerCaseArrayWithoutSpacesAndPunctuationSetArray = Array.from(inputLowerCaseArrayWithoutSpacesAndPunctuationSet); 9 | const inputLowerCaseArrayWithoutSpacesAndPunctuationSetArrayLength = inputLowerCaseArrayWithoutSpacesAndPunctuationSetArray.length; 10 | const alphabetLength = alphabet.length; 11 | if (inputLowerCaseArrayWithoutSpacesAndPunctuationSetArrayLength === alphabetLength) { 12 | return true; 13 | } else { 14 | return false; 15 | } 16 | }; 17 | 18 | -------------------------------------------------------------------------------- /12_givenNumberSpellOut.js: -------------------------------------------------------------------------------- 1 | var NUMBER2TEXT = { 2 | ones: [ 3 | "", 4 | "one", 5 | "two", 6 | "three", 7 | "four", 8 | "five", 9 | "six", 10 | "seven", 11 | "eight", 12 | "nine", 13 | "ten", 14 | "eleven", 15 | "twelve", 16 | "thirteen", 17 | "fourteen", 18 | "fifteen", 19 | "sixteen", 20 | "seventeen", 21 | "eighteen", 22 | "nineteen", 23 | ], 24 | tens: [ 25 | "", 26 | "", 27 | "twenty", 28 | "thirty", 29 | "fourty", 30 | "fifty", 31 | "sixty", 32 | "seventy", 33 | "eighty", 34 | "ninety", 35 | ], 36 | sep: [ 37 | "", 38 | " thousand ", 39 | " million ", 40 | " billion ", 41 | " trillion ", 42 | " quadrillion ", 43 | " quintillion ", 44 | " sextillion ", 45 | ], 46 | }; 47 | 48 | const { ones, tens, sep } = NUMBER2TEXT; 49 | 50 | const sayNumberInEnglish = (number) => { 51 | let arr = [], 52 | str = "", 53 | i = 0; 54 | if (number.length === 0) { 55 | return; 56 | } 57 | 58 | number = parseInt(number, 10); 59 | if (isNaN(number)) { 60 | return; 61 | } 62 | 63 | while (number) { 64 | arr.push(number % 1000); 65 | number = parseInt(number / 1000, 10); 66 | } 67 | 68 | while (arr.length) { 69 | str = 70 | (function (a) { 71 | var x = Math.floor(a / 100), 72 | y = Math.floor(a / 10) % 10, 73 | z = a % 10; 74 | 75 | return ( 76 | (x > 0 ? ones[x] + " hundred " : "") + 77 | (y >= 2 ? tens[y] + "-" + ones[z] : ones[10 * y + z]) 78 | ); 79 | })(arr.shift()) + 80 | sep[i++] + 81 | str; 82 | } 83 | 84 | return str; 85 | }; 86 | 87 | console.log(`5635 in english is: ${sayNumberInEnglish(5635)}`); 88 | --------------------------------------------------------------------------------