Fun with Java Script
Get Random number : Think one day your cousin visit your home and you guys want to play ludu . but you do not have the instrument of ludu . suddenly you got a idea you know java script. you can make a apps like ludu game and you wrote some code and impress your cousin. yahhh!!!!
Now what is the code :
const ludu = Math.random() * 6;
const luduNumber = Math.round(ludu);
console.log(luduNumber);
Get Max Value by JavaScript : Think now your all cousin are in same room .suddenly your grandfather come to this room and said “I have 500 taka and i want to give who is elder in this room . ” Every one said “they are elder than other” . So again everyone comes to you said please find out how is elder in this room . You worte some code and find the elder age person.
Now what is the code?
const age = [15, 20, 30, 7, 10];
const findMaxAge = Math.max(…age);
console.log(findMaxAge);
Get total value of an array : In this time your grand mother come into the room and said “I will give the same amount of money to all the people in the room” . Again everyone comes to you and request please do something . You wrote some code and find out total age .
Now the code will look like this .
const age = [15, 20, 30, 7, 10];
const findTotalAge = age.reduce((a, b) => {
return a + b;
}, 0);
console.log(findTotalAge);
Find the Duplicate in the array : After dinner you guys again make a cricket game. In this time wrote every one name and toss . who win the toss he/she will be bat first . But someone wrote his name twice and you and to remove his one name because now you have two name and you need one . you wrote some code and solve the problem .
The code will be like this :
const name = [“sufian”, “rahi”, “maudud”, “sanjida”, “ritu”, “sufian”, “rahi”];
const unique = [];
for (let i = 0; i < name.length; i++) {
const element = name[i];
const index = unique.indexOf(element);
if (index === -1) {
unique.push(element);
}
}
console.log(unique);
Find out how much word in a string : you need to find out how much word in a name ? you wrote a code and solve the problem .
where is the code.
const name = “sufian”;
const total = name.length;
console.log(total);
Reverse the string : One day you meet a alien you said “hello”. But he can not understand . But if you reverse hello then alien can understand . You write a code and this time he can understand .
here is the code :
const greeting = “hello”;
let reverse = “”;
for (let i = 0; i < greeting.length; i++) {
const element = greeting[i];
reverse = element + reverse;
}
console.log(reverse);
Convert inch to feet :
function inchToFeet(inch) {
const feet = inch / 12;
return feet;
}
const result = inchToFeet(inch);
console.log(result);
Find the year is leap year or not :
function leapYear(year) {
if (year % 4 === 0 && year % 100 !== 0 && year % 400 === 0) {
console.log(“LeapYear”);
} else {
console.log(“not”);
}
}
leapYear(year);
Now we talk about true and false value in java script .
in java Script in number only 0 means false an other number means true .Same as string , if you have no value in the citation java script think it is false value and you have anything its true value . So we can make summary all false value — 0 , ‘’, undefined , null, NaN,false.
How we can get undefined :
- if you dealer a variable but do not set any value .
- if you do not return from function.
- if you do not pass any parameter.
- if you have no properties in object but you want to find out that .
- if you set undefined as your self .
What is deference between null vs undefined ?
we can get undefined but we set null.