-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
154 lines (125 loc) · 3.78 KB
/
Copy pathapp.js
File metadata and controls
154 lines (125 loc) · 3.78 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// question 1
let input_1 = +prompt("enter no 1");
let input_2 = +prompt("enter no 2");
function parent_function(value_1){
return function child_functon(value_2){
console.log(value_1 + value_2);
// let a = 4;//scope error
}
}
let function_1 = parent_function(input_1);
let function_2 = function_1(input_2);
// //*=================================================*//
//Question:2
function searchArray(array, value) {
// Base case: if the array is empty, return false
if (array.length === 0) {
return false;
}
// Base case: if the first element of the array is the value, return true
if (array[0] === value) {
return true;
}
// Recursive case: search the rest of the array
return searchArray(array.slice(1), value);
constarr =[1, 2, 3, 4, 5]
const value = 3;
console.log(searchArray(arr, value)); // Output: true
}
// *==================================================*//
//Question:3
let input_text = prompt("enter content:")
let para = document.createElement("p");
let parent = document.getElementsByTagName("html")[0];
// console.log(parent);
function para_work(string) {
para.innerHTML = string;
parent.appendChild(para);
parent.insertAdjacentElement("afterbegin", para);
}
para_work(input_text)
// //*======================================================*//
// //Question:4
function addListItem(afshan) {
const ul = document.querySelector('ul'); // Get the unordered list element
const li = document.createElement('li'); // Create a new list item element
li.textContent = afshan; // Set the text content of the list item
ul.appendChild(li); // Add the list item to the end of the unordered list
}
addListItem('New list item');
// //Question :5
localStorage.clear()
let object_1 = {
name:"Afshan",
age: 23,
phone_no:"03492991343",
height:"5",
cnic: "2667800894920",
eng_marks:23,
urdu_marks:65,
maths_marks:100
}
let object_2 = {
name:"ayesha",
age: 15,
phone_no:"94500684",
height:"4'3",
cnic: "8058543456-9",
eng_marks:90,
urdu_marks:40,
maths_marks:80
}
let object_3 = {
name:"fizza",
age: 14,
phone_no:"94500684",
height:"5'6",
cnic: "8058543456-9",
eng_marks:50,
urdu_marks:80,
maths_marks:60
}
function object_saving(key , value){
let new_object = JSON.stringify(value);
localStorage.setItem(key, new_object);
}
object_saving("data of Afshan" , object_1);
// // //*======================================================*//
// // //Question :6
function saveToLocalStorage(key, obj) {
localStorage.setItem(key, JSON.stringify(obj));
}
const myObject = {name: "John", age: 30};
saveToLocalStorage("myKey", myObject);
// //*=========================================================*//
// // //Question :7
function getObjectFromLocalStorage(key) {
try {
const item = localStorage.getItem(key);
if (item) {
return JSON.parse(item);
} else {
return null;
}
}}
catch(error) {
console.error(`Error retrieving item from localStorage: ${error.message}`);
return null;
}
// // //*======================================================*//
// // //Question:8
function saveObjectToLocalStorage(object) {
// Save each property to localStorage
for (let property in object) {
localStorage.setItem(property, JSON.stringify(object[property]));
}
// Retrieve the object from localStorage
const retrievedObject = {};
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
retrievedObject[key] = JSON.parse(localStorage.getItem(key));
}
// Return the retrieved object
return retrievedObject;
}
// // //*========================================================*//