AmitDiwan has Published 10744 Articles

Comparing adjacent element and swap - JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 15:05:38

382 Views

This is the concept of Bubble Sort. It compares to adjacent element if it is lesser it will swap the value.ExampleFollowing is the code −var numbers = [10, 100, 30, 40, 90, 4, 91, 56, 78]; function bubbleSorting(numbers) {    for (var outer = 0; outer < numbers.length; outer++) { ... Read More

Match the style of
 element while performing paste - jQuery?
							

AmitDiwan

AmitDiwan

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

112 Views

For this, use tag. Set it to contenteditable −We have set the following style for paste −    pre {       min-height: 150px;       min-width: 300px;       font-family: 'Times New Roman', Times, serif;       white-space: pre;       background-color: rgb(19, ... Read More

How to get value of data attribute and use it in jQuery?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 14:57:06

8K+ Views

To get value of data attribute, use −$(“yourSelector”).data()The following is our input type with data attribute −ExampleFollowing is the code − Live Demo            Document        var value = $('#txtValue').data('value');    console.log("The value is=" + value); OutputThe output is as follows −

Number prime test in JavaScript by creating a custom function?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 14:52:27

168 Views

Create a custom function to test a number is prime or not in JavaScript.ExampleFollowing is the code −function checkNumberIsPrime(number) {    var flag = false;    for (start = 2; start < number / 2; start++) {       if (number % start === 0) {       ... Read More

Display the result difference while using ceil() in JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 14:49:47

152 Views

The Math.ceil() is part of the Math object in JavaScript. Suppose, your value is 4.5 or 4.3, it will give the result 5.Let’s say the following are our values −var result1 = 98; var result2 = 5;Following is the code to display the result difference while using ceil() or not ... Read More

Remove/ filter duplicate records from array - JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 14:48:10

269 Views

Let’s say the following are our records with some duplicate values −var objectOfNationality =    [       { nationality: "Indian" },       { nationality: "American" },       { nationality: "Emirati" },       { nationality: "Indian" },       { nationality: "American" ... Read More

How do I turn a string in dot notation into a nested object with a value – JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 14:45:22

4K+ Views

Let’s say the following is our string in dot notation −const keys = "details1.details2.details3.details4.details5"And the following is our array −const firsName = "David";To turn into a nested object, use the concept of split(‘.’) along with map().ExampleFollowing is the code −const keys = "details1.details2.details3.details4.details5" const firsName = "David"; var tempObject = ... Read More

Enter values with prompt and evaluate on the basis of conditions in JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 14:42:45

235 Views

Let’s say we are getting two values with prompt −var firstvalue = parseInt(prompt("Enter the value1")); var secondvalue = parseInt(prompt("Enter the value2"));ExampleFollowing is the code − Live Demo            Document    var firstvalue = parseInt(prompt("Enter the value1"));    var ... Read More

Variable defined with a reserved word const can be changed - JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 14:35:00

133 Views

You cannot change the value of an object when it is defined using const keyword.After changing it will remain same.Let’s say the following is our variable defined with const −const details1 = {    firstName: 'David',    subjectDetails: { subjectName: 'JavaScript' } }ExampleFollowing is the code to change the const ... Read More

Object comparison Complexity in JavaScript using comparison operator or JSON.stringlify()?

AmitDiwan

AmitDiwan

Updated on 03-Oct-2020 14:32:54

178 Views

Let’s say the following are our objects −var object1 = { firstName: "David" }; var object2 = { firstName: "David" };You will not get the correct result using comparison operator (== or ===). Use JSON.stringify() for this.ExampleFollowing is the code implementing both the ways and showing the correct result −var ... Read More

Advertisements