Last active
December 15, 2015 02:30
-
-
Save kavitshah8/144ad2f85a1b570adb22 to your computer and use it in GitHub Desktop.
Numbers
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
| var cachedNumbers = {}; | |
| function calculateFibonacciSum (num) { | |
| if(cachedNumbers[num]) { | |
| return cachedNumbers[num]; | |
| } | |
| if(('number' === typeof num) && num <= 0) { | |
| throw new Error ('Fibonnci series starts with 0. Please, enter any interget greater than or equal to 0'); | |
| } | |
| else if(('number' === typeof num) && num === 0) { | |
| return 0; | |
| } | |
| else if(('number' === typeof num) && (num === 1 || num === 2)) { | |
| return 1; | |
| } | |
| else { | |
| var value = calculateFibonacciSum(num-1) + calculateFibonacciSum(num-2); | |
| cachedNumbers[num] = value; | |
| return value; | |
| } | |
| } | |
| console.log(calculateFibonacciSum(10)); // 55 | |
| console.log(calculateFibonacciSum(-10)); // Uncaught Fibonnci series starts with 0. Please, enter any interget greater than or equal to 0 | |
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 closetPairs (arr) { | |
| var result = []; | |
| var minDiff = Number.MAX_VALUE; | |
| // sort the given array | |
| var sortedArr = arr.sort(function(a, b) { | |
| return a - b; | |
| }); | |
| // find the lowest difference for a pair of elements in the sorted array | |
| for (var i = 0; i < sortedArr.length - 1; i++) { | |
| var currDiff = sortedArr[i + 1] - sortedArr[i]; | |
| if (currDiff < minDiff) { | |
| minDiff = currDiff;2, 5, 11, 19 | |
| } | |
| } | |
| // find all the pairs with lowest differnece | |
| for (var j = 0; j < sortedArr.length - 1; j++) { | |
| var sortedEleDiff = sortedArr[j + 1] - sortedArr[j]; | |
| if (minDiff == sortedEleDiff) { | |
| result.push(sortedArr[j + 1], sortedArr[j]); | |
| } | |
| } | |
| var sortedResult = result.sort(function (a, b) { | |
| return a - b; | |
| }); | |
| return sortedResult; | |
| } | |
| var inputArr = [-20, -3910, -357, -3620, 7374, -7330, 30, 6246, -646, 266, -510, -460 ]; | |
| console.log(closetPairs(inputArr)); // [-510, -460, -20, 30] | |
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 findPrimeFactors (num) { | |
| var primeFactors = []; | |
| while (num % 2 === 0) { | |
| primeFactors.push(2); | |
| num = num / 2; | |
| } | |
| var sqrtNum = Math.sqrt(num); | |
| for (var i = 3; i <= sqrtNum; i++) { | |
| while (num % i === 0) { | |
| primeFactors.push(i); | |
| num = num / i; | |
| } | |
| } | |
| if (num > 2) { | |
| primeFactors.push(num); | |
| } | |
| return primeFactors; | |
| } | |
| console.log(findPrimeFactors(10)); // [2, 5] | |
| console.log(findPrimeFactors(11)); // [11] | |
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
| Number.prototype.isOdd = function() { | |
| return ((this % 2) === 1) || ((this % 2) === -1) ? true : false; | |
| } | |
| var a = -3; | |
| console.log(a.isOdd()); // true | |
| a = 2.1; | |
| console.log(a.isOdd()); // false | |
| console.log((3).isOdd()); // true | |
| console.log((2).isOdd()); // false | |
| a = '3'; | |
| console.log(a.isOdd()); // Uncaught Not a 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
| function isOdd (num) { | |
| if (typeof num === 'number') { | |
| return ((num % 2) === 1) || ((num % 2) === -1) ? true : false; | |
| } | |
| else { | |
| throw 'Not a number'; | |
| } | |
| } | |
| console.log(isOdd(3)); // true | |
| console.log(isOdd(2)); // false | |
| console.log(isOdd(-3)); // true | |
| console.log(isOdd(2.1)); // false | |
| console.log(isOdd('3')); // Uncaught Not a 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
| 'use strict'; | |
| function negate (num) { | |
| if (num === 0) { // base case | |
| return 0; | |
| } | |
| var neg = 0; | |
| var subtractor = num < 0 ? 1 : -1; | |
| while (num != 0) { | |
| neg += subtractor; | |
| num += subtractor; | |
| } | |
| return neg; | |
| } | |
| console.log(negate(0)); // 0 | |
| console.log(negate(5)); // -5 | |
| console.log(negate(-5)); // 5 | |
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
| 'use strict'; | |
| function power (base, exponent) { | |
| if (exponent < 0) { | |
| throw new Error ('Negative expoenents are not acceted'); | |
| } | |
| var result = 1; | |
| while (exponent) { | |
| if ((exponent & 1) === 1) { | |
| result *= base; | |
| } | |
| exponent = exponent >> 1; | |
| base *= base; | |
| } | |
| return result; | |
| } | |
| console.log(power(2, 3)); // 8 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment