Guess the output - break is missing after Monday and Tuesday
const getWeekdays = (id) => {
let weekday = "";
switch (id) {
case 0:
weekday = "Sunday";
break;
case 1:
weekday = "Monday";
case 2:
weekday = "Tuesday";
case 3:
weekday = "Wednesday";
break;
case 4:
weekday = "Thursday";
break;
case 5:
weekday = "Friday";
break;
case 6:
weekday = "Saturday";
break;
default:
break;
}
return weekday;
};
console.log(`%c ${getWeekdays(1)}`, "color:red;font-size:20px");
//Wednesday
Guess the outupt
HOVER ON THIS TO FLIP AND SEE THE OUTPUT
Wednesday
small tip :- you can wrap curly braces in switch statement...
const getWeekdays = (id) => {
switch (id) {
case 0: {
let weekday = "Sunday";
console.log(weekday);
break;
}
case 1: {
let weekday = "Monday";
console.log(weekday);
break;
}
case 2: {
let weekday = "Tuesday";
console.log(weekday);
break;
}
case 3: {
let weekday = "Tuesday";
console.log(weekday);
break;
}
case 4:
{
let weekday = "Thursday";
console.log(weekday);
break;
}
break;
case 5:
{
let weekday = "Friday";
console.log(weekday);
break;
}
break;
case 6: {
let weekday = "Saturday";
console.log(weekday);
break;
}
default: {
let weekday = "unknown";
console.log(weekday);
break;
}
}
return weekday;
};
console.log(`%c ${getWeekdays("1")}`, "color:red;font-size:20px");
//if you remove curly braces you will get error - variable cannot be redeclared
//OUTPUT- Monday
Guess the output
let A = { a: 1, b: 2 };
let B = A;
B.x = 20;
console.log(A.x);
Guess the outupt
HOVER ON THIS TO FLIP AND SEE THE OUTPUT
20
Guess the output
const CopyEmployee = (emp) => {
emp.age = 45; //emp1 changes here..below doesnt impact emp1
//emp1- Alok , and age changed to 45
//emp2 will be assigned below
//emp2 will be Rahul and age - 20
emp = {
name: "Rahul",
age: 20,
};
return emp;
};
let emp1 = {
name: "Alok",
age: 50,
};
let emp2 = CopyEmployee(emp1);
console.log(emp1); //{name: 'Alok', age: 45}
console.log(emp2); //{name: 'Rahul', age: 20}
Guess the output
let learn = {
name: "Anurag",
study: function (course) {
console.log(this.name + " is learning " + course);
},
};
learn.study("Javascript");//Anurag is learning Javascript
Explanation
//Function inside object-->this is -->Owner Object
//owner object is --> learn
//this.name is --> Anurag
Object referencing - Guess the output
let emp = { name: "Anurag" };
let copyemp = [emp];
console.log(copyemp); //below output
//output
/*
[
{
"name": "Anurag"
}
]
*/
console.log(Array.isArray(copyemp)); //true
emp = null;
console.log(copyemp); //below output
//output
/*
[
{
"name": "Anurag"
}
]
*/
console.log(emp); //null
Object referencing - Guess the output
// Now change the property of original emp
let emp = { name: "Anurag" };
let copyemp = [emp];
emp.name = null;
console.log(copyemp);
/* name becomes null in copyemp- as object copy is reference type...
[
{
"name": null
}
]
*/
Guess the output
const input = { number: 5 };
const Addition = (x = { ...input }) => {
console.log((x.number += 10));
};
Addition(); //15 -->clone is done using spread operator..doesnt affect original input
Addition(); //15 -->clone is done using spread operator..doesnt affect original input
Addition(); //15 -->clone is done using spread operator..doesnt affect original input
Addition(input); //15(5+10)...it affects the original input
Addition(input); //25(15+10)..it affects the original input
Addition(input); //35(25+10)..it affects the original input
//normal copy of object
const input = { number: 5 };
let x1 = input;
input.number = 123;
console.log(x1.number); //123
console.log(input.number); //123
//spread operator copy - doesn't affect original object
const input2 = { number: 5 };
let x2 = { ...input2 };
x2.number = 123;
console.log(x2.number); //123
console.log(input2.number); //5
Addition(input); //15(5+10)...it affects the original input
this is just like
x= input ; so its a normal copy
********************
Addition();
this was like copy with spread operator
x ={...input }
Guess the output
//Guess the output of arr1 and arr2
let arr1 = [1, 2, 3];
let arr2 = [...arr1];
arr2.push(4);
console.log(arr1);
console.log(arr2);
Guess the outupt
HOVER ON THIS TO FLIP AND SEE THE OUTPUT
arr1 --> [1,2,3]
arr2 --> [1,2,3,4]
Guess the output
var employee = {
name: "Anurag",
getEmployeeCode: function () {
return this.name;
},
};
let accessEmployeeCode = employee.getEmployeeCode;
console.log(accessEmployeeCode());//undefined
console.log(employee.getEmployeeCode());//Anurag
Guess the output
var employee = {
name: "Anurag",
getEmployeeCode: function () {
return this.name;
},
};
let accessEmployeeCode = employee.getEmployeeCode.bind(employee);
console.log(accessEmployeeCode()); //Anurag
console.log(employee.getEmployeeCode()); //Anurag
Guess the output
console.log(typeof typeof 1);
//typeof 1 --> number
//typeof number --> string
Guess the output
let obj4 = {};
obj4.title = "learning object!!";
obj4["title"] = "learning new object !!!";
console.log(obj4.title); // learning new object !!!
console.log(obj4["title"]); // learning new object !!!
Guess the output
let side = 10;
const square = {
side: 5,
test: function () {
console.log(side);
}
};
//10
console.log(square.test());
let side = 10;
const square = {
side: 5,
area: function () {
return this.side * this.side;
},
perimeter: () => 4 * this.side,
};
//25--> this.radius refers to side in square function
console.log(square.area());
//NAN --> here side in arrow function refers to window object
console.log(square.perimeter());
let side = 10;
const square = {
side: 5,
area: function () {
return this.side * this.side;
},
perimeter: () => 4 * side,
};
//25
console.log(square.area());
//40
console.log(square.perimeter());
var side1 = 20;
const square = {
side1: 5,
area: function () {
return this.side1 * this.side1;
},
perimeter: () => 4 * this.side1,
};
//25--> this.radius refers to side in square function
console.log(square.area());
// here side in arrow function refers to window object.
//var side1=20 ...this will be in window.side1=20
console.log(square.perimeter());//80 (20 * 4)
let obj = {
name: "Anurag",
gender: "Male",
name: "Abhishek",
};
console.log(obj);
Guess the outupt
HOVER ON THIS TO FLIP AND SEE THE OUTPUT
{name: 'Abhishek', gender: 'Male'}
name is not Anurag but Abhishek
square the numbers inside object
//Square the numbers inside obj
let obj = {
num1: 4,
different: "different",
num2: 5,
num3: 6,
num4: 9,
};
for (let x in obj) {
if (typeof obj[x] === "number") {
obj[x] *= obj[x];
}
}
console.log(obj);
//{num1: 16, different: 'different', num2: 25, num3: 36, num4: 81}
Guess the output
const x = {};
const y = {};
const z = {};
const u = { key: "key" };
x[y] = 90;
x[u] = 95;
x[z] = 100;
console.log(x[u]);//100
// it becomes like this when you run it
//so finally the value is 100
// x[[object Object]]: 90
// x[[object Object]]: 95
// x[[object Object]]: 100
//x[u] is nothing but -->x[[object Object]] --> the final value is 100
Guess the output
const employee = {
name: "anurag",
getName: () => {
console.log(this.name);
},
};
employee.getName();
/*
this inside the arrow function is window object
OUTPUT:- undefined
----------normal function-----------
const employee = {
name: "anurag",
getName: function () {
console.log(this.name);
},
};
employee.getName(); //anurag
*/
AMPM time to HH:MM format
let timeAMOrPM = "10:32AM";
let amOrpm = timeAMOrPM.substring(timeAMOrPM.length - 2, timeAMOrPM.length);//AM
let time = timeAMOrPM.substring(0, timeAMOrPM.length - 2);//10:32
let hour = time.split(":")[0];//10
let min = time.split(":")[1];//32
if (amOrpm === "PM") {
hour = 12 + Number.parseInt(hour);
}
console.log("final time -->" + hour + ":" + min);
Make changes to get expected output
//what has to be done to do this human function so that we can call as below ?
//human.eat().play().sleep();
let human = {
eat() {
console.log("I am eating");
},
play() {
console.log("I am eating");
},
sleep() {
console.log("I am sleeping");
},
};
///////////////////////////////////
let human = {
eat() {
console.log("I am eating");
return this;
},
play() {
console.log("I am eating");
return this;
},
sleep() {
console.log("I am sleeping");
return this;
},
};
human.eat().play().sleep();
return this
Guess the output !!
console.log(+true); //1
console.log(true); //true
console.log(typeof +true); //number
console.log(typeof true); //boolean
Guess the outupt
HOVER ON THIS TO FLIP AND SEE THE OUTPUT
1
true
number
boolean
Guess the output !!
let x = 3;
let num = new Number(3);
console.log(x === num);
console.log(x == num);
Guess the outupt
HOVER ON THIS TO FLIP AND SEE THE OUTPUT
false
true
== Type corecion happens - both are 3 - true
=== does exact match - its false here
=== does exact match - its false here
Guess the output
var x = 1,y = 1;
x;
++
y;
console.log(x);
console.log(y);
Guess the outupt
HOVER ON THIS TO FLIP AND SEE THE OUTPUT
1
2
It becomes as below
var x = 1,y = 1;
x;
++y;
console.log(x);
console.log(y);
//output :- 1 2
var x = 1,y = 1;
x;
++y;
console.log(x);
console.log(y);
//output :- 1 2
Guess the output
let myArray = [
{ name: "Anurag", surname: "Nayak" },
{ name: "Abhishek", surname: "Nayak" },
];
console.log(myArray.indexOf({ name: "Anurag", surname: "Nayak" }));
Guess the outupt
HOVER ON THIS TO FLIP AND SEE THE OUTPUT
-1
Guess the output
console.log(12 > 14 > 16);
console.log(12 < 14 < 16);
console.log(2 + "2");
console.log(2 - "2");
Guess the outupt
HOVER ON THIS TO FLIP AND SEE THE OUTPUT
OUTUPUT
console.log(12 > 14 > 16);
12>14 - false - 0
0 > 16 - false
console.log(12 < 14 < 16);
12 < 14 - true - 1
1 < 16 - true
2 + "2" --> 22
2 - "2" --> 0
12>14 - false - 0
0 > 16 - false
console.log(12 < 14 < 16);
12 < 14 - true - 1
1 < 16 - true
2 + "2" --> 22
2 - "2" --> 0
Guess the output
Number.prototype[Symbol.iterator] = function* () {
for (let i = 0; i < this; i++) {
yield i * i;
}
};
console.log([...5]);
Guess the outupt
HOVER ON THIS TO FLIP AND SEE THE OUTPUT
OUTUPUT
[0, 1, 4, 9, 16]