Let’s practice some JavaScript

1. Truthy and falsy values

2. Closure

let name = “Ashutosh”;
function greet() {
console.log(‘hello ‘ + name);
}
greet();

3. bind, call and apply

const boundFunc = someFunction.bind(object, arg1, arg2, ...)
someFunc.call(object, arg1, arg2, ..)
Math.max.apply(null, [1,2,3,4,5]);  // 5

4. What is recursion?

5. Finding factorial of a number

const factorial = n => n ? n * factorial(n - 1) : 1;

6. How to remove duplicate items from an array

const removeDuplicate = arr => {
const uniqueArray = [];
for (let i = 0; i < arr.length; i++) {
if (!uniqueArray.includes(arr[i])) uniqueArray.push(arr[i]);
}
return uniqueArray;
}

7. Reverse a string

const reverseString = str => str ? str.substr(-1) + reverseString(str.slice(0, str.length - 1)) : '';

8. Finding Fibonacci series

const fibSeries = n =>  n < 2n ? [[0n], [0n, 1n]][n] : (s => [...s, s.pop() + s.pop()])(fibSeries(n - 1n));console.log(fibSeries(500n).join("\n"));

9. Counting the number words

const numberOfWords = someText.split(/\s+/).length;

10. Checking if a number is prime or not

const isPrime = n => {
if (n < 2) return false;
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store