01.Difference Between Math.floor and Math.round?
Although Math.floor function and Math.round looks like they are mostly doing similar jobs but there’s a little bit of difference between them.
First try to understand Math.floor();
Let’s consider we took a variable
var equation = 3/2;
console.log(equation); //returns 1.5
The result we will get by it is 1.5.
But when we will do the floor function …
console.log(Math.floor(equation)); // returns 1
It will not gonna give us a floating-point value, it will give us a round value like 1.
Ok now we understood Math.floor() function.
Now try to understand Math.round() with the same equation.
console.log(Math.round(equation)); //returns 2
As the same of Math.floor() function Math.round() is also not giving us floating-point value. It’s giving us an integer number.
So the difference we got between Math.floor() & Math.round() is floor always rounds down to the nearest integer & Math.round() always rounds up depending on which side of the 0.5 number falls on.
2. charAt() function
Firstly let’s simplify the charAt word. “Char” is an abbreviation of “Character”. Now if we read the function like characterAt there’s no problem seen to me. This function will find the character using the index number.
var x = “hello world”;
console.log(x.charAt(4)); // returns 0
This function returns ‘o’ because on x variable the index of 4 is character o and it returns the character. This is how this charAt() function works.
Let’s see an example of it:
var array1 = [‘a’, ‘b’, ‘c’];
var array2 = [‘d’, ‘e’, ‘f’];
var array3 = array1.concat(array2);
// expected output: Array [“a”, “b”, “c”, “d”, “e”, “f”]
var x= ‘ hello world ’;
console.log(x.trim()); //returns ; ‘hello world’
Here it returns ‘hello world’ because trim() function removes all the white spaces from the right and left part of the string.
This function simply gives us the index of array elements or strings or which index we want to know. If we want to know something but the indexOf() function didn’t get what we wants to know than it will return -1.
var colors = [“green”,”blue”,”yellow”];
console.log(colors.indexOf(“blue”); //returns 1
Because blue is the first index value of the colors array.
This method replace a part of value in a string and returns a complete new string.
Let’s consider :
var x = “hello world”
var replace = x.replace(“world”,”mars”);// returns “hello mars”
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
Example of Filter:
var words = [‘spray’, ‘limit’, ‘elite’, ‘exuberant’, ‘destruction’, ‘present’];
var result = words.filter(word => word.length > 6);
console.log(result); //returns [“exuberant”, “destruction”, “present”]
find() method just returns the first value of provided array from which are passed in the testing function.If it get no passed value then it will return undefined.
var array1 = [5, 12, 8, 130, 44]; var found = array1.find(element => element > 10);
console.log(found) //returns 12
Because 12 is the only first element that matches with the condition and passed the testing function.
This method converts the string value to lowercase letters. And returns the string to lowercase letter.
var name = ‘RIMON’;
This method converts the string value to uppercase letters and finally returns the string to uppercase letter.
Let’s consider :
var x = ‘rimon’;
console.log(x.toUpperCase()); //returns ‘RIMON’