Found 9150 Articles for Object Oriented Programming

Write an algorithm that takes an array and moves all of the zeros to the end JavaScript

AmitDiwan
Updated on 24-Aug-2020 05:53:19

300 Views

We have to write a function that takes in an array and moves all the zeroes present in that array to the end of the array without using any extra space. We will use the Array.prototype.forEach() method here along with Array.prototype.splice() and Array.prototype.push().The code for the function will be −Exampleconst arr = [34, 6, 76, 0, 0, 343, 90, 0, 32, 0, 34, 21, 54]; const moveZero = (arr) => {    for(ind = 0; ind < arr.length; ind++){       const el = arr[ind];       if(el === 0){          arr.push(arr.splice(ind, 1)[0]);          ind--;       };    } }; moveZero(arr); console.log(arr);OutputThe output in the console will be −[34, 6, 76, 343, 90, 32, 34, 21, 54, 0, 0, 0, 0]

Recursive product of summed digits JavaScript

AmitDiwan
Updated on 24-Aug-2020 05:51:42

200 Views

We have to create a function that takes in any number of arguments (Number literals), adds them together, and returns the product of digits when the answer is only 1 digit long.For example −If the arguments are −16, 34, 42We have to first add them together −16+34+42 = 92And then keep multiplying the digits together until we get a 1-digit number like this −9*2 = 18 1*8 = 8When we get the one-digit number, we have to return it from our function.We will break this into two functions −One function accepts a number and returns the product of its digits, ... Read More

JavaScript code for recursive Fibonacci series

AmitDiwan
Updated on 24-Aug-2020 05:49:10

378 Views

We have to write a recursive function fibonacci() that takes in a number n and returns an array with first n elements of fibonacci series. Therefore, let’s write the code for this function −Exampleconst fibonacci = (n, res = [], count = 1, last = 0) => {    if(n){       return fibonacci(n-1, res.concat(count), count+last, count);    };    return res; }; console.log(fibonacci(8)); console.log(fibonacci(0)); console.log(fibonacci(1)); console.log(fibonacci(19));OutputThe output in the console will be −[    1, 1, 2, 3,    5, 8, 13, 21 ] [] [ 1 ] [    1, 1, 2, 3, 5,    8, 13, 21, 34, 55,    89, 144, 233, 377, 610,    987, 1597, 2584, 4181 ]

Get the longest and shortest string in an array JavaScript

AmitDiwan
Updated on 24-Aug-2020 05:47:08

718 Views

We have an array of string literals like this −const arr = ['Some', 'random', 'words', 'that', 'actually', 'form', 'a', 'sentence.'];We are required to write a function that returns the longest and the shortest word from this array. We will use Array.prototype.reduce() method to keep track of the longest and shortest word in the array through a complete iteration.The code for this will be −Exampleconst arr = ['Some', 'random', 'words', 'that', 'actually', 'form', 'a', 'sentence.']; const findWords = (arr) => {    return arr.reduce((acc, val) => {       const { length: len } = val;       if(len ... Read More

Sum of consecutive numbers in JavaScript

AmitDiwan
Updated on 24-Aug-2020 05:43:58

506 Views

Let’s say, we have to write a function that takes in an array and returns another array in which the consecutive similar numbers are added up together.For example −const array = [1, 5, 5, 5, 8, 8, 9, 1, 4, 4, 2];The output should be −[1, 15, 16, 9, 1, 8, 2]All consecutive 5s added up to 15, then 2 consecutive 8s added up to 16 similarly 4s added up to 8.Therefore, let’s write the code for this function. We will use the Array.prototype.reduce() method here to reduce the original array and simultaneously construct a new one.Exampleconst array = [1, ... Read More

Recursively loop through an array and return number of items with JavaScript?

AmitDiwan
Updated on 24-Aug-2020 05:41:29

2K+ Views

We have to write a function, say searchRecursively() that takes in an array and a search query and returns the count of that search query in the nested array.For example, if the array is given by −const names = ["rakesh", ["kalicharan", "krishna", "rakesh", "james", ["michael", "nathan", "rakesh", "george"]]];Then −searchRecursively(names, ‘’rakesh’);Should return 3 because it makes a total of 3 appearances in the array. Therefore, let’s write the code for this recursive function −Exampleconst names = ["rakesh", ["kalicharan", "krishna", "rakesh", "james", ["michael", "nathan", "rakesh", "george"]]]; const searchRecursively = (arr, query, count = 0, len = 0) => {    if(len < ... Read More

JavaScript program to merge two objects into a single object and adds the values for same keys

AmitDiwan
Updated on 24-Aug-2020 05:39:37

507 Views

We have to write a function that takes in two objects, merges them into a single object, and adds the values for same keys. This has to be done in linear time and constant space, means using at most only one loop and merging the properties in the pre-existing objects and not creating any new variable.So, let’s write the code for this function −Exampleconst obj1 = {    value1: 45,    value2: 33,    value3: 41,    value4: 4,    value5: 65,    value6: 5,    value7: 15, }; const obj2 = {    value1: 34,    value3: 71,   ... Read More

JavaScript algorithm for converting Roman numbers to decimal numbers

Disha Verma
Updated on 11-Mar-2025 16:35:09

487 Views

For converting a Roman numeral into a decimal number, JavaScript provides simple and efficient ways. Roman numerals are a number system that started in ancient Rome and are still used today in different situations. Converting Roman numerals to decimal (integer) values can be useful in many applications, such as date conversions, numbering systems, or educational tools. In this article, we will see a JavaScript algorithm to convert Roman numerals into decimal numbers efficiently. Understanding Roman Numerals Roman numerals are a Number System that uses combinations of letters from the Latin alphabet to represent values. It uses seven letters to ... Read More

JavaScript algorithm for converting integers to roman numbers

AmitDiwan
Updated on 21-Aug-2020 15:12:46

1K+ Views

Let’s say, we are required to write a function, say intToRoman(), which, as the name suggests, returns a Roman equivalent of the number passed in it as an argument.Let’s write the code for this function −Exampleconst intToRoman = (num) => {    let result = "";    while(num){       if(num>=1000){          result += "M";          num -= 1000;       }else if(num>=500){          if(num>=900){             result += "CM";             num -= 900;          }else{   ... Read More

How to check existence of NaN keyword in an array JavaScript

AmitDiwan
Updated on 21-Aug-2020 15:08:01

1K+ Views

We have an array of elements that contains both truth and false values. Our job is to write a function that returns an array with indices of those elements which are NaN in the original array.NaN !== NaNThe datatype of NaN is actually number. Although NaN is a falsy value, it has a peculiar property that no other datatype or variable has. It’s that the expression NaN === NaN yields false. And it’s only in the case of NaN that its false.So, we can use this behavior to our good and pick out NaN value index. The code for this ... Read More

Advertisements