Found 9313 Articles for Object Oriented Programming

How to selectively retrieve value from json output JavaScript

AmitDiwan
Updated on 24-Aug-2020 10:08:55

163 Views

We have the following data inside a json file data.json −data.json{    "names": [{       "name": "Ramesh",       "readable": true    }, {       "name": "Suresh",       "readable": false    }, {       "name": "Mahesh",       "readable": true    }, {       "name": "Gourav",       "readable": true    }, {       "name": "Mike",       "readable": false    } ] }Our job is to create a function parseData that takes in the path to this file as one and only argument, ... Read More

JavaScript Compare two sentences word by word and return if they are substring of each other

AmitDiwan
Updated on 24-Aug-2020 10:06:27

119 Views

The idea here is to take two strings as input and return true if a is substring of b or b is sub string of a, otherwise return false.For example −isSubstr(‘hello’, ‘hello world’) // true isSubstr(‘can I use’ , ‘I us’) //true isSubstr(‘can’, ‘no we are’) //falseTherefore, in the function we will check for the longer string, the one with more characters and check if the other is its substring or not.Here is the code for doing so −Exampleconst str1 = 'This is a self-driving car.'; const str2 = '-driving c'; const str3 = '-dreving'; const isSubstr = (first, second) ... Read More

JavaScript program to take in a binary number as a string and returns its numerical equivalent in base 10

AmitDiwan
Updated on 24-Aug-2020 10:01:56

192 Views

We are required to write a JavaScript function that takes in a binary number as a string and returns its numerical equivalent in base 10. Therefore, let’s write the code for the function.This one is quite simple, we iterate over the string using a for loop and for each passing bit, we double the number with adding the current bit value to it like this −Exampleconst binaryToDecimal = binaryStr => {    let num = 0;    for(let i = 0; i < binaryStr.length; i++){       num *= 2;       num += Number(binaryStr[i]);    };   ... Read More

JavaScript Narcissistic number

AmitDiwan
Updated on 24-Aug-2020 09:59:48

605 Views

Narcissistic numberA narcissistic number in a given number base b is a number that is the sum of its own digits each raised to the power of the number of digits.For example −153 = 1^3 + 5^3 + 3^3 = 1+125+27 = 153Similarly, 1 = 1^1 = 1ApproachWe will first count the number of digits using a while loop. Then with another while loop, we pick last digit of the number and add its (count) th power to a variable sum. After the loop we return a boolean checking whether the sum is equal to the number or not.The code ... Read More

How to turn words into whole numbers JavaScript?

AmitDiwan
Updated on 24-Aug-2020 09:53:55

1K+ Views

We have to write a function that takes in a string as one and only argument, and return its equivalent number.For example −one five seven eight -------> 1578 Two eight eight eight -------> 2888This one is pretty straightforward; we iterate over the array of words splitted by whitespace and keep adding the appropriate number to the result.The code for doing this will be −Exampleconst wordToNum = (str) => {    const legend = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];    return str.toLowerCase().split(" ").reduce((acc, val) => {       const index = legend.indexOf(val);     ... Read More

Product of all other numbers an array in JavaScript

AmitDiwan
Updated on 24-Aug-2020 09:50:42

261 Views

Let’s say, we have to write a function that takes an array of numbers as argument. We have to return a new array with the products of each number except the index we are currently calculating product for.For example, if arr had 5 indices and we were creating the value for index 1, the numbers at index 0, 2, 3 and 4 would be multiplied. Similarly, if we were creating the value for index 2, the numbers at index 0, 1, 3 and 4 would be multiplied and so on.Note − It is guaranteed that all the elements inside the ... Read More

Taking part from array of numbers by percent JavaScript

AmitDiwan
Updated on 24-Aug-2020 09:48:23

1K+ Views

We have an array of Number literals like this −const numbers = [10, 6200, 20, 20, 350, 900, 26, 78, 888, 10000, 78, 15000, 200, 1280, 2000, 450];We are supposed to write a function that takes an array of numbers and a number between [0, 100], basically this number represents a certain percent. Let us denote this number by x for now.Now we have to return a subarray of first n elements of the original array that add up equal to or just less than the x % of the total sum of all array elements.Take a simpler example −const ... Read More

How to get odd and even position characters from a string?

AmitDiwan
Updated on 24-Aug-2020 09:44:07

1K+ Views

We have to write a function that removes every second character (starting from the very first character) from a string and appends all of those removed characters at the end in JavaScript.For example −If the string is "This is a test!" Then it should become "hsi etTi sats!"Therefore, let’s write the code for this function −Exampleconst string = 'This is a test!'; const separateString = (str) => {    const { first, second } = [...str].reduce((acc, val, ind) => {       const { first, second } = acc;       return {          first: ... Read More

How to calculate the average in JavaScript of the given properties in the array of objects

AmitDiwan
Updated on 24-Aug-2020 09:42:21

1K+ Views

We have an array of objects. Each object contains a few properties and one of these properties is age −const people = [    {       name: 'Anna',       age: 22    }, {       name: 'Tom',       age: 34    }, {       name: 'John',       age: 12    }, {       name: 'Kallis',       age: 22    }, {       name: 'Josh',       age: 19    } ]We have to write a function that takes in such an ... Read More

Sum of even numbers from n to m regardless if nm JavaScript

AmitDiwan
Updated on 24-Aug-2020 09:38:30

196 Views

We are required to write a function that takes two numbers as arguments m and n, and it returns the sum of all even numbers that falls between m and n (both inclusive)For example −If m = 10 and n = -4The output should be 10+8+6+4+2+0+(-2)+(-4) = 24ApproachWe will first calculate the sum of all even numbers up to n and the sum of all even numbers up to m.Then we will check for the bigger of the two m and n. Subtract the sum of smaller from the sum of bigger which will eventually give us the sum between ... Read More

Advertisements