Create Diamond Shape 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 Plus 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

Replace Part of a String in JavaScript Without Creating a New String

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

190 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

Select Text Boxes with JavaScript

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

724 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 Enter Key Press in JavaScript

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

322 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

Filter Array of Strings Matching Case-Insensitive Substring in JavaScript

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

Generate Array of N Equidistant Points Along a Line Segment in JavaScript

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

357 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

Sum All Integers from Nested Array using Recursive Loop in JavaScript

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

414 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 Enter Key in Text Input Field with JavaScript

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

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

Advertisements