Javascript Interview and some Math solving.
01| Truthy and Falsy Values;
function total(value){
if(value){
return "This is truty value"
}else{
return "This is false value"
}
}
console.log(total(4))
- This is a truthy value. Causes You using a value.
console.log(total(0))
console.log(total("0"))
- Only “0” is the false value. Without “0” all number is a truth value. “Negative” value is a truthy value.
- If the value surrounding a Quotation mark then it is a string. And it is Truty value.
console.log(total(false))
console.log(total("false"))
- If you using a false value then it is a false value.
- If the value surrounding a Quotation mark then it is a string. And it is Truty value.
console.log(total(null))
console.log(total(undefined))
console.log(total(NaN))
- If you using null, undefined, NaN value then this value is a falsy value.
console.log(total("Azizul"))
console.log(total(""))
console.log(total(" "))
- The first console.log is a true value. Causes First console.log value length is not “0”. A similar thing is happened in the third console.log.
- It is not an empty string. This string length is one.
- But The second console.log value.length is “0”.
- Only the empty string is the false value.
let fruits;
console.log(fruits);
if(name){
return "This is truty value"
}else{
return "This is false value"
}
console.log(total(4))
- If you declare the fruits variable. But any value is not assigned. Then this value is undefined. And this is falsy value.
The falsey value is “”, NaN, undefined, null, 0 , false, without assinged any value.
02|Null Vs Undefined:
i) Undefined:
let fruits;
console.log(fruits);
- You can declare a variable but not set any value. Then it gives undefined.
function total(num){
console.log(num)
}
console.log(total(4))
- You can declare a function without return. Then it gives undefined.
function total(x,y){
return x * y;
}
console.log(total(4))
- You have a function that has two-parameter. But you give one parameter for the result. Then it gives undefined if this function has no default parameter value.
const fruits = {name: "mango", price: 60}
console.log(fruits.quantity)
- If you can read a value in these objects, But this value is not set here. Then this is called undefined.
ii) Null:
- Null is the value this is set by a developer.
- Null is a non-existent and empty value.
- Null is a value that must be assigned.
Undefined is the value that set browser and null is the value that set a developer.
03| Double Equal and Triple Equal:
const number1 = 2;
const number2= "2";
if(number1 == number2){
console.log('condition is true');
}else{
console.log("condition is not true");
}if(number1 === number2){
console.log('condition is true');
}else{
console.log("condition is not true");
}
- Here number1 and number2 are variable. When we checked this value is double equal then it checks the value. It is not checking the variable type.
- If you set a string value and check it for double equal then for using double equal the second variable number2 is converting a number and try to match it.
- So the first condition is true.
- On the other hand, the triple equal is checked value and type both.
- So the second condition is false.
const number1 = 0;
const number2= false;
if(number1 == number2){
console.log('condition is true');
}else{
console.log("condition is not true");
}
- Then the result is true. Causes actually only “0” is a falsy value.
const number1 = 1;
const number2= true;
if(number1 == number2){
console.log('condition is true');
}else{
console.log("condition is not true");
}
- Then the result is true. Causes actually “1” is a truthy value.
- Without 1 any value is using then the condition is false.
04| Map Method:
const fruits = [
{ name: "mango", price: 25},
{ name: "banana", price: 5},
{ name: "jackfruit", price: 50},
]
for(let i = 0 , i < fruits.length i++){
const food = fruits[i];
console.log(food.name)
}const singleFruit = fruits.map(fruit => fruit.name)
console.log(singleFruit)
- The first time in javascript we using the loop(for, while) method.
- After coming ES5 the developer using the map method.
- Map is the alternative method of Loop.
- Map always returns an array.
- In this example, I mapped in fruits array and want all fruits names.
- So the result is [“mango”, “banana”, “jackfruit”]
05| How to hooks work:
function useState(initiateValue){
let value = initiateValue;
function state(){
return value
}
function setState(newValue){
value = newValue;
}
return [state, setState];
}// let [food, setFood] = useState([])// console.log(food());
- This is the useState hook building method.
- At first, we declared a variable that has a parameter. And that assigned into a variable. The variable name is value.
- Then the first function is created that named state and return the parameter value
- THen the second function called setState and take a new parameter. Returns the new parameter.
- At last return two functions.
- And these two functions called outside the function.
- Then console. log food function.
- You get a value that we set by setFood function.
06| Global Scope, Block Scope, Functional Scope or Local Scope:
ler name = "kolmilota"
function total(value){
console.log(name);
if(value){
console.log(name);
return "This is truty value"
}else{
return "This is false value"
}
console.log(name);
}
console.log(total(4))
- In this example, we use a let variable outside the function.
- It is called Global scope variable.
- You can excess any place in function or outside the function.
function total(value){
console.log(name);
if(value){
Let name = "kolmilota"
console.log(name);
return "This is truty value"
}else{
return "This is false value"
}
console.log(name);
}
console.log(total(4))
- In this example, we use a let variable inside the if condition in function.
- It is called the Block scope variable.
- You can excess only inside if condition.
function total(value){
console.log(name);
if(value){
var name = "kolmilota"
console.log(name);
return "This is truty value"
}else{
return "This is false value"
}
console.log(name);
}
console.log(total(4))
- When we use the var variable then this variable goes at the top of a function. But the value is not gone.
If you declaired a let or const keyword then it make block scope. It has a specific area. In this example the area is if condition.
If you using a var keyword then it is local or functional scope. You can acccess any place in function this var keyword.
07| Find and Filter Method:
const fruits = [
{ name: "mango", price: 25},
{ name: "banana", price: 5},
{ name: "jackfruit", price: 50},
]
const fruit = fruits.find(fruit => fruit.price > 10);
const fruit = fruits.filter(fruit => fruit.price > 10);
- In this example, we are using the find and filter method.
- In find method check the condition and gives an object.
- The result is { name: ‘mango’, price: 25 }
- On the other hand filter method check the condition and returns an array.
- The result is [ { name: ‘mango’, price: 25 }, { name: ‘jackfruit’, price: 50 } ]
Filter returns in array, and find returns an object.
08| Find max element:
let marks = [42, 3, 4, 23, 44]
let max = marks[0];
for(let i = 0; i < marks.length; i++){
let element = marks[i]
if(element > max){
max = element
}
}
- In this example, we declare a variable before for loop.
- We check every element in this array and find the largest element and assign the max variable.
colsole.log(Math.max(...marks))
- In this example, this is the Math.max method. We can easily use this method.
09| Reserve Method:
function stringReversing(string) {
return string.split("").reverse().join("");
}
stringReversing("Hello");
- In this example, we want to reverse the “hollo” word.
- At first, we split this word then reverse and then join.
10| Define Sum in the Array:
let number= [42, 3, 4, 23, 44]
function getArraySum(number){
let sum= 0;
for(let i = 0; i < number.length; i++){
let element = number[i]
sum = element + sum;
}
return sum;
}
console.log(getArraySum(number));
- In this example, we declare a sum variable. Then we start for loop for catching every element in this array.
- Then we added this element to the sum variable.
- We are using the same thing by reduce function using.