|
| 1 | +# Practise Loop, functions & Math Problems |
| 2 | + |
| 3 | +### Question 1: sum of all natural numbers from 1 to n |
| 4 | + |
| 5 | +```javascript |
| 6 | +function sumOfNaturalNumber(num){ |
| 7 | + let sum = 0; |
| 8 | + for(let i=1; i<=num; i++){ |
| 9 | + sum = sum + i; |
| 10 | + } |
| 11 | + return sum; |
| 12 | +} |
| 13 | + |
| 14 | +console.log(sumOfNaturalNumber(5)) //15 |
| 15 | +console.log(sumOfNaturalNumber(10)) //55 |
| 16 | +console.log(sumOfNaturalNumber(8)) //36 |
| 17 | +``` |
| 18 | + |
| 19 | + |
| 20 | +### Question 2: Sum of digits of a number |
| 21 | + |
| 22 | +```javascript |
| 23 | +function sumOfDigits(num){ |
| 24 | + let sum = 0; |
| 25 | + while(num > 0){ |
| 26 | + sum += num%10; |
| 27 | + num = Math.floor(num / 10); |
| 28 | + } |
| 29 | + return sum; |
| 30 | +} |
| 31 | + |
| 32 | +console.log(sumOfDigits(1287)) //18 |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | +### Question 3: count the number of digits of a number |
| 37 | + |
| 38 | +```javascript |
| 39 | +function countDigits(num){ |
| 40 | + num = Math.abs(num); |
| 41 | + let count = 0; |
| 42 | + do { |
| 43 | + count++; |
| 44 | + num = Math.floor(num / 10); |
| 45 | + } while (num > 0); |
| 46 | + return count; |
| 47 | +} |
| 48 | + |
| 49 | +console.log(countDigits(121)) //3 |
| 50 | +console.log(countDigits(-1211413131)) //10 |
| 51 | +``` |
| 52 | + |
| 53 | +### Question 4: Given an integer x, return true if x is a palindrome, and false otherwise. |
| 54 | +A palindrome number is a number that remains the same when digits are reversed |
| 55 | + |
| 56 | +```javascript |
| 57 | +let isPalindrome = function(x) { |
| 58 | + let copyNum = x, reverseNum = 0; |
| 59 | + |
| 60 | + while(copyNum > 0){ |
| 61 | + const lastDigit = copyNum%10; |
| 62 | + reverseNum = reverseNum*10 + lastDigit; |
| 63 | + copyNum = Math.floor(copyNum/10) |
| 64 | + } |
| 65 | + |
| 66 | + return x === reverseNum |
| 67 | +}; |
| 68 | + |
| 69 | +console.log(isPalindrome(121)) //true |
| 70 | +console.log(isPalindrome(1234)) //false |
| 71 | +``` |
| 72 | + |
| 73 | +### Question 5: Find nth fibonacci number |
| 74 | +The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, |
| 75 | +such that each number is the sum of the two preceding ones, starting from 0 and 1. |
| 76 | + |
| 77 | +```javascript |
| 78 | +let fib = function(n) { |
| 79 | + if(n < 2){ |
| 80 | + return n; |
| 81 | + } |
| 82 | + |
| 83 | + let prev = 0, curr = 1, next; |
| 84 | + for(let i=2; i<= n; i++){ |
| 85 | + next = prev + curr; |
| 86 | + prev = curr; |
| 87 | + curr = next; |
| 88 | + } |
| 89 | + return next; |
| 90 | +}; |
| 91 | +// Fibonacci Sequence: 0 1 1 2 3 5 8... |
| 92 | +console.log(fib(5)) // 5 |
| 93 | +console.log(fib(10)) //55 |
| 94 | +``` |
| 95 | + |
| 96 | + |
| 97 | +### Question 6: Missing Number |
| 98 | +Given an array nums containing n distinct numbers in the range [0, n], |
| 99 | +return the only number in the range that is missing from the array. |
| 100 | + |
| 101 | +```javascript |
| 102 | +let missingNumber = function(nums) { |
| 103 | + let sum = 0; |
| 104 | + for(let i=0; i<nums.length; i++){ |
| 105 | + sum += nums[i]; |
| 106 | + } |
| 107 | + return nums.length*(nums.length+1)/2 - sum; |
| 108 | +}; |
| 109 | + |
| 110 | + |
| 111 | +// One Line Solution: |
| 112 | +let missingNumber = (nums) => nums.length*(nums.length+1)/2 - nums.reduce((acc, num) => num + acc); |
| 113 | + |
| 114 | + |
| 115 | +console.log(missingNumber([3,0,1])) //2 |
| 116 | +console.log(missingNumber([9,6,4,2,3,5,7,0,1])) //8 |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | +# Practice Questions |
| 121 | + |
| 122 | +- [Count odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/) |
| 123 | +- [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) |
| 124 | +- [Power of Two](https://leetcode.com/problems/power-of-two/) |
| 125 | +- [Find Square root of a Number](https://leetcode.com/problems/sqrtx/) |
0 commit comments