JavaScript Problem Solving for Beginners
When I started learning JavaScript, I found that problem solving is the most difficult in JavaScript. Especially for beginners. Here we going to discuss some common problem solving in series in JavaScript.
Factorial
One of the most common problem solving practice that you are going to face when starting JavaScript. Factorial symbol looks like this ( ! ). What it does you pick up a number, you have to multiply that number with the all previous number. Which is something look like this:
1! = 1
2! = 1*2
3! = 1*2*3
4! = 1*2*3*4
5! = 1*2*3*4*5
So how do we do it JavaScript !
let element = 1;for (let i = 1; i <= 10; i++) {element = element * i;console.log(element)}
This is the one of the way to do factorial in JavaScript.
factorial in a recursive way.
function factorial(num) {if (num < 0)return -1;else if (num == 0)return 1;else {return (num * factorial(num — 1));}}factorial(5);
Fibonacci
Fibonacci is a series of number, where you pick up a number from that series, and which is the result of previous two number that is added. Fibonacci series looks like this -
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
Let’s see how we can do it using JavaScript
// fibonaccilet fibonacci = [0, 1];for (let i = 2; i <= 10; i++) {fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]}console.log(fibonacci);
Fibonacci in a recursive way
function fibonacci(n) {if (n == 0) {return [0];}if (n == 1) {return [0, 1];}else {let fibo = fibonacci(n-1);let element = fibo(n-1) + fibo(n-2);fibo.push(element)return fibo;}}
Swap
Swap the value of a variable, often time we need to change the value of a variable, but how do we do this? Let’s see
let a = 5let b = 6let x = a;a = bb = x;console.log(a ,b)
there is another way which we can call the JavaScript way to do this
let x = 5;let y = 6;[x, y] = [y, x]
Play with Numbers
There is several way to solve and play with numbers in JavaScript
for example take a number
let num = 3.154653
If you want the number to be without decimal. Or you want to make it integer. Let’s see how we can do it
const myNum = Math.round(num);
this will give a result of 3;
well, what if we want the number to be the highest if there is a decimal. this is how we do it..
const myNum = Math.ceil(num);
Find the max value
Find the max value from different variable
const value1 = 1500;const value2 = 900;const value3 = 1600;if (value1 > value2){if (value1 > value3){console.log(“value1 is bigger”)}else{console.log(“value3 is bigger”)}}else {console.log(“value2 is bigger”)}
Find the largest element in the array
Find the large number in the array, even in the real world sometimes you would need this.
const num = [20, 25, 48, 35, 85, 65];let max = [0];for (let i = 0; i < num.length; i++) {const element = num[i];if( element > max){max = element;}}console.log(max)
Add all the numbers in the array
const num = [20, 25, 48, 35, 85, 65];let sum = 0;for (let i = 0; i < num.length; i++) {const element = num[i];sum = sum + element;}console.log(sum);
Null vs. Undefined
At first glance, null
and undefined
may seem the same, but they are far different from each other.
null is an empty or non-existent value.
let a = null;console.log(a);// null
Undefined means a variable has been declared, but not defined.
let c = undefined;console.log(c);// undefined
Single, double, triple equal in JavaScript
= is used to declare a value
== is to compare & === is also to compare
But, there is a but
== Will look at the value and if it is similar, but not necessary in types, the result is going to be true. Al though one is number one is string.
const num = 2;const num2 = “2”;
=== Will look at value and types as well, then the result is going to be true.
const num = 2;const num2 = 2;
Smart way to loop in JavaScript
Using JavaScript in built methods like map, filter, find is a smarter way to loop in JavaScript, also time consuming and headache free
Map
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
const array1 = [1, 4, 9, 16];// pass a function to mapconst map1 = array1.map(x => x * 2);console.log(map1);// expected output: Array [2, 8, 18, 32]
Find
The find() method returns the value of the first element in the provided array that satisfies the provided testing function.
const array1 = [5, 12, 8, 130, 44];const found = array1.find(element => element > 10);console.log(found);// expected output: 12