This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// program to find the factorial of a number | |
function findFactorial(num) { | |
// if number is 0 | |
if (num === 0) { | |
return 1 | |
} else { | |
// if number is positive, | |
// multiply number with response of function findFactorial(num - 1), | |
// having argument one less than num |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// function to count down numbers to 1 | |
function countDownFunc(number) { | |
// display the number | |
console.log(number); | |
// decrease the number value by 1 | |
const newNumber = number - 1; | |
// check condition, the countDownFunc will call itself till the value of newNumber becomes 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function recursiveFunction() { | |
// other code.. | |
// calling itself, means calling recursive function | |
// from within recursiveFunction | |
recursiveFunction(); | |
// other code.. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* RUN THIS CODE AND OPEN YOUR CONSOLE TO SEE THE OUTPUT */ | |
// Code to search an item from an array using linear search | |
// declare a function which will take two arguments: array and itemToSearch | |
const doLinearSearch = (arr, itemToSearch) => { | |
// iterate using for loop, where i is index and elem is each item | |
for (const [i, elem] of arr.entries()) { | |
if (elem === itemToSearch) { | |
// return index, if elem matched with item to search |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* RUN THIS CODE AND OPEN YOUR CONSOLE TO SEE THE OUTPUT */ | |
// Code to delete last element from an array | |
// declare an array | |
arr = [10, 20, 30, 40, 50, 60] | |
console.log("To print elements before deletion") | |
for (i = 0; i < arr.length; i++) { | |
console.log(`Element at index ${i} is ${arr[i]}`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* RUN THIS CODE AND OPEN YOUR CONSOLE TO SEE THE OUTPUT */ | |
// Code to delete first element from an array | |
// declare an array | |
arr = [10, 20, 30, 40, 50, 60] | |
console.log("To print elements before deletion") | |
for (i = 0; i < arr.length; i++) { | |
console.log(`Element at index ${i} is ${arr[i]}`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* RUN THIS CODE AND OPEN YOUR CONSOLE TO SEE THE OUTPUT */ | |
// Code to delete element at a specific position from an array | |
// declare an array | |
arr = [10, 20, 30, 40, 50, 60] | |
console.log("To print elements before deletion") | |
for (i = 0; i < arr.length; i++) { | |
console.log(`Element at index ${i} is ${arr[i]}`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* RUN THIS CODE AND OPEN YOUR CONSOLE TO SEE THE OUTPUT */ | |
// declare an array | |
arr = [10, 20, 30, 40, 50] | |
// travers an array | |
for (i = 0; i < arr.length; i++) { | |
// travers means visiting each element once | |
console.log(`Element at index ${i} is ${arr[i]}`) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* RUN THIS CODE AND OPEN YOUR CONSOLE TO SEE THE OUTPUT */ | |
const print3dArray = (arr3d) => { | |
console.log("I am in 3d array visualization") | |
for (let i = 0; i< arr3d.length; i ++) { | |
for (let k = 0; k< arr3d[i].length; k ++) { | |
for (let m = 0; m< arr3d[i][k].length; m ++) { | |
console.log(`value at i[${i}] k[${k}] m[${m}] is ${arr3d[i][k][m]}`) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fullName() { | |
return "Hello, this is " + this.first + " " + this.last; | |
} | |
console.log(fullName()); // Output: Hello, this is undefined undefined | |
// create a person object and pass its values to the fullName function | |
var person = {first: "Foo", last: "Bar"}; | |
// pass a person object to the fullName function using bind |
NewerOlder