WIM
Web Development Session Assignment-JavaScript and HTML
Submission date- 27/08/2021
Name- Neha Vijay Khairnar
ID- 191081036
Branch- IT
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-
const dates = [];
dates.push(new Date('2015/01/01'));
dates.push(new Date('2014/05/09'));
dates.push(new Date('2014/05/27'));
const minDate = new Date(Math.min.apply(null, dates));
console.log("Minimum date from an array of dates is- "+ minDate);
Output-
b. minimum date from an array of dates Test Case:
[ ‘2015/01/01’, ‘2014/05/09’, ‘2014/05/27’ ]
Output: 2014/05/09
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-
var num = [1.1,2.3,15.5,4.7];
var sum=0;
let result = num.reduce((a,b) => {return a+b})
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.
Code: [{name: “Mary”, age: 21},{name:”John”, age: 17},
{name:”Mark”,
age: 24},{name: “Susan”, age: 16},{name: “Jacob”, age: 19}]
Output: Mary, Mark, Jacob
Answer-
Code-
const person=[{name: "Mary", age: 21},
{name:"John", age: 17},
{name:"Mark",age: 24},
{name: "Susan", age: 16},
{name: "Jacob", age: 19}];
const personAbove18 = person.filter((person) => person.age >= 18);
console.log("Person eligible to apply (18 or above) are : ");
console.log(personAbove18);
Output-
c. Given an array of fruits. Perform slice() operations to get the required
output. (There could be multiple ways to obtain an output)
Code: [apples, oranges, bananas, guavas, berries,peaches]
i. Output 1: apples, oranges, bananas
ii. Output 2: peaches
Answer-
Code-
var fruit=['apples', 'oranges', 'bananas', 'guavas', 'berries','peaches'];
const output1= fruit.slice(0,3);
console.log("outtput 1 : "+ output1);
const output2=fruit.slice(-1);
console.log("output 2: "+output2);
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-
var fruit=['apples', 'oranges', 'bananas', 'guavas', 'berries','peaches'];
fruit.splice(1,2,"lemon");
console.log("output is: \n"+fruit);
Output-
3) Variables and Data Types:
a) var num = 6;
let num2 = 6;
if (num===num2)
{
console.log("yes");
}
else
{
console.log("No")
}
What will be the 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
c) Which is not a data type in Javascript?
i) Int
ii) Var
iii) Let
iv) Const
Answer- Int
4) Call back and functions
a. function divideByHalf(sum)
{ console.log(Math.floor(sum / 2));
}
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);
What is the output?
Answer-
Output-
PS E:\T.Y. IT Sem 5\WIM\Assignment 3\nodejs> node 4a.js
3
20
b. Write a JavaScript function that accepts a string as a parameter and
converts the first letter of each word of the string in upper case.
Example string: 'the quick brown fox'
Expected Output: 'The Quick Brown Fox '
Answer-
Code-
function uppercase(str)
{
var array1 = str.split(' ');
var newarray1 = [];
for(var x = 0; x < array1.length; x++){
newarray1.push(array1[x].charAt(0).toUpperCase()+array1[x].slice(1));
}
return newarray1.join(' ');
}
console.log(uppercase("the quick brown fox"));
Output-
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
}
for (let key in obj1) {
if (obj1.hasOwnProperty(key)) {
console.log(key, obj1[key]);
}
}
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
}
function objectsAreSame(obj1, obj2) {
var objectsAreSame = true;
for(var propertyName in obj1) {
if(obj1[propertyName] !== obj2[propertyName])
{
objectsAreSame = false;
break;
}
}
return objectsAreSame;
}
console.log(objectsAreSame(obj1,obj2));
Output-