|
| 1 | +/*- |
| 2 | +function a() { |
| 3 | + var b=10; |
| 4 | + c(); |
| 5 | + function c() { |
| 6 | + console.log(b); |
| 7 | + |
| 8 | + } |
| 9 | + |
| 10 | +} |
| 11 | +a(); |
| 12 | +
|
| 13 | +// Output: 10 |
| 14 | +*/ |
| 15 | +/* |
| 16 | +2.WAP In JS to print 1 to 5 with 1 sec interval then 2 then 3 so on using setTimeout and closure |
| 17 | +
|
| 18 | +function x(){ |
| 19 | + for(let i=1; i<=5; i++){ |
| 20 | + setTimeout(function(){ |
| 21 | + console.log(i); |
| 22 | + },i* 1000); |
| 23 | + } |
| 24 | +} |
| 25 | +x();// Output: 1 2 3 4 5 |
| 26 | +
|
| 27 | +*/ |
| 28 | + |
| 29 | +/* |
| 30 | +3 |
| 31 | +function x(){ |
| 32 | + for(var i=1; i<=5; i++){ |
| 33 | + function closer(x){ |
| 34 | + setTimeout(function(){ |
| 35 | + console.log(x); |
| 36 | + },x* 1000); |
| 37 | +
|
| 38 | + } |
| 39 | + closer(i); |
| 40 | + |
| 41 | + } |
| 42 | +} |
| 43 | +x(); |
| 44 | +// Output: 1 2 3 4 5 |
| 45 | +
|
| 46 | +
|
| 47 | +4. wAP to find area of circle using function and array |
| 48 | +
|
| 49 | +const radius =[5,6,7,8,9]; |
| 50 | +const area = function(radius){ |
| 51 | + const Output =[]; |
| 52 | + for(let i=0; i<radius.length; i++){ |
| 53 | + Output.push(Math.PI*radius[i]*radius[i]); |
| 54 | + } |
| 55 | + return Output; |
| 56 | +} |
| 57 | +
|
| 58 | +console.log(area(radius)); |
| 59 | +
|
| 60 | +What is Higher order function? |
| 61 | +ans: A higher-order function is a function that takes |
| 62 | + one or more functions as arguments, returns a function as its result, |
| 63 | + or both. In other words, it can operate on functions, either by taking them as parameters or by returning them. |
| 64 | +
|
| 65 | +5. Higher order function to find area, circumference and diameter of circle using function and array |
| 66 | +const radius =[5,6,7,8,9]; |
| 67 | +
|
| 68 | +const area = function(radius){ |
| 69 | + |
| 70 | + return Math.PI*radius*radius; |
| 71 | +}; |
| 72 | +
|
| 73 | +const circumference = function(radius){ |
| 74 | + |
| 75 | + return 2*Math.PI*radius; |
| 76 | + }; |
| 77 | +
|
| 78 | +const diameter = function(radius){ |
| 79 | + |
| 80 | + return 2*radius; |
| 81 | + } |
| 82 | +
|
| 83 | +const calculate = function(radius, logic){ |
| 84 | + const Output =[]; |
| 85 | + for(let i=0; i<radius.length; i++){ |
| 86 | + Output.push(logic(radius[i])); |
| 87 | + } |
| 88 | + return Output; |
| 89 | +} |
| 90 | +
|
| 91 | +console.log(calculate(radius, area)); |
| 92 | +console.log(calculate(radius, circumference)); |
| 93 | +console.log(calculate(radius, diameter)); |
| 94 | +
|
| 95 | +
|
| 96 | +6. Map |
| 97 | +
|
| 98 | +const r=[5,6,7,8,9]; |
| 99 | +const Double = function(x){ |
| 100 | + return 2*x; |
| 101 | +}; |
| 102 | +
|
| 103 | +function Square(x){ |
| 104 | + return x*x; |
| 105 | +} |
| 106 | +
|
| 107 | +function Cube(x){ |
| 108 | + return x*x*x; |
| 109 | +} |
| 110 | +
|
| 111 | +function SquareRoot(x){ |
| 112 | + return Math.sqrt(x); |
| 113 | +} |
| 114 | +function Binary(x){ |
| 115 | + return x.toString(2); |
| 116 | +} |
| 117 | +
|
| 118 | +const Output=r.map(Double) |
| 119 | +const Output1=r.map(Binary) |
| 120 | +console.log(Output1); |
| 121 | +console.log(Output); |
| 122 | +
|
| 123 | +7. Filter |
| 124 | +
|
| 125 | +const r=[5,6,7,8,9]; |
| 126 | +
|
| 127 | +function isOdd(x){ |
| 128 | + return x%2!==0; |
| 129 | +} |
| 130 | +function isEven(x){ |
| 131 | + return x%2===0; |
| 132 | +} |
| 133 | +
|
| 134 | +function isPrime(x){ |
| 135 | + if(x<2) return false; |
| 136 | + for(let i=2; i<=Math.sqrt(x); i++){ |
| 137 | + if(x%i===0) return false; |
| 138 | + } |
| 139 | + return true; |
| 140 | +} |
| 141 | +function isPerfectSquare(x){ |
| 142 | + return Number.isInteger(Math.sqrt(x)); |
| 143 | +} |
| 144 | +function isArmstrong(x){ |
| 145 | + const str = x.toString(); |
| 146 | + const len = str.length; |
| 147 | + let sum = 0; |
| 148 | + for(let i=0; i<len; i++){ |
| 149 | + sum += Math.pow(Number(str[i]), len); |
| 150 | + } |
| 151 | + return sum === x; |
| 152 | +} |
| 153 | +
|
| 154 | +const Output=r.filter(isOdd) |
| 155 | +const Output1=r.filter(isEven) |
| 156 | +console.log(Output1); |
| 157 | +console.log(Output); |
| 158 | +
|
| 159 | +8. Reduce |
| 160 | +
|
| 161 | +const r=[5,5,5,5,5]; |
| 162 | +
|
| 163 | +function sumofall(x){ |
| 164 | + let sum = 0; |
| 165 | + for(let i=0; i<x.length; i++){ |
| 166 | + sum += x[i]; |
| 167 | + } |
| 168 | + return sum; |
| 169 | +} |
| 170 | +
|
| 171 | +const Output=r.reduce((acc, curr) => acc + curr, 0); |
| 172 | +
|
| 173 | +console.log(Output); |
| 174 | +*/ |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | +// Four Example of falsy values in JS |
| 179 | +/* |
| 180 | +// 1. undefined |
| 181 | +var a; |
| 182 | +console.log(typeof(a));// undefined |
| 183 | +console.log(Boolean(a)); |
| 184 | +// 2. null |
| 185 | +var a = null; |
| 186 | +console.log(typeof(a));// object |
| 187 | +console.log(Boolean(a));// false |
| 188 | +
|
| 189 | +
|
| 190 | +// 3. NaN |
| 191 | +var a = NaN; |
| 192 | +console.log(typeof(a)); |
| 193 | +console.log(Boolean(a)); |
| 194 | +// 4. 0 |
| 195 | +var a = 0; |
| 196 | +console.log(typeof(a));// number |
| 197 | +console.log(Boolean(a));// false |
| 198 | +*/ |
| 199 | +/* |
| 200 | +// 5. "" (empty string) |
| 201 | +var a = ""; |
| 202 | +console.log(typeof(a));// string |
| 203 | +console.log(Boolean(a));// false |
| 204 | +*/ |
| 205 | + |
| 206 | +/* |
| 207 | +let b; |
| 208 | +console.log(b);// undefined |
| 209 | +
|
| 210 | +*/ |
| 211 | + |
| 212 | + |
| 213 | +/* |
| 214 | +var a; |
| 215 | +console.log(a);// undefined |
| 216 | +
|
| 217 | +*/ |
| 218 | +/* |
| 219 | +const a; |
| 220 | +console.log(a);// SyntaxError: Missing initializer in const declaration |
| 221 | +
|
| 222 | +*/ |
| 223 | + |
| 224 | + |
| 225 | +/* |
| 226 | +console.log(a); // ReferenceError: Cannot access 'a' before initialization |
| 227 | +let a = 10; |
| 228 | +*/ |
| 229 | + |
| 230 | +/* |
| 231 | +console.log(a); // undefined |
| 232 | +var a; // undefined |
| 233 | +*/ |
| 234 | + |
| 235 | +/* |
| 236 | +console.log(a); // ReferenceError: Cannot access 'a' before initialization |
| 237 | +const a; |
| 238 | +*/ |
| 239 | + |
| 240 | +/* |
| 241 | +console.log(name);// undefined |
| 242 | +var name; |
| 243 | +*/ |
| 244 | + |
| 245 | +/* |
| 246 | +function sum(a, b) { |
| 247 | + return a + b; |
| 248 | +} |
| 249 | +
|
| 250 | +console.log(sum(2, 3)); // 5* |
| 251 | +*/ |
| 252 | + |
| 253 | +/* Lambda function or Arrow function Example give the output of 1 to 5 |
| 254 | + |
| 255 | + What is Lambda function or Arrow function? |
| 256 | + ans: An arrow function is a shorter syntax for writing function expressions in JavaScript. |
| 257 | + example: const add = (a, b) => a + b; |
| 258 | + It is a more concise way to write functions, especially when using them as arguments to higher-order functions. |
| 259 | + what is expression? |
| 260 | + ans: An expression is a piece of code that produces a value. It can be a variable, a function call, or an operation that evaluates to a value. |
| 261 | +
|
| 262 | +
|
| 263 | +const numbers = [1, 2, 3, 4, 5]; |
| 264 | +const squares = numbers.map(num => num * num); |
| 265 | +console.log(squares); // Output: [1, 4, 9, 16, 25] |
| 266 | +
|
| 267 | +
|
| 268 | +
|
| 269 | +function sum(...num){ |
| 270 | + return num.reduce((a,b)=>a+b,0) |
| 271 | +} |
| 272 | +
|
| 273 | +console.log(sum(1,2)) |
| 274 | +console.log(sum(1,2,3)) |
| 275 | +console.log(sum(1,2,3,4)) |
| 276 | +
|
| 277 | +
|
| 278 | +console.log(typeof null);//object |
| 279 | +
|
| 280 | +
|
| 281 | + let a = 5; |
| 282 | +let b = "5"; |
| 283 | +console.log(a == b); //true |
| 284 | +console.log(a === b); //false |
| 285 | +
|
| 286 | +
|
| 287 | +console.log(0.1 + 0.2 === 0.3); //false |
| 288 | +
|
| 289 | +
|
| 290 | +console.log([] == []);//false |
| 291 | +
|
| 292 | +console.log(typeof NaN); //number |
| 293 | +
|
| 294 | +let x; |
| 295 | +console.log(x + 2); //NAN |
| 296 | +
|
| 297 | +
|
| 298 | +let x; |
| 299 | +console.log(x - 2); //NAN |
| 300 | +
|
| 301 | +
|
| 302 | +
|
| 303 | +let x; |
| 304 | +console.log(x * 2); //NAN |
| 305 | +
|
| 306 | +
|
| 307 | +let x; |
| 308 | +console.log(x / 2); //NAN |
| 309 | +
|
| 310 | +
|
| 311 | +console.log("5" - 2); //3 number |
| 312 | +console.log("5" + 2); //52 string |
| 313 | +
|
| 314 | +console.log(typeof typeof 1);//string typeof number number is string |
| 315 | +
|
| 316 | +console.log(typeof typeof 10.25);//string |
| 317 | +
|
| 318 | +console.log(typeof typeof null);//string |
| 319 | +
|
| 320 | +console.log(typeof typeof undefined);//string |
| 321 | +
|
| 322 | +
|
| 323 | +console.log( typeof typeof Infinity);//string |
| 324 | +
|
| 325 | +console.log(typeof Object);//function |
| 326 | +
|
| 327 | +
|
| 328 | +console.log(typeof typeof true);//string |
| 329 | +
|
| 330 | +console.log(3 > 2 > 1); //false true>1 type coreceion false |
| 331 | +
|
| 332 | +let a = [1, 2, 3]; |
| 333 | +let b = a; |
| 334 | +b.push(4); |
| 335 | +console.log(a);//[1,2,3,4] |
| 336 | +*/ |
| 337 | + |
| 338 | + |
| 339 | + |
| 340 | + |
| 341 | + |
| 342 | + |
| 343 | + |
| 344 | + |
| 345 | + |
| 346 | + |
0 commit comments