JavaScript Assignment
JavaScript Assignment
Questions-
1) Familiarize yourself with Date object and solve the following exercises
a. Get number of days in a month (Code will have month number and year)
Test Cases:
Code: 1 2012
Output: 31
Code: 2 2016
Output: 29
Answer-
Code-
function getDays(month,year){
var daysInMonth= new Date(year,month,0).getDate();
return daysInMonth;
}
console.log(getDays(1,2012));
console.log(getDays(2,2016));
Output-
2) Familiarize yourself with Arrays and solve the following exercises:
a. Given is an array of numbers. Round the numbers and display the sum using
reduce() method.
Code: [1.1, 2.3, 15.5, 4.7]
Output: 24
Answer-
Code-
result=Math.round(result);
console.log(result);
Output-
b. Given an array of names and ages of people applying for driver’s license.
Return the names of people eligible to apply (18 or above) using the
filter() method.
Output-
Output
d. Given the above array use splice() operation to change the Code to the given
output
Output: apples, lemon, guavas, berries, peaches
Answer-
Code-
fruit.splice(1,2,"lemon");
console.log("output is: \n"+fruit);
Output-
Output-
yes
b) var i=0;
if(i===0)
{
let x=1;
console.log(x);
{
let x=8;
}
console.log(x);
}
else
{
console.log("i has different value")
}
What will be the output?
Answer-
Output-
PS E:\T.Y. IT Sem 5\WIM\Assignment 3\nodejs> node 3b.js
1
1
Answer- Int
function
multiplyBy2(sum)
{ console.log(sum * 2);
}
function
operationOnSum(num1,num2,operation){ var
sum = num1 + num2;
operation(sum);
}
operationOnSum(3, 3, divideByHalf);
operationOnSum(5, 5,
multiplyBy2);
Answer-
Output-
Answer-
Code-
function uppercase(str)
{
var array1 = str.split(' ');
var newarray1 = [];
5) Objects:
a. let obj1 = {
firstProp: 1,
secondProp
:{
innerProp: 2,
},
thirdProp: 3
}
Loop over and print each property of the object
Answer-
Code-
let obj1 = {
firstProp: 1,
secondProp: {
innerProp: 2,
},
thirdProp: 3
}
Output-
b. let obj1 = {
firstProp: 1,
secondProp
:{
innerProp: 2,
},
thirdProp: 3
}
let obj2 = {
firstProp: 1,
secondProp
:{
innerProp: 2,
},
thirdProp: 3
}
Perform deep comparison of the objects to check whether they are the same or
not.
Answer-
Code-
let obj1 = {
firstProp: 1,
secondProp: {
innerProp: 2,
},
thirdProp: 3
}
let obj2 = {
firstProp: 1,
secondProp: {
innerProp: 2,
},
thirdProp: 3
}
console.log(objectsAreSame(obj1,obj2));
Output-