|
| 1 | +document.write(`<h2>ARRAYS</h2>`); |
| 2 | + |
| 3 | +// Question# 1: |
| 4 | +// 1. Declare an empty array using JS literal notation to store |
| 5 | +// student names in future. |
| 6 | +// Question# 2: |
| 7 | +// 2. Declare an empty array using JS object notation to store |
| 8 | +// student names in future. |
| 9 | + |
| 10 | +var std_names = []; |
| 11 | + |
| 12 | +// Question# 3: |
| 13 | +// 3. Declare and initialize a strings array. |
| 14 | +var friend_name = ["Ummulqura", "Ramla", "Maheen", "Sakina", "Usbah"]; |
| 15 | + |
| 16 | +// Question# 4: |
| 17 | +// 4. Declare and initialize a numbers array. |
| 18 | +var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
| 19 | + |
| 20 | +// Question# 5: |
| 21 | +// 5. Declare and initialize a boolean array. |
| 22 | +var conditional = [true, false, true, true, false, false]; |
| 23 | + |
| 24 | +// Question# 6: |
| 25 | +// 6. Declare and initialize a mixed array. |
| 26 | +var mixed_arr = ["Aleena", 5, false, 6, 10, true, "Aleeza"]; |
| 27 | + |
| 28 | +// Question# 7: |
| 29 | +// 7. Declare and Initialize an array and store available |
| 30 | +// education qualifications in Pakistan (e.g. SSC, HSC, BCS, |
| 31 | +// BS, BCOM, MS, M. Phil., PhD). Show the listed |
| 32 | +// qualifications in your browser like: |
| 33 | + |
| 34 | +// document.write(`<h3>Qualifications</h3>`); |
| 35 | +// var edu_qualifi = ["SSC", "HSC", "BCS", "BS", "BCOM", "MS", "M.Phil", "PhD"]; |
| 36 | +// document.write(`1) ${edu_qualifi[0]} </br>`); |
| 37 | +// document.write(`2) ${edu_qualifi[1]} </br>`); |
| 38 | +// document.write(`3) ${edu_qualifi[2]} </br>`); |
| 39 | +// document.write(`4) ${edu_qualifi[3]} </br>`); |
| 40 | +// document.write(`5) ${edu_qualifi[4]} </br>`); |
| 41 | +// document.write(`6) ${edu_qualifi[5]} </br>`); |
| 42 | +// document.write(`7) ${edu_qualifi[6]} </br>`); |
| 43 | +// document.write(`8) ${edu_qualifi[7]} </br> </br>`); |
| 44 | + |
| 45 | +// Question# 8: |
| 46 | +// Write a program to store 3 student names in an array.Take |
| 47 | +// another array to store score of these three students. |
| 48 | +// Assume that total marks are 500 for each student, display |
| 49 | +// the scores & percentages of students like: |
| 50 | + |
| 51 | +// var stud_name = ["Izma", "Naddiya", "Ayesha"]; |
| 52 | +// var stud_score = [400, 490, 400]; |
| 53 | +// var total_marks = 500; |
| 54 | +// let stud1_per = (stud_score[0] / total_marks) * 100; |
| 55 | +// let stud2_per = (stud_score[1] / total_marks) * 100; |
| 56 | +// let stud3_per = (stud_score[2] / total_marks) * 100; |
| 57 | + |
| 58 | +// document.write( |
| 59 | +// `Score of ${stud_name[0]} is ${stud_score[0]}. Percentage: ${stud1_per}% </br>` |
| 60 | +// ); |
| 61 | +// document.write( |
| 62 | +// `Score of ${stud_name[1]} is ${stud_score[1]}. Percentage: ${stud2_per}% </br> ` |
| 63 | +// ); |
| 64 | +// document.write( |
| 65 | +// `Score of ${stud_name[2]} is ${stud_score[2]}. Percentage: ${stud3_per}% </br> </br> ` |
| 66 | +// ); |
| 67 | + |
| 68 | +// Question# 9: |
| 69 | +// Initialize an array with color names. Display the array |
| 70 | +// elements in your browser. |
| 71 | + |
| 72 | +// var color = ["red", "blue", "green", "black", "yellow"]; |
| 73 | +// document.write(`${color} </br>`); |
| 74 | +// // a. Ask the user what color he/she wants to add to the |
| 75 | +// // beginning & add that color to the beginning of the array. |
| 76 | +// var user_color = prompt("Enter a color to add in beginning of the array"); |
| 77 | +// color.unshift(user_color); |
| 78 | +// // Display the updated array in your browser. |
| 79 | +// document.write(`${color} </br>`); |
| 80 | +// // b. Ask the user what color he/she wants to add to the end |
| 81 | +// // & add that color to the end of the array. Display the |
| 82 | +// // updated array in your browser. |
| 83 | + |
| 84 | +// var user_color1 = prompt("Enter a color to add in End of the array"); |
| 85 | +// color.push(user_color1); |
| 86 | +// document.write(`${color} </br>`); |
| 87 | +// // c. Add two more color to the beginning of the array. |
| 88 | +// // Display the updated array in your browser. |
| 89 | +// color.unshift("pink", "parrot"); |
| 90 | +// document.write(`${color} </br>`); |
| 91 | + |
| 92 | +// // d. Delete the first color in the array. Display the updated |
| 93 | +// // array in your browser. |
| 94 | +// color.shift(); |
| 95 | +// document.write(`${color} </br>`); |
| 96 | + |
| 97 | +// // e. Delete the last color in the array. Display the updated |
| 98 | +// // array in your browser. |
| 99 | +// color.pop(); |
| 100 | +// document.write(`${color} </br>`); |
| 101 | + |
| 102 | +// // f. Ask the user at which index he/she wants to add a color |
| 103 | +// // & color name. Then add the color to desired |
| 104 | +// // position/index. . Display the updated array in your |
| 105 | +// // browser. |
| 106 | +// var user_index = prompt("At which index you want to add color?"); |
| 107 | +// var user_color2 = prompt("Enter a color to add the color to desired index!"); |
| 108 | +// color.splice(user_index, 0, user_color2); |
| 109 | +// document.write(`${color} </br>`); |
| 110 | + |
| 111 | +// // g. Ask the user at which index he/she wants to delete |
| 112 | +// // color(s) & how many colors he/she wants to delete. Then |
| 113 | +// // remove the same number of color(s) from user-defined |
| 114 | +// // position/index.Display the updated array in your |
| 115 | +// // browser. |
| 116 | +// var user_index1 = prompt("At which index you want to delete color"); |
| 117 | +// var delete_clr = prompt("how many colors you want to delete?"); |
| 118 | +// color.splice(user_index1, delete_clr); |
| 119 | +// document.write(`${color} </br> </br>`); |
| 120 | + |
| 121 | +// Question# 10: |
| 122 | +// 'Write a program to store student scores in an array & |
| 123 | +// sort the array in ascending order using Array’s sort |
| 124 | +// method. ' |
| 125 | +var std_scores = [260, 450, 100, 200, 250, 240, 350, 105, 112]; |
| 126 | +document.write(`Score of Students: ${std_scores} </br>`); |
| 127 | +std_scores.sort(); |
| 128 | +document.write(`Order Score of Students: ${std_scores} </br> </br>`); |
| 129 | + |
| 130 | +// Question# 11: |
| 131 | +// Write a program to initialize an array with city names. |
| 132 | +// Copy 3 array elements from cities array to selectedCities |
| 133 | +// array. |
| 134 | + |
| 135 | +var city_name = [ |
| 136 | + "Karachi", |
| 137 | + "Islamabad", |
| 138 | + "Lahore", |
| 139 | + "Multan", |
| 140 | + "Quetta", |
| 141 | + "Peshawar", |
| 142 | + "Rawalpindi", |
| 143 | +]; |
| 144 | +document.write(`Cities List:</br> ${city_name} </br>`); |
| 145 | +var selected_cities = ["Karachi", "Islamabad", "Quetta"]; |
| 146 | +document.write(`selected Cities List: </br> ${selected_cities} </br> </br>`); |
| 147 | + |
| 148 | +// Question# 12: |
| 149 | +// Write a program to create a single string from the |
| 150 | +// below mentioned array: |
| 151 | +var arr = ["This", "is", " my", "cat"]; |
| 152 | +// (Use array’s join method) |
| 153 | +var sentence = arr.join(" "); |
| 154 | +document.write(`${sentence} </br></br>`); |
| 155 | + |
| 156 | +// Question# 13: |
| 157 | + |
| 158 | +// Create a new array. Store values one by one in such a way |
| 159 | +// that you can access the values in the order in which they |
| 160 | +// were stored. (FIFO-First In First Out) |
| 161 | + |
| 162 | +var elec_appliances = ["Fridge", "Fan", "Room Cooler", "Ac", "Iron", "Grinder"]; |
| 163 | +document.write(`Out: </br> ${elec_appliances[0]}</br>`); |
| 164 | +document.write(`Out: </br> ${elec_appliances[1]}</br>`); |
| 165 | +document.write(`Out: </br> ${elec_appliances[2]}</br>`); |
| 166 | +document.write(`Out: </br> ${elec_appliances[3]}</br>`); |
| 167 | +document.write(`Out: </br> ${elec_appliances[4]}</br>`); |
| 168 | +document.write(`Out: </br> ${elec_appliances[5]}</br></br>`); |
| 169 | + |
| 170 | +// Question# 14: |
| 171 | +// Create a new array. Store values one by one in such a way |
| 172 | +// that you can access the values in reverse order. (Last In |
| 173 | +// First Out) |
| 174 | +var fabric = [ |
| 175 | + "Lawn", |
| 176 | + "Silk", |
| 177 | + "Cotton", |
| 178 | + "Tissue", |
| 179 | + "Pluster Cotton", |
| 180 | + "Georgette", |
| 181 | +]; |
| 182 | +document.write(`Out: </br> ${fabric[5]}</br>`); |
| 183 | +document.write(`Out: </br> ${fabric[4]}</br>`); |
| 184 | +document.write(`Out: </br> ${fabric[3]}</br>`); |
| 185 | +document.write(`Out: </br> ${fabric[2]}</br>`); |
| 186 | +document.write(`Out: </br> ${fabric[1]}</br>`); |
| 187 | +document.write(`Out: </br> ${fabric[0]}</br></br>`); |
| 188 | + |
| 189 | +// Question# 15: |
| 190 | +// Write a program to store phone manufacturers (Apple, |
| 191 | +// Samsung, Motorola, Nokia, Sony & Haier) in an array. |
| 192 | +// Display the following dropdown/select menu in your |
| 193 | +// browser using document.write() method: |
| 194 | + |
| 195 | +var ph_manufac = ["Apple", "Samsung", "Motorola", "Nokia", "Sony", "Haier"]; |
| 196 | + |
| 197 | +document.write( |
| 198 | + `<form action=""> |
| 199 | + <select name="phone-manufacturer"> |
| 200 | + <option value="${ph_manufac[0]}">${ph_manufac[0]}</option> |
| 201 | + <option value="${ph_manufac[1]}">${ph_manufac[1]}</option> |
| 202 | + <option value="${ph_manufac[2]}">${ph_manufac[2]}</option> |
| 203 | + <option value="${ph_manufac[3]}">${ph_manufac[3]}</option> |
| 204 | + <option value="${ph_manufac[4]}">${ph_manufac[4]}</option> |
| 205 | + <option value="${ph_manufac[5]}">${ph_manufac[5]}</option> |
| 206 | + </select> |
| 207 | + </form>` |
| 208 | + ); |
0 commit comments