/*19) let a=2.9; let b=~~a; console.log(b); let c=Math.floor(a); console.log(c); console.log(36%10); let n=786; let n1 =String(n).split(""); let n2 = n1[1]+n1[2]; console.log(n2);*/ /*function num2words(n){ let nums = "zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split(" "); console.log(nums); let tens = "twenty thirty fourty fifty sixty seventy eighty ninety"; if(n<20){return nums[n];}; let digits = n%10; //6 if(n<100){ return tens[~~(n/10)-2] + nums[digits]; }; let n1 =String(n).split(""); let n2 = n1[1]+n1[2]; let digits1 = n2%10; if(n<1000){ return nums[~~(n/100)]+" hundredand "+tens[~~(n2/10)-2] + nums[digits1]; }; }; console.log(num2words(777));*/ //74) /*let str = "abdulasif"; console.log(Array.from(str)); console.log(str.split(""))*/ /*console.log("abdulasif".toUpperCase()); console.log("ISMATH".toLowerCase())*/ /*let date = new Date(2019,1,28).getDate(); console.log(date); function leapornot(year){ let date = new Date(year,1,29).getDate()===29; if(date){ console.log(year+" is a Leap year"); }else{ console.log(year+" is not a Leap year"); } }; leapornot(2024);*/ //49) /*let date = new Date(); let year = date.getFullYear(); console.log(year); function getage(dob){ let year = Number(dob.substr(0,4)); let month = Number(dob.substr(4,2)); let date = Number(dob.substr(6,2)); let today = new Date(); let age = today.getFullYear() - year; if(today.getMonth()<month || (today.getMonth==month && today.getDate()<date)){ age-- ; } return age; }; console.log(getage("20030711"));*/ //string to number => parseInt() //syntax: parseInt(string,radix) /*console.log( parseInt("78666")); //493158 //16 means hexadecimal console.log(parseInt("78666 assif")); //78666 console.log((78666).toString(16)); //converts to hexadec console.log(parseInt("10")); //10 console.log(parseInt("786.66")); //786 it gives only decimal part //to overcome the parseInt() issue => parseFloat() console.log(parseFloat("786.66")); //786.66*/ /*function convertfarenheittocelcius(farenheit){ let celcius = (farenheit-32)/1.8 ; console.log(celcius); }; convertfarenheittocelcius(100); //37.77777777777778*/ /*function convertcelciustofarenheit(celcius){ let farenheit = (celcius*1.8)+32; console.log(farenheit); }; convertcelciustofarenheit(37.77777777777778); //100*/ /*function fizzbuzz(){ for(let i=1; i<=100; i++){ if(i%15 == 0){ console.log("FizzBuzz"); }else if(i%3 == 0){ console.log("Fizz"); }else if(i%5 == 0){ console.log("Buzz") } else{ console.log(i); } }; }; fizzbuzz()*/ /*function findfactorialwithrecursion(x){ //important if(x==0){ //10*9*8*7*6*5*4*3*2*1=3628800 return 1; } return x * findfactorialwithrecursion(x-1); }; console.log(findfactorialwithrecursion(10));*/ /*function decimaltohexadecimal(dec){ console.log((dec).toString(16)); }; decimaltohexadecimal(33);*/ /*function decimaltobinary(dec){ console.log((dec).toString(2)); }; decimaltobinary(174);*/ /*function decimaltooctal(dec){ console.log((dec).toString(8)); }; decimaltooctal(15);*/ /*function factorsofgnno(num){ for(let i=0; i<=num; i++ ){ if(num%i==0){ console.log(i); }; }; }; factorsofgnno(60); */ //1 2 3 4 5 6 10 12 15 20 30 60 /* let number1 = 6; //lcm with hcf let number2 = 8; function hcf(num1,num2){ let hcf; for(let i=1; i<=num1&&i<=num2; i++){ if(num1%i==0 && num2%i==0){ hcf=i; } }; console.log(hcf); //2 let lcm = (num1*num2)/hcf; console.log(lcm); //24 }; hcf(number1,number2); */ /*function noofdaysbwtwodatesofsamemonthyear(date1,date2){ let today = new Date(); let year1 = Number(date1.substr(0,4)); let year2 = Number(date2.substr(0,4)); let month1 = Number(date1.substr(4,2)); let month2 = Number(date2.substr(4,2)); let ddate1 = Number(date1.substr(6,2)); let ddate2 = Number(date2.substr(6,2)); if(year1==year2 && month1==month2){ let higherdate = ddate1>ddate2?ddate1:ddate2; let lowerdate = ddate1<ddate2?ddate1:ddate2; let noofdays = higherdate-lowerdate; console.log(noofdays); }; }; noofdaysbwtwodatesofsamemonthyear("20220602","20220624") let date = new Date();*/ //go for this one /* function counttheoccurenceofgnword(string,particularword){ //this method is coorect but some problem is there that is it counts another word having is artially like "this".so go to another method let count =string.split(particularword).length-1; //but for count the particular charaacter it is ok to use. but it counts small leter for small leter large leter for large leter console.log(particularword+ -count); }; counttheoccurenceofgnword("abdulasabifABDULASIFA","ab"); //a-2 //A-3 counttheoccurenceofgnword("the the lion king is back buthe the tiger is not giveup","t")*/ /*function count_substr(str,searchvalue){ //it search only the gn word(the) but one problem is there that is it also counts another word containing "the" E.G "lathef" let count = 0; let i=0; while(true){ let r =str.indexOf(searchvalue,i); if(r!==-1){ count = count + 1; i=r+1; }else{ return count; } }; }; console.log(count_substr("the lion king is back but the tiger is not giveup","the")); //2 console.log(count_substr("the asif the ismath the lathef the anees","the")); //5 console.log(count_substr("The asif the ismath the lathef the anees","the")) //4 */ /*function gnnoisperfectornot(num){ //6,28,496,8128,33550336 these are th only perfect numbers ans so on. let temp=0; for(let i=1; i<=(Math.floor(num/2)); i++){ if(num%i == 0){ temp=temp+i; }; }; if(temp===num && temp!==0){ console.log(num+" is a Perfect Number") }else{ console.log(num+" is a not a Perfect Number") } }; gnnoisperfectornot(8128);*/ //console.log(10+ +"10"+10); /* console.log(1&1); //1 // &operator console.log(1&0); //0 console.log(0&1); //0 console.log(0&0); //0*/ /* console.log(1|1); //1 // |operator console.log(1|0); //1 console.log(0|1); //1 console.log(0|0); //0*/ /* console.log(1^1); //0 // ^operator console.log(1^0); //1 console.log(0^1); //1 console.log(0^0); //0*/ /*console.log(typeof "1"); //string console.log(typeof parseInt("asif")); //number //convert string to number console.log(typeof Number("asif")); //number console.log((786).toString(2)); //1100010010 //convert dec to binary console.log((786).toString(8)); //1422 //convert dec to octal console.log((786).toString(16)); //312 */ //convert dec to hexadecimal /*let a=1; console.log(a++); //1 it results 1 but internally increment is done and the internal value is 2 console.log(++a); //3 so go for this one console.log(a); //3 console.log(a--); //3 it results 3 but internally decrement is done and the internal value is 2 console.log(--a); //1 so go for this one*/ /*let b=1; //console.log(++b + ++b); //2 + 3 = 5 console.log(b++ + ++b); //1 + 3 = 4 */ /*arr=[10,20,50,30,10,20,30,10,20,30,40]; console.log(arr.indexOf(20,4)); //5 //synax: indexOf(element,index) =>starts from that index console.log(arr.indexOf(30,3)); //3 console.log(arr.indexOf(50,3)); //-1 console.log(arr.indexOf(20,-2)); //-1 console.log(arr.indexOf(20,-4)); //8 console.log(arr.indexOf(20,-11)); //1 console.log(arr.indexOf(20,-10)); //1 */ //this method only gives first appearence of string characters /*str = "ibrahimismath"; let arr = Array.from(str); console.log(arr); console.log([...new Set(arr)].join(""));*/ /*function howtoaddthenumbersinsidethestring(str){ let strnum =str.match(/[0-9]/g).join(""); console.log( strnum); ////1124984 let num = Number(strnum); let copynum =num; let total = 0; console.log(typeof num); while(copynum!==0){ total = total + (copynum%10) copynum = Math.floor(copynum/10); }; console.log(total); // ans:- 29 }; howtoaddthenumbersinsidethestring("ab11dul2aa498asi4f");*/ //wrong /*function removespecialcharactersandinlowercase(str){ let str1 = str.toLowerCase(); let str2 = str1.replace(/\s/g,""); //remove white spaces let str3 = str2.match(/\w/g).join(""); //it includes a-z,A-Z,0-9,_ and it converts the string into array so we use join() return str3; }; console.log(removespecialcharactersandinlowercase("$A BD ULasif@ 757555")); function howtoaddthenumbersinsidethestring(str){ let strnum =str.match(/[0-9]/g).join(""); //1124984 console.log(strnum); let num = Number(strnum); let copynum =num; let total = 0; console.log(typeof num); while(copynum!==0){ total = total + (copynum%10) copynum = Math.floor(copynum/10); }; console.log(total); }; howtoaddthenumbersinsidethestring("a$b1@1dul2aa498asi4f");*/ /*function countnoofvowels(str){ let count = str.match(/[aeiou]/g).join("").length; console.log(count); }; countnoofvowels("aneesrani"); function counttheoccurenceofparticularcharactersinstring(str){ let count = str.match(/[aif]/g).length; console.log(count); }; counttheoccurenceofparticularcharactersinstring("abdulaaasif");*/ /*function num2words(n){ let nums = "zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split(" "); console.log(nums); let tens = "twenty thirty fourty fifty sixty seventy eighty ninety".split(" "); if(n<20){return nums[n];}; let digits = n%10; //6 if(n<100){ return tens[~~(n/10)-2] + nums[digits]; }; let n1 =String(n).split(""); let n2 = n1[1]+n1[2]; console.log(typeof n2); let digits1 = n2%10; if(n<1000){ return nums[~~(n/100)]+" hundredand "+tens[~~(n2/10)-2] + nums[digits1]; }; }; console.log(num2words(777));*/ /*function primeornot(num){ for(let i=2; i<num; i++){ if(num%i==0){ return"gn no is not a rime no"; }; }; return "gn no is a prime no"; }; console.log(primeornot(10));*/ /*function primeornot(num){ for(let i=2; i<num; i++){ if(num%i==0){ return false; }; }; return true; }; //console.log(primeornot(10)); function primeno1to100anditsaverage(n){ let arr = [2]; for(let i=3; i<=n; i++){ if(primeornot(i)){ arr.push(i); }; }; console.log(arr); console.log("AVERAGE:"+ (arr.reduce((arg1,arg2)=>{return arg1+arg2}))/(arr.length)); }; primeno1to100anditsaverage(100);*/ //AVERAGE:42.4 //wrong:console.log(Array.from(String(54321)).reduce((arg1,arg2)=>{return arg1+arg2})) /*function sumofthedigits(n){ let num = n; let copynum = n; let total = 0; while(copynum!==0){ total = total + (copynum%10); copynum = Math.floor(copynum/10); }; console.log(total); //21 }; sumofthedigits(786);*/ //console.log(Math.floor(5432.1)); //5432 function pattern1(){ //1 22 333 4444 55555 let string = ""; for(let i=1; i<=5; i++){ for(let j=1; j<=i; j++){ string = string + i; }; string = string + "\n"; }; console.log(string); }; pattern1(); function pattern2(){ //1 12 123 1234 12345 let string = ""; let n = 5; for(let i=1; i<=n; i++){ for(let j=1; j<=i; j++){ string = string + j; }; string = string + "\n"; }; console.log(string); }; pattern2(); function pattern3(){ let string = ""; let n = 5; for(let i=1; i<=n; i++){ for(let j=1; j<=(n-i+1); j++){ string = string + j; }; string = string + "\n"; }; console.log(string); }; pattern3(); function pattern4(){ let string = ""; let count = 1; for(let i=1; i<=4; i++){ for(let j=1; j<=i; j++){ string = string + count; count++; }; string = string + "\n"; }; console.log(string); }; pattern4(); function pattern5(){ let string = ""; let n = 5; for(let i=1; i<=n; i++){ for(let j=1; j<=(n-i+1); j++){ string = string + (n-j+1); }; string = string + "\n"; }; console.log(string); }; pattern5();
Write, Run & Share Javascript code online using OneCompiler's JS online compiler for free. It's one of the robust, feature-rich online compilers for Javascript language. Getting started with the OneCompiler's Javascript editor is easy and fast. The editor shows sample boilerplate code when you choose language as Javascript and start coding.
Javascript(JS) is a object-oriented programming language which adhere to ECMA Script Standards. Javascript is required to design the behaviour of the web pages.
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line){
console.log("Hello, " + line);
});
Keyword | Description | Scope |
---|---|---|
var | Var is used to declare variables(old way of declaring variables) | Function or global scope |
let | let is also used to declare variables(new way) | Global or block Scope |
const | const is used to declare const values. Once the value is assigned, it can not be modified | Global or block Scope |
let greetings = `Hello ${name}`
const msg = `
hello
world!
`
An array is a collection of items or values.
let arrayName = [value1, value2,..etc];
// or
let arrayName = new Array("value1","value2",..etc);
let mobiles = ["iPhone", "Samsung", "Pixel"];
// accessing an array
console.log(mobiles[0]);
// changing an array element
mobiles[3] = "Nokia";
Arrow Functions helps developers to write code in concise way, it’s introduced in ES6.
Arrow functions can be written in multiple ways. Below are couple of ways to use arrow function but it can be written in many other ways as well.
() => expression
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const squaresOfEvenNumbers = numbers.filter(ele => ele % 2 == 0)
.map(ele => ele ** 2);
console.log(squaresOfEvenNumbers);
let [firstName, lastName] = ['Foo', 'Bar']
let {firstName, lastName} = {
firstName: 'Foo',
lastName: 'Bar'
}
const {
title,
firstName,
lastName,
...rest
} = record;
//Object spread
const post = {
...options,
type: "new"
}
//array spread
const users = [
...adminUsers,
...normalUsers
]
function greetings({ name = 'Foo' } = {}) { //Defaulting name to Foo
console.log(`Hello ${name}!`);
}
greet() // Hello Foo
greet({ name: 'Bar' }) // Hi Bar
IF is used to execute a block of code based on a condition.
if(condition){
// code
}
Else part is used to execute the block of code when the condition fails.
if(condition){
// code
} else {
// code
}
Switch is used to replace nested If-Else statements.
switch(condition){
case 'value1' :
//code
[break;]
case 'value2' :
//code
[break;]
.......
default :
//code
[break;]
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
ES6 introduced classes along with OOPS concepts in JS. Class is similar to a function which you can think like kind of template which will get called when ever you initialize class.
class className {
constructor() { ... } //Mandatory Class method
method1() { ... }
method2() { ... }
...
}
class Mobile {
constructor(model) {
this.name = model;
}
}
mbl = new Mobile("iPhone");