Javascript Articles - Page 349 of 607

How do I subtract one week from this date in JavaScript?

AmitDiwan
Updated on 03-Sep-2020 07:21:43

3K+ Views

You need to subtract one week i.e. 7 days from the current date. Following is the syntax −var anyVariableName=new Date(yourCurrentDate.setDate(yourCurrentDate.getDate() - 7)At first, get the current date −var currentDate = new Date(); console.log("The current Date="+currentDate);Now, set the new date with setDate() method and subtract 7 days −Examplevar currentDate = new Date(); console.log("The current Date="+currentDate); var before7Daysdate=new Date(currentDate.setDate(currentDate.getDate() - 7)); console.log("The One week ago date="+before7Daysdate);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo60.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo60.js The current Date=Tue Jul 14 2020 19:12:43 GMT+0530 (India ... Read More

JavaScript Update specific index in a boolean matrix?

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

238 Views

To updates, use the concept of fill() in JavaScript. The fill() method is used to fill the array elements with a static value. Following is the code −Exampleconst array= Array(4) var fillWithTrueValue=array.fill(true); const matrixWithOnlyBooleanTrue = Array(4).fill(fillWithTrueValue); console.log(matrixWithOnlyBooleanTrue);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo59.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo59.js [    [ true, true, true, true ],    [ true, true, true, true ],    [ true, true, true, true ],    [ true, true, true, true ] ]

Function to create diamond shape given a value in JavaScript?

AmitDiwan
Updated on 03-Sep-2020 07:19:04

2K+ Views

You can create your own function to create diamond shapes for a given value. Following is the code −Examplefunction createDimondShape(size){    for(var i=1;i=i;s--){          process.stdout.write(" ");       }       for(var j=1;j

JavaScript (+) sign concatenates instead of giving sum?

AmitDiwan
Updated on 03-Sep-2020 07:12:21

6K+ Views

The + sign concatenates because you haven’t used parseInt(). The values from the textbox are string values, therefore you need to use parseInt() to parse the value.After selecting the value from text box, you need to parse that value. Following is the code −Example Live Demo Document FirstNumber: SecondNumber:    function sumOfTwoNumbers(){       var num1 = document.querySelector(".num1").value;       var num2 = document.querySelector(".num2").value;       var addition = parseInt(num1)+parseInt(num2);       document.querySelector(".output").innerHTML = "The addition of two       numbers= " + ... Read More

Is it possible to select text boxes with JavaScript?

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

775 Views

Yes, select text box with JavaScript using the select() method. At first, let us create an input text −Enter your Name: Select Text BoxNow, let us select the text box on button click −Example Live Demo Document Enter your Name: Select Text Box    function check(){       document.getElementById("txtName").select();    } 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 −When you click ... Read More

Replace a value if null or undefined in JavaScript?

AmitDiwan
Updated on 03-Sep-2020 07:05:15

1K+ Views

To replace a value if null or undefined, you can use below syntax −var anyVariableName= null; var anyVariableName= yourVariableName|| anyValue;Examplevar value = null; console.log("The value ="+value) var actualValue = value || "This is the Correct Value"; console.log("The value="+actualValue);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo56.jsOutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo56.js The value =null The value=This is the Correct Value

Check whether Enter key is pressed or not and display the result in console with JavaScript?

AmitDiwan
Updated on 03-Sep-2020 06:58:28

357 Views

For this, use onkeypress. Let’s first create input text −Now, let’s see the demoForEnterKey() function and check whether enter key is pressed or not −function demoForEnterKey(eventName) {    if (eventName.keyCode == 13) {       var t = document.getElementById("textBox");       console.log(t.value);       console.log("Enter key is pressed.....")       return true;    } else {       console.log("Enter key is not pressed.....")       return false;    } }Example Live Demo Document    function demoForEnterKey(eventName) {       if (eventName.keyCode == 13) ... Read More

JavaScript filter an array of strings, matching case insensitive substring?

AmitDiwan
Updated on 03-Sep-2020 06:54:01

3K+ Views

Let’s first create array of strings −let studentDetails = [    {studentName: "John Smith"},    {studentName: "john smith"},    {studentName: "Carol Taylor"} ];Now, match case insensitive substring, use the filter() and the concept of toLowerCase() in it. Following is the code −Examplelet studentDetails = [    {studentName: "John Smith"},    {studentName: "john smith"},    {studentName: "Carol Taylor"} ]; var searchName="John Smith" console.log(studentDetails.filter(obj => obj.studentName.toLowerCase().indexOf(searchName.toLowerCase()) >= 0));To run the above program, you need to use the following command;node fileName.js.Here, my file name is demo55.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo55.js [ { studentName: 'John Smith' }, { studentName: ... Read More

How to generate array of n equidistant points along a line segment of length x with JavaScript?

AmitDiwan
Updated on 03-Sep-2020 06:51:55

382 Views

To generate array of n equidistant points along a line segment of length x, use the below syntax −for (var anyVariableName= 0; yourVariableName< yourStartPointName; yourVariableName++) {    var v = (yourVariableName+1)/ (yourStartPointName+1);    var v2 = v*yourEndPointName;Examplefunction drawPoints(start, end) {    const arrayOfPoints = []    for (var index = 0; index < start; index++) {       var v = (index+1)/ (start+1);       var v2 = v*end;       arrayOfPoints.push(Math.round(v2));    }    return arrayOfPoints; } const arrayOfPoints = drawPoints(5, 50); console.log("The Points="); console.log(arrayOfPoints);To run the above program, you need to use the following command ... Read More

JavaScript recursive loop to sum all integers from nested array?

AmitDiwan
Updated on 03-Sep-2020 06:49:42

451 Views

You need to call the same function again and again to sum all integers from nested array. Following is the code −Examplefunction sumOfTotalArray(numberArray){    var total= 0;    for (var index = 0; index < numberArray.length; index++) {       if (numberArray[index] instanceof Array){          total=total+sumOfTotalArray(arr[index]);       }       if (numberArray[index] === Math.round(numberArray[index])){          total=total+numberArray[index];       }    }    return total; } var number = new Array(6); number=[10, 20, 30, 40, 50, 60]; console.log("The sum is="+sumOfTotalArray(number));To run the above program, you need to use the following ... Read More

Advertisements