Number of Carries Required While Adding Two Numbers in JavaScript

AmitDiwan
Updated on 19-Apr-2021 07:14:05

354 Views

ProblemWe are required to write a JavaScript function that takes in two numbers.Our function should count the number of carries we are required to take while adding those numbers as if we were adding them on paper.Like in the following image while adding 179 and 284, we had used carries two times, so for these two numbers, our function should return 2.ExampleFollowing is the code − Live Democonst num1 = 179; const num2 = 284; const countCarries = (num1 = 1, num2 = 1) => {    let res = 0;    let carry = 0;    while(num1 + num2){   ... Read More

Finding Smallest Sum After Transformations in JavaScript

AmitDiwan
Updated on 19-Apr-2021 07:09:30

170 Views

ProblemWe are required to write a JavaScript function that takes in an array of positive integers. We can transform its elements by running the following operation on them as many times as required −if arr[i] > arr[j] then arr[i] = arr[i] - arr[j]When no more transformations are possible, our function should return its sum.ExampleFollowing is the code − Live Democonst arr = [6, 9, 21]; const smallestSum = (arr = []) => {    const equalNums = arr => arr.reduce((a, b) => {       return (a === b) ? a : NaN;    });    if(equalNums(arr)){       ... Read More

Switch Positions of Selected Characters in a String in JavaScript

AmitDiwan
Updated on 19-Apr-2021 07:07:01

374 Views

ProblemWe are required to write a JavaScript function that takes in a string that contains only the letter ‘k’, ‘l’ and ‘m’.The task of our function is to switch the positions of k with that of l leaving all the instances of m at their positions.ExampleFollowing is the code − Live Democonst str = 'kklkmlkk'; const switchPositions = (str = '') => {    let res = "";    for(let i = 0; i < str.length; i++){       if (str[i] === 'k') {          res += 'l';       } else if (str[i] === 'l') {          res += 'k';       } else {          res += str[i];       };    };    return res; }; console.log(switchPositions(str));OutputFollowing is the console output −llklmkll

Difference Between Internal and External Fragmentation

AmitDiwan
Updated on 19-Apr-2021 06:32:01

1K+ Views

In this post, we will understand the difference between internal and external fragmentation −Internal FragmentationThe difference between the memory allocated and the space required is known as internal fragmentation.In this fragmentation, fixed-sized memory blocks are used to process data.This process occurs when a method or process is larger than the required memory.The method used in internal fragmentation is ‘best-fit’ block.It occurs when the memory is divided into fixed sized partitions.External FragmentationThe unused spaces that is formed between fragments of non-contiguous memory, which are too small to help with a new process, is known as external fragmentation.It uses variable-sized memory blocks ... Read More

Difference Between Contiguous and Non-Contiguous Memory Allocation

AmitDiwan
Updated on 19-Apr-2021 06:30:11

2K+ Views

In this post, we will understand the difference between contiguous and non-contiguous memory allocation −Contiguous Memory AllocationIn this allocation type, the consecutive blocks of memory is allocated to a file/process.It executes quickly in comparison to non-contiguous memory.It is easy to be controlled by the operating system.Minimum overhead since there are not many address translation when a process is being executed.There is internal fragmentation in contiguous memory allocation.There are different types of partitions: Single partition allocation and multi-partition allocation.Memory gets wasted.The swapped-in process is arranged in the originally allocated space itself.Non-contiguous Memory AllocationIn this type of allocation, separate blocks of memory ... Read More

Difference Between Linker and Loader

AmitDiwan
Updated on 19-Apr-2021 06:07:03

13K+ Views

In this post, we will understand the difference between a linker and a loader −LinkerThe main function of the linker is to generate executable files.The linker takes the input as the object code which would be generated by a compiler/assembler.The process of linking can be understood as a method to combine different snippets of code in order to obtain executable code.There are two types of linkers available: Linkage Editor and Dynamic Linker.Linker also helps combine all the object modules.Linker is responsible to arrange the objects in the program’s address space.LoaderThe main function of a loader is to load executable files ... Read More

Reversing All Alphabetic Characters in JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:45:11

138 Views

ProblemWe are required to write a JavaScript function that takes in a string str. The job of our function is to reverse it, omitting all non-alphabetic characters.ExampleFollowing is the code − Live Democonst str = 'exa13mple'; function reverseLetter(str) {    const res = str.split('')    .reverse()    .filter(val => /[a-zA-Z]/.test(val))    .join('');    return res; }; console.log(reverseLetter(str));Outputelpmaxe

Finding Sum of Sequence Up to a Specified Accuracy Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:40:35

182 Views

ProblemSuppose the following sequence:Seq: 1/1 , 1/1x2 , 1/1x2x3 , 1/1x2x3x4 , ....The nth term of this sequence will be −1 / 1*2*3 * ... nWe are required to write a JavaScript function that takes in a number n, and return the sum of first n terms of this sequence.ExampleFollowing is the code − Live Democonst num = 12; const seriesSum = (num = 1) => {    let m = 1;    let n = 1;    for(let i = 2; i < num + 1; i++){       m *= i;       n += (m * -1);    };    return n; }; console.log(seriesSum(num));Output-522956311

Implement Custom Function Like String Prototype Split in JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:39:53

1K+ Views

ProblemWe are required to write a JavaScript function that lives on the prototype object of the String class.It should take in a string separator as the only argument (although the original split function takes two arguments). And our function should return an array of parts of the string separated and split by the separator.ExampleFollowing is the code − Live Democonst str = 'this is some string'; String.prototype.customSplit = (sep = '') => {    const res = [];    let temp = '';    for(let i = 0; i < str.length; i++){       const el = str[i];     ... Read More

Implement Partial Sum Over an Array Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 13:39:29

372 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should construct and return a new array in which each corresponding element is the sum of all the elements right to it (including it) in the input array.ExampleFollowing is the code − Live Democonst arr = [5, 6, 1, 3, 8, 11]; const partialSum = (arr = []) => {    let sum = arr.reduce((acc, val) => acc + val);    const res = [];    let x = 0;    if(arr.length === 0){       return [0];    }    for(let i = 0; i

Advertisements