-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
111 lines (87 loc) · 3.74 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Hide and show divs
var divs = ["Div1", "Div2", "Div3", "Div4"];
var visibleDivId = null;
function divVisibility(divId) {
if (visibleDivId === divId) {
visibleDivId = null;
} else {
visibleDivId = divId;
}
hideNonVisibleDivs();
}
function hideNonVisibleDivs() {
var i, divId, div;
for (i = 0; i < divs.length; i++) {
divId = divs[i];
div = document.getElementById(divId);
if (visibleDivId === divId) {
div.style.display = "block";
} else {
div.style.display = "none";
}
}
}
//Assignment Question 2
var details = {
name: "Danish",
email: "[email protected]",
password: "Secrate",
age: 23,
gender: "Male",
city: "Karachi",
country: "Pakistan"
}
var a = 'age' in details
var b = 'country' in details
var c = 'firstName' in details
var d = 'lastName' in details
document.getElementById("output").innerHTML = "Age exist in Object" + "             " + a + "<br/> <br/>" + "Country exist in Object" + "       " + b + "<br/> <br/>" + "firstName exist in Object" + "    " + c + "<br/> <br/>" + "lastName exict in Object" + "     " + d;
// // //Assignment Question 3
function Students(name, fName, _class, id, phoneNo) {
this.name = name;
this.fName = fName;
this._class = _class;
this.id = id;
this.phoneNo = phoneNo;
}
var student1 = new Students("Danish", "Shamsuddin", 12, 0001, "03120250");
var student2 = new Students("Junaid", "Nazeer", 11, 0002, "03002121");
var student3 = new Students("Sharjeel", "Shakel", 10, 0003, "0333121212");
document.getElementById("student").innerHTML = JSON.stringify(student1, null, 2) + "<br>" + JSON.stringify(student2, null, 2) + "<br>" + JSON.stringify(student3, null, 2);
// // //Assignment Question 4
var details = [];
const addDetails = (ev) => {
ev.preventDefault(); //to stop the form submitting
function Population(Name, gender, education, profession) {
this.Name = Name
this.gender = gender
this.education = education
this.profession = profession
}
details.push(new Population(document.getElementById('name').value, document.getElementById("gender").value, document.getElementById('education').value, document.getElementById('profession').value));
document.querySelector('form').reset();
console.warn('added', { details });
let pre = document.querySelector('#msg pre');
pre.textContent = '\n' + JSON.stringify(details, '\t', 2);
//saving to localStorage
localStorage.setItem('PopulationData', JSON.stringify(details));
}
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('btn').addEventListener('click', addDetails);
});
//Assignment Question 1
var itemsArray = [
{ name: "juice", price: 50, quantity: 3 },
{ name: "cookie", price: 30, quantity: 9 },
{ name: "shirt", price: 880, quantity: 1 },
{ name: "pen", price: 100, quantity: 2 }];
var arr = [];
for (i = 0; i <= itemsArray.length; i++) {
var iteamPrice = itemsArray[i].price * itemsArray[i].quantity
arr.push(iteamPrice);
document.getElementById("totalPrice").innerHTML = "Total price of" + " " + itemsArray[0].name + arr[0] + "<br> <br>" + "Total price of" + " " + itemsArray[1].name + arr[1] + "<br> <br>" + "Total price of" + " " + itemsArray[2].name + arr[2] + "<br> <br>" + "Total price of" + " " + itemsArray[3].name + arr[3];
document.getElementById("Gtotal").innerHTML = "Sum of all total prices " + arr.reduce(totalSum);
function totalSum(total, num) {
return total + num;
}
}