Skip to content

Instantly share code, notes, and snippets.

View AnkitMaheshwariIn's full-sized avatar

Ankit Maheshwari AnkitMaheshwariIn

View GitHub Profile
@AnkitMaheshwariIn
AnkitMaheshwariIn / find-factorial.js
Last active March 31, 2022 17:26
program to find the factorial of a number
// 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
@AnkitMaheshwariIn
AnkitMaheshwariIn / count-down.js
Last active March 30, 2022 13:01
unction to count down numbers to 1 in JavaScript
// 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
@AnkitMaheshwariIn
AnkitMaheshwariIn / recursion-in-javascript.js
Created March 30, 2022 11:47
What is the Recursion technique
function recursiveFunction() {
// other code..
// calling itself, means calling recursive function
// from within recursiveFunction
recursiveFunction();
// other code..
}
@AnkitMaheshwariIn
AnkitMaheshwariIn / linear-search-javascript-array
Last active March 24, 2022 11:36
Code to search an item from an array using linear search
/* 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
@AnkitMaheshwariIn
AnkitMaheshwariIn / deletion-of-last-element
Created March 24, 2022 06:09
Code to delete last element from an array
/* 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]}`)
@AnkitMaheshwariIn
AnkitMaheshwariIn / deletion-of-first-element
Created March 24, 2022 06:06
Code to delete first element from an array
/* 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]}`)
@AnkitMaheshwariIn
AnkitMaheshwariIn / deletion-of-any-element
Created March 24, 2022 06:00
Code to delete element at a specific position from an array
/* 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]}`)
@AnkitMaheshwariIn
AnkitMaheshwariIn / travers-an-array.js
Last active March 23, 2022 12:22
travers an array
/* 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]}`)
}
@AnkitMaheshwariIn
AnkitMaheshwariIn / print3dArray.js
Last active March 23, 2022 12:22
print3dArray
/* 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]}`)
}
}
@AnkitMaheshwariIn
AnkitMaheshwariIn / bind-function-in-JavaScript.js
Last active August 8, 2021 12:49
When to use the bind function?
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