Remove Leading Zeros in a JavaScript Array

AmitDiwan
Updated on 07-Sep-2020 07:58:49

397 Views

To remove leading zeroes in an array, use the concept of filter(). Following is our input wherein we will remove the leading zeroes −[10,0,12,0,0]) [0,0,0,0,0,0,10,12,0]) [12,0,0,1,0,0])Exampleconst removeLeadingZero = input => input.filter((lastValue => value => lastValue= lastValue || value) (false) ); console.log(removeLeadingZero([10,0,12,0,0])); console.log(removeLeadingZero([0,0,0,0,0,0,10,12,0])); console.log(removeLeadingZero([12,0,0,1,0,0]));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo76.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo76.js [ 10, 0, 12, 0, 0 ] [ 10, 12, 0 ] [ 12, 0, 0, 1, 0, 0 ]

Change Value of Input on Form Submit in JavaScript

AmitDiwan
Updated on 07-Sep-2020 07:57:36

5K+ Views

To change value of input, use the concept of document. The syntax is as follows −document.yourFormName.yourTextName.value=anyValue;Let’s say the following is our input type calling submitForm() on clicking “Submit Form” −         The submitForm() function changing the value of input −function submitForm(){    document.studentForm.txtInput.value='STUDENT-100'; }Example Live Demo Document    function submitForm(){       document.studentForm.txtInput.value='STUDENT-100';    } To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in ... Read More

onclick Not Working for Multiple Links with Same Function in JavaScript

AmitDiwan
Updated on 07-Sep-2020 07:54:13

356 Views

If onclick not working for two or more links, then use the document.querySelectorAll() in JavaScript. Following is the code −Example Live Demo Document www.example.com www.test.com    const values = document.querySelectorAll('[class=class1]');    values.forEach(function(v) {       console.log(v.innerHTML);    }); To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −

Check If Input Is a Number or Letter in JavaScript

AmitDiwan
Updated on 07-Sep-2020 07:52:13

2K+ Views

To check if the input is a number or letter, use the isNaN() function from JavaScript. It returns true if the value is NaN i.e. Not a Number. Following is the code −Example Live Demo Document Enter the value:    function checkInputIsNumber(){       var value=document.forms["checkingInput"]["txtValue"].value;       if (isNaN(value)){          alert("Please Provide the input as a number");          return false;       }    } To run the above program, save the file ... Read More

Check If Array Element Contains a False Value in JavaScript

AmitDiwan
Updated on 07-Sep-2020 07:47:41

206 Views

To check if array element contains a false value, you can use the Object.values() in JavaScript. Following is the code −Exampleconst details = [    {       customerDetails:       [          {             isMarried: true          },          {             isMarried: true          }       ]    },    {       customerDetails:    [       {             isMarried: true          },          {          isMarried: true          }       ]    } ] const isNotMarried = Object.values(details) .some(({customerDetails})=>customerDetails.some(({isMarried})=>!isMarrie d)); console.log(isNotMarried);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo75.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo75.js false

Calling Asynchronous Code from Index HTML Page in JavaScript

AmitDiwan
Updated on 07-Sep-2020 07:37:19

276 Views

You need to use async and await with body onLoad attribute Following is the code −index.htmlExample Live Demo Document    async function callFunction(){       await testFunction();    }    function testFunction(){       console.log("Hi...");    } To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −

Return String By Capitalizing First Letter Of Each Word And Rest In Lower Case With JavaScript

AmitDiwan
Updated on 07-Sep-2020 07:34:41

254 Views

For this, use toUpperCase() along with toLowerCase(). Following is the code −Examplefunction capitalEveryFirstletter(subjectTitle) {    return subjectTitle.split(' ')    .map(st => st.charAt(0).toUpperCase() + st.slice(1).toLowerCase())    .join(' ');; } var subjectTitle="iNtroduction tO JavaScript ProgRAMMINg"; var output=capitalEveryFirstletter(subjectTitle); console.log("The result="+output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo74.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo74.js The result=Introduction To JavaScript Programming

Create Global Variable in jQuery Outside Document Ready Function

AmitDiwan
Updated on 07-Sep-2020 07:32:39

2K+ Views

To create a global variable, you need to place variable inside the tag. Following is the code −Example Live Demo Document checkGlobalVariable    var globalVariable;    $(document).ready(function() {       function initializeGlobalVariable() {          globalVariable = [{             "name":"John",             "age":23          }];       }       initializeGlobalVariable();    });    function createGlobalVariable() {       if (globalVariable.length) {          console.log('The Global variable name   ... Read More

Set Div Position Relative to Another Div in JavaScript

AmitDiwan
Updated on 07-Sep-2020 07:29:16

702 Views

For this, you need to use “flex-direction” concept of CSS. Let’s say the following is our CSS style −    .demo{       display: flex;       flex-direction: column-reverse;    } Now, set the div position relative to another div −Example Live Demo Document    .demo{       display: flex;       flex-direction: column-reverse;    } DIV_DEMO1 DIV_DEMO2 To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −

Run Functions Iteratively with Async/Await in JavaScript

AmitDiwan
Updated on 07-Sep-2020 07:27:03

224 Views

You can use keyword async as well as await. Following is the code −Exampleasync function test(i) {    while (i node demo73.js Call Demo1() Call Demo2() Call Demo1() Call Demo2() Call Demo1() Call Demo2() Call Demo1() Call Demo2() Call Demo1() Call Demo2() Call Demo1() Call Demo2() Call Demo1() Call Demo2() Call Demo1() Call Demo2() Call Demo1() Call Demo2() Call Demo1() Call Demo2()

Advertisements