Output based numerical javascript question answers solved

 Here are some solved JavaScript questions with a focus on numerical operations. These questions cover basic arithmetic, looping, number manipulation, and more.

1. Find the sum of numbers from 1 to N.

Problem: Write a function to find the sum of numbers from 1 to N.

javascript
function sumOfNumbers(N) { let sum = 0; for (let i = 1; i <= N; i++) { sum += i; } return sum; } console.log(sumOfNumbers(5)); // Output: 15 (1 + 2 + 3 + 4 + 5)

2. Check if a number is prime.

Problem: Write a function to check if a given number is prime.

javascript
function isPrime(num) { if (num <= 1) return false; for (let i = 2; i <= Math.sqrt(num); i++) { if (num % i === 0) return false; } return true; } console.log(isPrime(5)); // Output: true console.log(isPrime(10)); // Output: false

3. Find the factorial of a number.

Problem: Write a function to find the factorial of a number.

javascript
function factorial(n) { if (n === 0 || n === 1) return 1; let result = 1; for (let i = 2; i <= n; i++) { result *= i; } return result; } console.log(factorial(5)); // Output: 120 (5! = 5 * 4 * 3 * 2 * 1)

4. Reverse a number.

Problem: Write a function to reverse a number.

javascript
function reverseNumber(num) { let reversed = 0; while (num !== 0) { reversed = reversed * 10 + (num % 10); num = Math.floor(num / 10); } return reversed; } console.log(reverseNumber(12345)); // Output: 54321

5. Check if a number is Armstrong (Narcissistic) number.

Problem: Write a function to check if a number is an Armstrong number. An Armstrong number is a number that is equal to the sum of its digits each raised to the power of the number of digits.

For example, 153 is an Armstrong number because 13+53+33=1531^3 + 5^3 + 3^3 = 153.

javascript
function isArmstrong(num) { let digits = num.toString().split(''); let numberOfDigits = digits.length; let sum = 0; for (let digit of digits) { sum += Math.pow(Number(digit), numberOfDigits); } return sum === num; } console.log(isArmstrong(153)); // Output: true console.log(isArmstrong(123)); // Output: false

6. Find the greatest common divisor (GCD) of two numbers.

Problem: Write a function to find the GCD of two numbers using the Euclidean algorithm.

javascript
function gcd(a, b) { while (b !== 0) { let temp = b; b = a % b; a = temp; } return a; } console.log(gcd(56, 98)); // Output: 14

7. Find the Least Common Multiple (LCM) of two numbers.

Problem: Write a function to find the LCM of two numbers.

javascript
function lcm(a, b) { return (a * b) / gcd(a, b); } console.log(lcm(4, 5)); // Output: 20

8. Fibonacci series up to Nth term.

Problem: Write a function to generate the Fibonacci series up to the Nth term.

javascript
function fibonacci(n) { let fibSeries = [0, 1]; for (let i = 2; i < n; i++) { fibSeries.push(fibSeries[i - 1] + fibSeries[i - 2]); } return fibSeries; } console.log(fibonacci(7)); // Output: [0, 1, 1, 2, 3, 5, 8]

9. Find the sum of digits of a number.

Problem: Write a function to find the sum of the digits of a number.

javascript
function sumOfDigits(num) { let sum = 0; while (num > 0) { sum += num % 10; num = Math.floor(num / 10); } return sum; } console.log(sumOfDigits(12345)); // Output: 15 (1 + 2 + 3 + 4 + 5)

10. Find the power of a number (x^n).

Problem: Write a function to calculate the power of a number (x raised to the power n).

javascript
function power(x, n) { return Math.pow(x, n); } console.log(power(2, 3)); // Output: 8 (2^3)

11. Find the largest digit in a number.

Problem: Write a function to find the largest digit in a given number.

javascript
function largestDigit(num) { let digits = num.toString().split(''); return Math.max(...digits.map(Number)); } console.log(largestDigit(12345)); // Output: 5

12. Count the number of vowels in a string.

Problem: Write a function to count the number of vowels in a string.

javascript
function countVowels(str) { let count = 0; const vowels = 'aeiouAEIOU'; for (let char of str) { if (vowels.includes(char)) { count++; } } return count; } console.log(countVowels('Hello World')); // Output: 3 (e, o, o)

13. Find if a number is a perfect number.

Problem: A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding the number itself.

Example: 6 is a perfect number because 1 + 2 + 3 = 6.

javascript
function isPerfectNumber(num) { let sum = 0; for (let i = 1; i <= num / 2; i++) { if (num % i === 0) { sum += i; } } return sum === num; } console.log(isPerfectNumber(6)); // Output: true console.log(isPerfectNumber(28)); // Output: true

14. Find the prime factors of a number.

Problem: Write a function to find all the prime factors of a given number.

javascript
function primeFactors(num) { let factors = []; for (let i = 2; i <= Math.sqrt(num); i++) { while (num % i === 0) { factors.push(i); num /= i; } } if (num > 1) factors.push(num); // if num is prime return factors; } console.log(primeFactors(56)); // Output: [2, 2, 2, 7]

15. Find the nth triangular number.

Problem: The nth triangular number is the sum of the first n natural numbers. Write a function to find the nth triangular number.

javascript
function triangularNumber(n) { return (n * (n + 1)) / 2; } console.log(triangularNumber(5)); // Output: 15 (1 + 2 + 3 + 4 + 5)

These problems provide a wide range of fundamental numerical challenges that help test a candidate's understanding of algorithms, number manipulation, loops, and arithmetic in JavaScript.

Comments

Popular posts from this blog

PrimeNG tutorial with examples using frequently used classes

Docker and Kubernetes Tutorials and QnA

oAuth in angular