Found 8591 Articles for Front End Technology

Take an array and find the one element that appears an odd number of times in JavaScript

AmitDiwan
Updated on 09-Nov-2020 11:01:20

362 Views

Given an array of integers, we are required to write a function that takes this array and finds the one element that appears an odd number of times. There will always be only one integer that appears an odd number of times.We will approach this problem by sorting the array. Once sorted, we can iterate over the array to pick the element that appears for odd number of times.ExampleFollowing is the code −const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOdd = arr => {    let ... Read More

Replace multiple instances of text surrounded by specific characters in JavaScript?

AmitDiwan
Updated on 09-Nov-2020 10:53:16

310 Views

Let’s say the following is our string. Some text is surrounded by special character hash(#) −var values = "My Name is #yourName# and I got #marks# in JavaScript subject";We need to replace the special character with valid values. For this, use replace() along with shift().ExampleFollowing is the code −var values = "My Name is #yourName# and I got #marks# in JavaScript subject"; const originalValue = ["David Miller", 97]; var result = values.replace(/#([^#]+)#/g, _ => originalValue.shift()); console.log(result);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo298.js.OutputThis will produce the following output ... Read More

How to set new id attribute with jQuery?

AmitDiwan
Updated on 09-Nov-2020 10:49:58

2K+ Views

To implement this, extract id from attr() and use replace() to replace the id attribute.ExampleFollowing is the code −            Document        $('[id*="-"]').each(function () {       console.log('Previous Id attribute: ' + $(this).attr('id'));       $(this).attr('id', $(this).attr('id').replace('-', '----'));       console.log('Now Id Attribute: ' + $(this).attr('id'));    }); To run the above program, save the file name “anyName.html(index.html)”. Right click on the file and select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −Following is the value on console −

Display form values after clicking Submit button using event.preventdefault() - jQuery?

AmitDiwan
Updated on 09-Nov-2020 10:46:21

5K+ Views

For this, use document.getElementById(“”) along with addEventListener().ExampleFollowing is the code − Live Demo            Document           FirstName:                           LastName:                                  const formDetails = document.getElementById("details");    formDetails.addEventListener("submit", async (ev) => {       ev.preventDefault();       var fName = document.getElementById("firstName").value;       var lName = document.getElementById("lastName").value;       console.log("First Name=" + ... Read More

Way of validating RadioBoxes with jQuery?

AmitDiwan
Updated on 09-Nov-2020 10:42:23

168 Views

Following is how you can validate RadioBoxes with jQuery −ExampleFollowing is the code − Live Demo            Document           Gender:       Male       Female             isStudent:       yes       No                    var nameValues = 'gen;student'.split(';');    $(function () {       $("form").on("submit", function (ev) {          if (nameValues.filter(val => $(`input[name=${val}]:checked`).length === 0).length > 0) {   ... Read More

Make JavaScript take HTML input from user, parse and display?

AmitDiwan
Updated on 09-Nov-2020 10:38:22

2K+ Views

The HTML input value is a string. To convert the string to integer, use parseInt().ExampleFollowing is the code − Live Demo            Document        GetANumber    function result() {       var numberValue = document.getElementById("txtInput").value;       if (!isNaN(numberValue))          console.log("The value=" + parseInt(numberValue));       else          console.log("Please enter the integer value..");    } To run the above program, save the file name “anyName.html(index.html)”. Right click on the file and select the option “Open with ... Read More

What's the most efficient way to turn all the keys of an object to lower case - JavaScript?

AmitDiwan
Updated on 09-Nov-2020 10:35:30

202 Views

Let’s say the following is our object −var details = {    "STUDENTNAME": "John",    "STUDENTAGE": 21,    "STUDENTCOUNTRYNAME": "US" }As you can see above, the keys are in capital case. We need to turn all these keys to lower case. Use toLowerCase() for this.ExampleFollowing is the code −var details = {    "STUDENTNAME": "John",    "STUDENTAGE": 21,    "STUDENTCOUNTRYNAME": "US" } var tempKey, allKeysOfDetails = Object.keys(details); var numberOfKey = allKeysOfDetails.length; var allKeysToLowerCase = {} while (numberOfKey--) {    tempKey = allKeysOfDetails[numberOfKey];    allKeysToLowerCase[tempKey.toLowerCase()] = details[tempKey]; } console.log(allKeysToLowerCase);To run the above program, you need to use the following command −node ... Read More

How to get id from tr tag and display it in a new td with JavaScript?

AmitDiwan
Updated on 09-Nov-2020 10:33:19

4K+ Views

Let’s say the following is our table −           StudentName       StudentCountryName               JohnDoe       UK               DavidMiller       US     To get id from tr tag and display it in a new td, use document.querySelectorAll(table tr).ExampleFollowing is the code − Live Demo            Document    td,    th,    table {       border: 1px solid black;       margin-left: 10px;       ... Read More

How to decrease size of a string by using preceding numbers - JavaScript?

AmitDiwan
Updated on 09-Nov-2020 09:05:13

105 Views

Let’s say our original string is the following with repeated letters −var values = "DDAAVIDMMMILLERRRRR";We want to remove the repeated letters and precede letters with numbers. For this, use replace() along with regular expression.ExampleFollowing is the code −var values = "DDAAVIDMMMILLERRRRR"; var precedingNumbersInString = values.replace(/(.)\1+/g, obj => obj.length + obj[0]); console.log("The original string value=" + values); console.log("String value after preceding the numbers ="); console.log(precedingNumbersInString);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo295.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo295.js The original string value=DDAAVIDMMMILLERRRRR String value ... Read More

How to automate this object using JavaScript to set one key on each iteration as null?

AmitDiwan
Updated on 09-Nov-2020 09:03:13

170 Views

For this, use Object.keys() and set one key on each iteration as null using a for loop..ExampleFollowing is the code −var objectValues = {    "name1": "John",    "name2": "David",    "address1": "US",    "address2": "UK" } for (var tempKey of Object.keys(objectValues)) {    var inEachIterationSetOneFieldValueWithNull = {       ...objectValues,        [tempKey]: null    };    console.log(inEachIterationSetOneFieldValueWithNull); }To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo294.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo294.js { name1: null, name2: 'David', address1: 'US', address2: 'UK' ... Read More

Advertisements