|
| 1 | +// 1. Write a script to greet your website visitor using JS alert box: |
| 2 | +// Your code goes here |
| 3 | +alert("hello world"); |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +// 2. Write a script to display the following message on your web page: |
| 8 | +// Your code goes here |
| 9 | +let a="this is my first website"; |
| 10 | +document.write(a); |
| 11 | +console.log(a); |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +// 4. Generate the following message through the browser’s developer console: |
| 16 | +// Your code goes here |
| 17 | +console.log("Hello, this is a message generated through the browser's developer console!"); |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +// ....................... |
| 22 | +// 1. Declaration of variable |
| 23 | +// Declare a variable called userName. |
| 24 | +// Your code goes here |
| 25 | +let a = "username"; |
| 26 | +console.log(a); |
| 27 | +document.write(a); |
| 28 | + |
| 29 | + |
| 30 | +// 2. Declaration of variable with assigning a name |
| 31 | +// Declare a variable called myName and assign it your name. |
| 32 | +// Your code goes here |
| 33 | +let myname = "Arman Asghar"; |
| 34 | +console.log(myname); |
| 35 | +document.write(myname); |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +// 3. Write a script to display a message using the alert box |
| 40 | +// Declare a variable called message and assign it the string "Hello World". |
| 41 | +// Use the alert() method to display the value of the message variable in an alert box. |
| 42 | +// Your code goes here |
| 43 | +let message = "Hello World"; |
| 44 | +alert(message); |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +// 4. Write a script to save student’s bio data in JS variables and show the data in alert boxes. |
| 50 | +// Create variables to store the following information about a student: stdName, stdAge, certification. |
| 51 | +// Display each variable's value in separate alert boxes. |
| 52 | +// Your code goes here |
| 53 | +// Save student's bio data in JS variables |
| 54 | +let studentName = "Arman Asghar"; |
| 55 | +let studentAge = 24; |
| 56 | +let certification = "Bachelor of Science"; |
| 57 | +alert("Student Name: " + studentName); |
| 58 | +alert("Student Age: " + studentAge); |
| 59 | +alert("Certification: " + certification); |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +// 6. Declare a variable called email and assign to it a string that represents your Email Address. |
| 65 | +// Use the alert() method to display a message that includes your email address. |
| 66 | +// Your code goes here |
| 67 | + |
| 68 | +alert("My email address is: " + email); |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +// 7. Declare a variable called book and give it the value "A smarter way to learn JavaScript". |
| 73 | +// Use the alert() method to display a message that includes the value of the book variable. |
| 74 | +// Your code goes here |
| 75 | +let book = "A smarter way to learn JavaScript"; |
| 76 | +alert("The book I am reading is: " + book); |
| 77 | + |
| 78 | + |
| 79 | +// 8. Write a script to display the following message in the browser using JS. |
| 80 | +// Declare a variable called displayBrowse and assign it the string "Yah! I can write HTML content through JavaScript". |
| 81 | +// Use document.write() method to display the message in the browser. |
| 82 | +// Your code goes here |
| 83 | +let displayBrowse = "Yah! I can write HTML content through JavaScript"; |
| 84 | +document.write(displayBrowse); |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +// 9. Declare a variable called age and assign it your age. |
| 89 | +// Use the alert() method to display a message that includes your age. |
| 90 | +// Your code goes here |
| 91 | +let age = 24; |
| 92 | +alert("My age is: " + age); |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +// 10. Declare and initialize a variable to keep track of how many times a visitor has visited a web page. |
| 97 | +// Use the alert() method to display a message that includes the number of times the visitor has visited the page. |
| 98 | +// Your code goes here |
| 99 | +let visitCount = localStorage.getItem('visitCount'); |
| 100 | + |
| 101 | +if (visitCount === null) { |
| 102 | + visitCount = 1; |
| 103 | + } else { |
| 104 | + visitCount = parseInt(visitCount) + 1; |
| 105 | + } |
| 106 | + localStorage.setItem('visitCount', visitCount); |
| 107 | + alert("You have visited this page " + visitCount + " times."); |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +// 11. Declare a variable called birthYear and assign it your birth year. |
| 112 | +// Use document.write() method to display a message that includes your birth year. |
| 113 | +// Your code goes here |
| 114 | +let birthYear = 1999; |
| 115 | +document.write("My birth year is: " + birthYear); |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +// 12. A visitor visits an online clothing store www.xyzClothing.com. |
| 120 | +// Write a script to store in variables the following information: store, visitorName, productTitle, Quantity. |
| 121 | +// Use document.write() method to display a message that includes the information. |
| 122 | +// Your code goes here |
| 123 | +let store = "www.mianexport.com"; |
| 124 | +let visitorName = "Arman Asghar"; |
| 125 | +let productTitle = "Black T-Shirt"; |
| 126 | +let quantity = 2; |
| 127 | + |
| 128 | +document.write("Welcome to " + store + "<br>"); |
| 129 | +document.write("Visitor Name: " + visitorName + "<br>"); |
| 130 | +document.write("Product Title: " + productTitle + "<br>"); |
| 131 | +document.write("Quantity: " + quantity + "<br>"); |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +// 13. Declare and initialize an empty multidimensional array. |
| 136 | +// Your code goes here |
| 137 | +let multiDimensionalArray = []; |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +// 14. Declare and initialize a multidimensional array representing the following matrix: |
| 142 | +// Your code goes here |
| 143 | +let matrix = [ |
| 144 | + [1, 2, 3], |
| 145 | + [4, 5, 6], |
| 146 | + [7, 8, 9] |
| 147 | + ]; |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +// 15. Write a program to print numeric counting from 1 to 10. |
| 152 | +// Use a loop to display the numbers from 1 to 10 using document.write() method. |
| 153 | +// Your code goes here |
| 154 | +for (let i = 1; i <= 10; i++) { |
| 155 | + document.write(i + "<br>"); |
| 156 | +} |
| 157 | + |
| 158 | + |
| 159 | +// 16. Declare different types of variables: |
| 160 | +// Declare variables with the following names and empty values: literalArr, objectArr, stringArr, numberArr, boolArr, mixedArr. |
| 161 | +// Initialize the stringArr variable as an empty array with one empty string element. |
| 162 | +// Initialize the numberArr variable as an empty array. |
| 163 | +// Initialize the mixedArr variable as an array containing a string, a number, a boolean, and an undefined value. |
| 164 | +// Display each array variable using document.write() method on separate lines. |
| 165 | +// Your code goes here |
| 166 | + |
| 167 | + let literalArr; |
| 168 | + let objectArr; |
| 169 | + let stringArr; |
| 170 | + let numberArr; |
| 171 | + let boolArr; |
| 172 | + let mixedArr; |
| 173 | + |
| 174 | + stringArr = ['']; |
| 175 | + |
| 176 | + numberArr = []; |
| 177 | + |
| 178 | + mixedArr = ['Hello', 42, true, undefined]; |
| 179 | + |
| 180 | + document.write("literalArr: " + literalArr + "<br>"); |
| 181 | + document.write("objectArr: " + objectArr + "<br>"); |
| 182 | + document.write("stringArr: " + stringArr + "<br>"); |
| 183 | + document.write("numberArr: " + numberArr + "<br>"); |
| 184 | + document.write("boolArr: " + boolArr + "<br>"); |
| 185 | + document.write("mixedArr: " + mixedArr + "<br>"); |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | +// 17. Write a program to store 3 student names in an array. |
| 190 | +// Take another array to store the scores of these three students. |
| 191 | +// Calculate the percentage of each student's score and display the result using document.write() method. |
| 192 | +// Your code goes here |
| 193 | +let studentNames = ["Arman", "Mudassar", "Umar"]; |
| 194 | + |
| 195 | +let scores = [85, 92, 78]; |
| 196 | + |
| 197 | +let percentages = []; |
| 198 | +for (let i = 0; i < scores.length; i++) { |
| 199 | +let percentage = (scores[i] / 100) * 100; // Assuming scores are out of 100 |
| 200 | +percentages.push(percentage); |
| 201 | +} |
| 202 | + |
| 203 | +for (let i = 0; i < studentNames.length; i++) { |
| 204 | +document.write(studentNames[i] + "'s percentage: " + percentages[i] + "%<br>"); |
| 205 | +} |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | +// 18. Initialize an array with color names. |
| 210 | +// Use document.write() method to display the array elements in your browser. |
| 211 | +// Your code goes here |
| 212 | +// Initialize an array with color names |
| 213 | +let colorNames = ["Red", "Green", "Blue", "Yellow", "Purple"]; |
| 214 | + |
| 215 | +for (let i = 0; i < colorNames.length; i++) { |
| 216 | + document.write(colorNames[i] + "<br>"); |
| 217 | +} |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | +// 19. Add new color names to the beginning and ending of the array using prompt() and unshift() or push() method. |
| 222 | +// Add two more color names to the beginning of the array. |
| 223 | +// Remove the first and last color names using shift() and pop() method. |
| 224 | +// Use prompt() to ask the user to enter a color name and add it at a specific index using splice() method. |
| 225 | +// Use prompt() to ask the user to enter a color name and remove it from the array using splice() method. |
| 226 | +// Your code goes here |
| 227 | + |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | +// 20. Write a program to sort the student scores in an array in ascending order using Array’s sort() method. |
| 232 | +// Your code goes here |
| 233 | +// Array containing student scores |
| 234 | +let scores = [85, 92, 78, 95, 88]; |
| 235 | + |
| 236 | +scores.sort((78, 95) => 85 - 88); |
| 237 | + |
| 238 | +document.write("Sorted Student Scores: " + scores.join('78, 95')); |
0 commit comments