In this article I am going to mention 6 Javascript Recursion examples. How Recursion works. It basically works by calling same function again and again until it reaches a base case that is usually an if condition and from here ...
Here we will simply create a static file using file module and then send the same file as the response. To view the result run this file using node app.js then visit localhost:8080 in the browser const fs = require("fs"); ...
var – Global scope when var variable is declared outside a function. It means that if you write following code if(true){ var x = 30 } console.log(x) In console it will print 30 so it means x has a ...
Suppose we have an array [1,2,3,4,5,6] and array is sorted in ascending order. Now we are asked to find the position of number 5 in the array using binary search. If number is found then return it’s index otherwise return ...
function sort012(arr, N) { let one=0; let zero=0; let two =0; for(let i=0; i<N; i++){ if(arr[i]==0){ zero++; }else if(arr[i]==1){ one++; }else{ two++; } } for(let k=0; k<N; k++){ if(zero !=0){ arr[k]=0; zero-- }else if(one !=0){ arr[k]=1; one-- }else if(two !=0){ ...
/** * @param {number[]} nums * @return {number} */ var maxSumOfSubArray = function(nums) { let localMax =0; let GlobalMax = Number.NEGATIVE_INFINITY; for(let i=0; i<nums.length; i++){ localMax = localMax+ nums[i]; GlobalMax = Math.max(localMax, GlobalMax); if(localMax <0){ localMax =0; } } return ...
In this post we will create a tool for coloring cells of a table. <div id="menuitems"> <h2 >Coloring Table<span id="timer"></span></h2> <button id="colorTable" class="btn btn-outline-dark btn-sm" data-bs-toggle="button" autocomplete="off" onclick="setColorPicker()"> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-paint-bucket" viewBox="0 0 16 16"> <path ...
In this article we will use recursion to count total digits in a given number function countDigits(n){ if(n ==0){ return n; } return 1+ countDigits(parseInt(n/10)); } let sum= countDigits(999999); console.log("result" + sum); How this works This is how countDigits function ...
Recursion in Javascript function sumOfDigits(n){ let num = parseInt(n/10); let digit = n%10; if(n ==0){ return n; } return digit + sumOfDigits(num); } let sum= sumOfDigits(99999); console.log("result" + sum); How this works 9 + sumOfDigits(9999) = returns 9 + 36 ...