AmitDiwan has Published 10744 Articles

Compare the Triplets - JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:30:31

187 Views

For this, you need to use if condition to compare triplets.Let’s say we are passing the following values −35, 36, 37, 33, 48, 50ExampleFollowing is the code −function tripletsSolution(first, second, third, fourth, fifth, sixth) {    var storedResult = []    if (first > fourth || second > fifth || ... Read More

Splitting a hyphen delimited string with negative numbers or range of numbers - JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:25:31

465 Views

Let’s say we have the following hyphen delimited string with negative or range of numbers −var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS";To split, use regular expressions. Following is the code −ExampleFollowing is the code −var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS"; var regularExpression = /-(?=[A-Za-z-]|\d+-\d)/; var result1 = ... Read More

How to make filter and word replacement method - JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:24:22

278 Views

There is no in-built function to replace all occurrences of word. You need to create your own function.Let’s say the following is our string −var sentence = "Yes,  My Name is John Smith. I live in US. Yes,  My Favourite Subject is JavaScript";ExampleFollowing is the code −var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript"; console.log(sentence); ... Read More

How to convert a string with zeros to number in JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:22:19

387 Views

Let’s say the following is our string”var stringValue="453.000.00.00.0000";To convert the above string with zeroes to number, use parseInt() along with replace() −var numberValue = parseInt(stringValue.replace(/\./gm, '')); ExampleFollowing is the complete code −var stringValue="453.000.00.00.0000"; var numberValue = parseInt(stringValue.replace(/\./gm, '')); console.log("Original value="+stringValue) console.log("After modification the value="+numberValue);To run the above program, use the ... Read More

What is the best way to reduce and merge a collection of objects – JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:17:19

670 Views

The best way to reduce and merge a collection of objects, use the concept of Object.values() along with reduce().Following is the object −var details = [    { studentId: 10, marks: 75, studentName: "John" },    { studentId: 10, marks: 75, studentName: "John" },    { studentId: 11, marks: 98, ... Read More

Difference between two times using Dayjs JavaScript library?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:15:09

480 Views

Let’s say the following are our time data −var startHour = dayjs().hour(10) var endHour = dayjs().hour(22)To get the difference, use the diff() method −ExampleFollowing is the code − Live Demo            Document    var startHour = dayjs().hour(10)    var ... Read More

Checking a Checkbox with JavaScript

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:13:12

554 Views

Let’s say the following are ourrinput type checkbox −John David We want to check any of the checkbox. Use the checked property to check the checkbox.ExampleFollowing is the code − Live Demo            Document    John       ... Read More

How to replace some preceding characters with a constant 4 asterisks and display the last 3 as well - JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:11:14

889 Views

Let’s say the following are our values −'6778922' '76633 56 1443' '8888 4532 3232 9999'We want the preceding characters to be replaced with 4 asterisks and the display rest of the last 3 characters. The output should be −**** 922 **** 443 **** 999For such conditions, use the replace() and ... Read More

Split the sentences by comma and remove surrounding spaces - JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:09:39

8K+ Views

Let’s say the following is our string with comma and whitespace −var sentences = "  John ,    David ,         Bob ,      Mike,            Carol        ";To split the sentences by comma, use split(). For removing surrounding spaces, ... Read More

How do I make an array with unique elements (remove duplicates) - JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:07:15

212 Views

Let’s say the following is our array with duplicate elements −var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000];We want the output to be −[10, 20, 100, 40, 1000];To display only the unique elements, use the concept of filter.ExampleFollowing is the code −var duplicateNumbers = [10, 20, 100, ... Read More

Advertisements