Found 9313 Articles for Object Oriented Programming

JavaScript (+) sign concatenates instead of giving sum?

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

5K+ 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

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

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

119 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 following command −node fileName.js.Here, my file name is demo57.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo57.js Before replacing the message=Hello, this is John After replacing the message=Hello, this is David MillerRead More

Is it possible to select text boxes with JavaScript?

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

580 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

250 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

288 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

323 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

Detect the ENTER key in a text input field with JavaScript

AmitDiwan
Updated on 03-Sep-2020 06:48:33

5K+ Views

You can use the keyCode 13 for ENTER key. Let’s first create the input −Now, let’s use the on() with keyCode to detect the ENTER key. Following is the complete code −Example Live Demo Document    $("#txtInput").on('keyup', function (event) {       if (event.keyCode === 13) {          console.log("Enter key pressed!!!!!");       }    }); 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 ... Read More

Create empty array of a given size in JavaScript

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

1K+ Views

To create an empty array of given size, use the new operator −var numberArray = new Array(10);After that, let’s set some values in the array. Following is the code −Examplevar numberArray = new Array(10); console.log("The length="+numberArray.length) numberArray=[10,20,30,40,50,60]; console.log("The array value="); for(var i=0;i node demo52.js The length=10 The array value= 10 20 30 40 50 60

Advertisements