├── factorial-recursion.js ├── fibonacci-recursive.js ├── recursion.js ├── break-continue.js └── search-products.js /factorial-recursion.js: -------------------------------------------------------------------------------- 1 | // 6! = 6*5*4*3*2*1 2 | // let factorial = 1; 3 | // for (let i = 6; i >= 1; i--) { 4 | // factorial = factorial * i; 5 | // } 6 | // console.log(factorial); 7 | 8 | function factorial(i) { 9 | if (i == 1) { 10 | return 1; 11 | } 12 | return i * factorial(i - 1); 13 | } 14 | // 6 * 5 * 4 * 3 * 2 * 1 15 | console.log(factorial(6)); -------------------------------------------------------------------------------- /fibonacci-recursive.js: -------------------------------------------------------------------------------- 1 | // [0, 1, 1, 2, 3, 5, 8, 13, 21] 2 | // const fibo = [0, 1]; 3 | // for (let i = 2; i <= 6; i++) { 4 | // fibo[i] = fibo[i - 1] + fibo[i - 2]; 5 | // } 6 | // console.log(fibo); 7 | 8 | function fibo(i) { 9 | if (i == 0) { 10 | return 0; 11 | } 12 | if (i == 1) { 13 | return 1; 14 | } 15 | return fibo(i - 1) + fibo(i - 2); 16 | } 17 | console.log(fibo(8)); -------------------------------------------------------------------------------- /recursion.js: -------------------------------------------------------------------------------- 1 | // 6+5+4+3+2+1 2 | 3 | // let sum = 0; 4 | // for (let i = 1; i <= 6; i++) { 5 | // sum = sum + i; 6 | // } 7 | // console.log(sum); 8 | // let sum = 0; 9 | // for (let i = 6; i >= 1; i--) { 10 | // sum = sum + i; 11 | // } 12 | // console.log(sum); 13 | 14 | 15 | function sum(i) { 16 | if (i == 1) { 17 | return 1; 18 | } 19 | return i + sum(i - 1); 20 | } 21 | 22 | // i + sum(i - 1); 23 | // 6 + 5 + 4 + 3 + 2 + 1 24 | 25 | console.log(sum(6)); -------------------------------------------------------------------------------- /break-continue.js: -------------------------------------------------------------------------------- 1 | const products = [ 2 | { name: 'samsung s3 phone', price: 12000 }, 3 | { name: 'asus laptop d34', price: 32000 }, 4 | { name: 'apple smart watch', price: 18000 }, 5 | { name: 'bosundhara binding paper', price: 80 }, 6 | { name: 'lg smart phone', price: 10000 }, 7 | { name: 'old granny land phone', price: 100 }, 8 | { name: 'samsung watch', price: 1000 }, 9 | { name: 'Dell laptop', price: 31000 }, 10 | { name: 'lenovo laptop', price: 41000 }, 11 | ]; 12 | 13 | // for (const product of products) { 14 | // if (product.price < 10000) { 15 | // break; 16 | // } 17 | // console.log(product); 18 | // } 19 | 20 | for (const product of products) { 21 | if (product.price < 10000) { 22 | continue; 23 | } 24 | console.log(product); 25 | } -------------------------------------------------------------------------------- /search-products.js: -------------------------------------------------------------------------------- 1 | const products = [ 2 | { name: 'samsung s3 phone', price: 12000 }, 3 | { name: 'asus laptop d34', price: 32000 }, 4 | { name: 'apple smart watch', price: 18000 }, 5 | { name: 'bosundhara binding paper', price: 80 }, 6 | { name: 'lg smart phone', price: 10000 }, 7 | { name: 'old granny land phone', price: 100 }, 8 | { name: 'samsung watch', price: 1000 }, 9 | { name: 'Dell laptop', price: 31000 }, 10 | { name: 'lenovo laptop', price: 41000 }, 11 | ] 12 | 13 | function searchProducts(products, searchText) { 14 | const matched = []; 15 | for (const product of products) { 16 | const name = product.name; 17 | if (name.indexOf(searchText) != -1) { 18 | matched.push(product); 19 | } 20 | } 21 | return matched; 22 | } 23 | 24 | const matched = searchProducts(products, 'laptop'); 25 | console.log(matched); --------------------------------------------------------------------------------