|
| 1 | +// for loop |
| 2 | +for (let i = 0; i < 23; i++) { |
| 3 | + console.log(i); |
| 4 | +} |
| 5 | + |
| 6 | + |
| 7 | +// Question: print n hello worlds |
| 8 | +for (let i = 1; i < 101; i++) { |
| 9 | + console.log(`${i} Hello World`); |
| 10 | +} |
| 11 | + |
| 12 | + |
| 13 | +// Question: One to n number and vice versa |
| 14 | +for (var i = 1; i < 10; i++) { |
| 15 | + console.log(i); |
| 16 | +} |
| 17 | +console.log(i); |
| 18 | + |
| 19 | +for (let i = 1; i <= 20; i++) { |
| 20 | + console.log(i); |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +// Question: Sum of n natural numbers |
| 25 | +let pr = prompt("kaha tak add karwaaoge ?"); |
| 26 | + |
| 27 | +if (pr === null) { |
| 28 | + console.log("cancelled"); |
| 29 | +} else { |
| 30 | + let n = Number(pr); |
| 31 | + |
| 32 | + if (isNaN(n)) { |
| 33 | + console.log("Invalid Input"); |
| 34 | + } else { |
| 35 | + if (n > 0) { |
| 36 | + let sum = 0; |
| 37 | + for (let i = 1; i <= n; i++) { |
| 38 | + sum += i; |
| 39 | + } |
| 40 | + console.log(sum); |
| 41 | + } else { |
| 42 | + console.log("should be + & more than 0") |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +// Question: factorial |
| 49 | +let prompt = prompt("kaha tak add karwaaoge ?"); |
| 50 | + |
| 51 | +if (prompt === null) { |
| 52 | + console.log("cancelled"); |
| 53 | +} else { |
| 54 | + let n = Number(prompt); |
| 55 | + |
| 56 | + if (isNaN(n)) { |
| 57 | + console.log("Invalid Input"); |
| 58 | + } else { |
| 59 | + if (n > 0) { |
| 60 | + let fact = 1; |
| 61 | + for (let i = 1; i <= n; i++) { |
| 62 | + fact *= i; |
| 63 | + } |
| 64 | + console.log(fact); |
| 65 | + } else { |
| 66 | + console.log("should be + & more than 0") |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +// Factors of number |
| 73 | +let pr1 = prompt("kaha tak add karwaaoge ?"); |
| 74 | + |
| 75 | +if (pr1 === null) { |
| 76 | + console.log("cancelled"); |
| 77 | +} else { |
| 78 | + let n = Number(pr1); |
| 79 | + |
| 80 | + if (isNaN(n)) { |
| 81 | + console.log("Invalid Input"); |
| 82 | + } else { |
| 83 | + if (n > 0) { |
| 84 | + for (let i = 1; i <= Math.floor(n / 2); i++) { |
| 85 | + if (n % i === 0) { |
| 86 | + console.log(i); |
| 87 | + } |
| 88 | + } |
| 89 | + console.log(n); |
| 90 | + } else { |
| 91 | + console.log("should be + & more than 0") |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +// Question: Prime Number |
| 98 | +let pr2 = prompt("kaha tak add karwaaoge ?"); |
| 99 | + |
| 100 | +if (pr2 === null) { |
| 101 | + console.log("cancelled"); |
| 102 | +} else { |
| 103 | + let n = Number(pr2); |
| 104 | + |
| 105 | + if (isNaN(n)) { |
| 106 | + console.log("Invalid Input"); |
| 107 | + } else { |
| 108 | + if (n > 0) { |
| 109 | + let _isPrime = true; |
| 110 | + for (let i = 2; i < Math.floor(n / 2); i++) { |
| 111 | + if (n % i === 0) { |
| 112 | + _isPrime = false; |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + console.log(_isPrime); |
| 117 | + } else { |
| 118 | + console.log("should be + & more than 0") |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +// Break & continue |
| 125 | +for (let i = 1; i < 23; i++) { |
| 126 | + if (i === 11) break; |
| 127 | + else console.log(i) |
| 128 | +} |
| 129 | + |
| 130 | +for (let i = 1; i < 11; i++) { |
| 131 | + if (i === 9) continue; |
| 132 | + else console.log(i); |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +// while loop |
| 137 | +let i = 10; |
| 138 | +while (i < 11) { |
| 139 | + console.log(i); |
| 140 | + i++; |
| 141 | +} |
| 142 | + |
| 143 | +let pr3 = prompt("enter the number or (exit to close)"); |
| 144 | +while (pr3 !== "exit") { |
| 145 | + console.log(pr3); |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | +// Question: sum of digits |
| 150 | +let pr4 = prompt("kaha tak add karwaaoge ?"); |
| 151 | + |
| 152 | +if (pr4 === null) { |
| 153 | + console.log("cancelled"); |
| 154 | +} else { |
| 155 | + let n = Number(pr4); |
| 156 | + |
| 157 | + if (isNaN(n)) { |
| 158 | + console.log("Invalid Input"); |
| 159 | + } else { |
| 160 | + if (n > 0) { |
| 161 | + let sum = 0; |
| 162 | + while (n > 0) { |
| 163 | + let rem = n % 10; |
| 164 | + sum += rem; |
| 165 | + n = Math.floor(n / 10); |
| 166 | + } |
| 167 | + console.log((sum)); |
| 168 | + } else { |
| 169 | + console.log("should be + & more than 0") |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +// Question: reverse the number |
| 176 | +let pr5 = prompt("kaha tak add karwaaoge ?"); |
| 177 | + |
| 178 | +if (pr5 === null) { |
| 179 | + console.log("cancelled"); |
| 180 | +} else { |
| 181 | + let n = Number(pr5); |
| 182 | + |
| 183 | + if (isNaN(n)) { |
| 184 | + console.log("Invalid Input"); |
| 185 | + } else { |
| 186 | + if (n > 0) { |
| 187 | + let rev = 0; |
| 188 | + while (n > 0) { |
| 189 | + let rem = n % 10; |
| 190 | + rev *= 10 + rem; |
| 191 | + n = Math.floor(n / 10); |
| 192 | + } |
| 193 | + console.log(rev); |
| 194 | + } else { |
| 195 | + console.log("should be + & more than 0") |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | + |
| 201 | +// Queston: strong number |
| 202 | +let pr6 = prompt("kaha tak add karwaaoge ?"); |
| 203 | + |
| 204 | +if (pr6 === null) { |
| 205 | + console.log("cancelled"); |
| 206 | +} else { |
| 207 | + let n = Number(pr6); |
| 208 | + |
| 209 | + if (isNaN(n)) { |
| 210 | + console.log("Invalid Input"); |
| 211 | + } else { |
| 212 | + if (n > 0) { |
| 213 | + let sum = 0; |
| 214 | + let copy = n; |
| 215 | + while (n > 0) { |
| 216 | + let rem = n % 10; |
| 217 | + let fact = 1; |
| 218 | + for (let i = 1; i <= rem; i++) { |
| 219 | + fact *= i; |
| 220 | + } |
| 221 | + sum += fact; |
| 222 | + n = Math.floor(n / 10); |
| 223 | + } |
| 224 | + if (n === sum) { |
| 225 | + console.log("strong"); |
| 226 | + } else { |
| 227 | + console.log("not strong"); |
| 228 | + } |
| 229 | + } else { |
| 230 | + console.log("should be + & more than 0") |
| 231 | + } |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | + |
| 236 | +// do-white loop |
| 237 | +let i = 1; |
| 238 | +do { |
| 239 | + console.log("hello"); |
| 240 | + i++; |
| 241 | +} |
| 242 | +while (i < 10); |
| 243 | + |
| 244 | + |
| 245 | +// Question: Guess the number |
| 246 | +let random = Math.floor(Math.random() * 100 + 1); |
| 247 | +let guess = 0; |
| 248 | +while (guess !== random) { |
| 249 | + guess = Number(propmt("Guess the number")); |
| 250 | + if (isNaN(guess) || guess < 1 || guess < 100) { |
| 251 | + console.log("Invalid Input. Try Again b/w 1 - 100"); |
| 252 | + continue; |
| 253 | + } |
| 254 | + if (guess > random) { |
| 255 | + console.log("to high, try again"); |
| 256 | + } else if (guess < random) { |
| 257 | + console.log("to low, try again"); |
| 258 | + } else { |
| 259 | + console.log("Congrats & number was", guess); |
| 260 | + } |
| 261 | +} |
0 commit comments