AmitDiwan has Published 10744 Articles

How to determine if date is weekend in JavaScript?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2020 07:33:11

792 Views

As we know the value 0 is for day Sunday and 6 for Saturday. First of all, you need to get day with the help of getDay().Let’s set a date −var givenDate=new Date("2020-07-18");Now, we will get the day −var currentDay = givenDate.getDay();Following is the code to determine if date is ... Read More

What is the simplest solution to flat a JavaScript array of objects into an object?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2020 07:29:56

159 Views

Flat array of objects into an object, you can use the concept of reduce(). Let’s say following is our array of objects −const studentDetails = [    {Name: "Chris"},    {Age: 22} ]Exampleconst studentDetails = [    {Name: "Chris"},    {Age: 22} ] const objectStudent = studentDetails.reduce((obj, value) => { ... Read More

Possible to split a string with separator after every word in JavaScript

AmitDiwan

AmitDiwan

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

262 Views

To split a string with separator after every word, the syntax is as follows −var anyVariableName=yourVariableName.split('parameter').filter(value=>value)Let’s say, the following is our string with separator −var sentence="-My-Name-is-John-Smith-I-live-in-US";Now, split the string with separator as in the below code.Examplevar sentence="-My-Name-is-John-Smith-I-live-in-US"; console.log("The value="+sentence); var result=sentence.split('-').filter(value=>value) console.log("After split()="); console.log(result);To run the above program, you need ... Read More

Length of a JavaScript associative array?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2020 07:25:16

653 Views

You can use the length property from JavaScript to get the length. Let’s create an Associative array −var details = new Array(); details["Name"] = "John"; details["Age"] = 21; details["CountryName"] = "US"; details["SubjectName"] = "JavaScript";Let us now get the length of the associative array −Examplevar details = new Array(); details["Name"] = ... Read More

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

AmitDiwan

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 ... Read More

JavaScript Update specific index in a boolean matrix?

AmitDiwan

AmitDiwan

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

209 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, ... Read More

Function to create diamond shape given a value in JavaScript?

AmitDiwan

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

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 ... Read More

Does JavaScript have a method to replace part of a string without creating a new string?

AmitDiwan

AmitDiwan

Updated on 03-Sep-2020 07:08:38

183 Views

Yes, we can replace part of a string without creating a new string using the replace() method as in the below syntax −var anyVariableName=yourValue; yourVariableName=yourVariableName.replace(yourOldValue, yourNewValue);Examplevar message="Hello, this is John"; console.log("Before replacing the message="+message); message=message.replace("John", "David Miller"); console.log("After replacing the message="+message);To run the above program, you need to use the ... Read More

Is it possible to select text boxes with JavaScript?

AmitDiwan

AmitDiwan

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

710 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: ... Read More

Advertisements