|
| 1 | +// Declare and initialize an empty multidimensional array. |
| 2 | +// (Array of arrays) |
| 3 | +// 2. Declare and initialize a multidimensional array |
| 4 | +// representing the following matrix: |
| 5 | + |
| 6 | +document.write(`<h1> Empty Array </h1>`); |
| 7 | + |
| 8 | +let emptyArr = [ |
| 9 | + [0, 1, 2, 3], |
| 10 | + [1, 0, 1, 2], |
| 11 | + [2, 1, 0, 1], |
| 12 | +]; |
| 13 | +let matrix = ""; |
| 14 | +for (let i = 0; i < emptyArr.length; i++) { |
| 15 | + matrix += emptyArr[i].join(" ") + "<br>"; // Join each row with spaces |
| 16 | +} |
| 17 | + |
| 18 | +document.write(matrix); |
| 19 | +// Write a program to print numeric counting from 1 to 10. |
| 20 | + |
| 21 | +document.write(`<h1> Numeric Counting from 1 to 10 </h1>`); |
| 22 | +for (let i = 1; i <= 10; i++) { |
| 23 | + document.write(`${i}</br>`); |
| 24 | +} |
| 25 | + |
| 26 | +// Write a program to print multiplication table of any |
| 27 | +// number using for loop. Table number & length should be |
| 28 | +// taken as an input from user. |
| 29 | +document.write(`<h1> Table Printer </h1>`); |
| 30 | +let table = prompt("Enter a number to print Table"); |
| 31 | +let tableLength = prompt("Enter a number to end table"); |
| 32 | + |
| 33 | +let print = ""; |
| 34 | +for (let i = 1; i <= tableLength; i++) { |
| 35 | + print += `${table} x ${i} = ${table * i}<br>`; |
| 36 | +} |
| 37 | +document.write(print); |
| 38 | + |
| 39 | +// Write a program to print items of the following array |
| 40 | +// using for loop: |
| 41 | +// fruits = ["apple", "banana", "mango", "orange", |
| 42 | +// "strawberry"] |
| 43 | +document.write(`<h1> Print Items (using for loop) </h1>`); |
| 44 | +let fruits = ["apple", "banana", "mango", "orange", "strawberry"]; |
| 45 | +for (i = 0; i < fruits.length; i++) { |
| 46 | + document.write(`${fruits[i]} </br>`); |
| 47 | +} |
| 48 | + |
| 49 | +// Generate the following series in your browser. See |
| 50 | +// example output. |
| 51 | + |
| 52 | +// a. Counting: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 |
| 53 | +document.write(`<h1> Counting </h1>`); |
| 54 | +let counting = []; |
| 55 | +for (i = 1; i <= 10; i++) { |
| 56 | + counting.push(i); |
| 57 | +} |
| 58 | +document.write(`${counting.join(" , ")} </br>`); |
| 59 | + |
| 60 | +// b. Reverse counting: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 |
| 61 | +document.write(`<h1> Reverse counting </h1>`); |
| 62 | + |
| 63 | +let reverseCounting = []; |
| 64 | +for (i = 10; i > 0; i--) { |
| 65 | + reverseCounting.push(i); |
| 66 | +} |
| 67 | +document.write(`${reverseCounting.join(" , ")} </br>`); |
| 68 | + |
| 69 | +// c. Even: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 |
| 70 | +document.write(`<h1> Even </h1>`); |
| 71 | +let even = []; |
| 72 | + |
| 73 | +for (i = 0; i <= 20; i += 2) { |
| 74 | + even.push(i); |
| 75 | +} |
| 76 | +document.write(`${even.join(" , ")} </br>`); |
| 77 | + |
| 78 | +// d. Odd: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 |
| 79 | +document.write(`<h1> Odd </h1>`); |
| 80 | +let oddNum = []; |
| 81 | +for (let i = 1; i < 20; i += 2) { |
| 82 | + oddNum.push(i); |
| 83 | +} |
| 84 | +document.write(`${oddNum.join(" , ")} </br>`); |
| 85 | + |
| 86 | +// e. Series: 2k, 4k, 6k, 8k, 10k, 12k, 14k, 16k, 18k, 20k\ |
| 87 | +document.write(`<h1> Series </h1>`); |
| 88 | +let series = []; |
| 89 | +for (let i = 1; i <= 10; i++) { |
| 90 | + series.push(2 * i); |
| 91 | +} |
| 92 | +document.write(`${series.join("k , ")} </br>`); |
| 93 | + |
| 94 | +// You have an array |
| 95 | +// A = ["cake", "apple pie", "cookie", "chips", "patties"] |
| 96 | +// Write a program to enable "search by user input" in an |
| 97 | +// array. |
| 98 | +// After searching, prompt the user whether the given item is |
| 99 | +// found in the list or not. |
| 100 | + |
| 101 | +document.write(`<h1> Search by User Input </h1>`); |
| 102 | +let A = ["cake", "apple pie", "cookie", "chips", "patties"]; |
| 103 | + |
| 104 | +let userSearch = prompt("what do you want to order?"); |
| 105 | + |
| 106 | +if (A.includes(userSearch)) { |
| 107 | + document.write( |
| 108 | + `${userSearch} is available at index ${A.indexOf(userSearch)} in our bakery` |
| 109 | + ); |
| 110 | +} else { |
| 111 | + document.write(`Sorry! ${userSearch} is not available at our bakery`); |
| 112 | +} |
| 113 | + |
| 114 | +// Write a program to identify the largest number in the |
| 115 | +// given array. |
| 116 | +// A = [24, 53, 78, 91, 12]. |
| 117 | + |
| 118 | +document.write(`<h1> Largest Number </h1>`); |
| 119 | + |
| 120 | +let C = [24, 53, 78, 91, 12]; |
| 121 | +let largest = C[0]; |
| 122 | + |
| 123 | +for (let i = 1; i < C.length; i++) { |
| 124 | + if (C[i] > largest) { |
| 125 | + largest = C[i]; |
| 126 | + } |
| 127 | +} |
| 128 | +document.write(`The largest number is: ${largest}`); |
| 129 | + |
| 130 | +// Write a program to identify the smallest number in the |
| 131 | +// given array. |
| 132 | +document.write(`<h1> Smallest Number </h1>`); |
| 133 | + |
| 134 | +let B = [24, 53, 78, 91, 12]; |
| 135 | +let smallest = B[0]; |
| 136 | + |
| 137 | +for (let i = 1; i < B.length; i++) { |
| 138 | + if (B[i] < smallest) { |
| 139 | + smallest = B[i]; |
| 140 | + } |
| 141 | +} |
| 142 | +document.write(`The smallest number is: ${smallest}`); |
| 143 | + |
| 144 | +// Write a program to print multiples of 5 ranging 1 to |
| 145 | +// 100. |
| 146 | + |
| 147 | +document.write(`<h1> multiples of 5 ranging 1 to 100 </h1>`); |
| 148 | +let tables = 5; |
| 149 | +for (let i = 1; i <= 20; i++) { |
| 150 | + let printTable = ` ${tables * i},`; |
| 151 | + document.write(printTable); |
| 152 | +} |
0 commit comments