To round the decimal number to the nearest tenth, use toFixed(1) in JavaScript. The syntax is as follows −var anyVaribleName=yourVariableName.toFixed(1)Let’s say the following is our decimal number −var decimalValue =200.432144444555; console.log("Actual value="+decimalValue)Let’s now round the decimal number. Following is the code −Examplevar decimalValue =200.432144444555; console.log("Actual value="+decimalValue) var modifiedValue=decimalValue.toFixed(1) console.log("Modified value="+ modifiedValue);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo38.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo38.js Actual value=200.432144444555 Modified value=200.4Read More
To get value from the first checkbox, which is not hidden, use the :visible selector. Following is the code −Example Live Demo >Document .notShown { display: none; } var v=$('input:checkbox:checked:visible:first').val(); console.log(v); 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.This will produce the following output displaying the visible value “second” in console −
To store and retrieve arrays into and from data attributes, use the data() method in jQuery. Following is the syntax −var anyVariableName= $('#yourIdName).data('yourJavscriptArrayName');Following is the jQuery code −Example Live Demo Document var value = $('#test').data('details'); alert(value); 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 −
The easiest way to detect keypresses in JavaScript, use the onKeyPress event handler −document.onkeypressThe key press is matched with the keyCode property, which returns the Unicode character code of the key that triggered the onkeypress event.Example Live Demo Document document.onkeypress = function (eventKeyName) { eventKeyName = eventKeyName || window.event; if(eventKeyName.keyCode==13){ console.log('You have pressed enter key'); } else { alert(String.fromCharCode(eventKeyName.keyCode)) } }; To run the above program, save the file name ... Read More
To find out which characters key is pressed, use the window.event along with keyCode. Following is the code −Example Live Demo Document function keyPressName(myEventKeyName){ var pressedKey; if(window.event){ pressedKey = myEventKeyName.keyCode; } else if(myEventKeyName.which) { pressedKey = myEventKeyName.which; } alert(String.fromCharCode(pressedKey)); } 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” ... Read More
To avoid unexpected string concatenation while concatenating strings, multiple strings, and numbers, use backticks.We have the following −const concatValue = 'John, David, Mike'; var friendNames= `${concatValue}`;The above value is concatenated with a string and number −var studentNameWithFriends=` ${concatValue}| 'Carol' | 24 ` ;Following is the complete JavaScript code for concatenation −Exampleconst concatValue = 'John, David, Mike'; var friendNames= `${concatValue}`; var studentNameWithFriends=` ${concatValue}| 'Carol' | 24 ` ; console.log(friendNames); console.log(studentNameWithFriends);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo37.jsOutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo37.js John, David, Mike John, ... Read More
To display only the visible text, use the concept of − visible selector in jQuery. It selects the element currently visible. Following is the code −Example Live Demo Document Test Class Demo class $('#myDiv').children(":visible").text() 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.This will produce the following output displaying the visible text −
To parse JSON, use parseJSON() and for displaying a specific pair, use the $.each() function.Let’s say the following is our JSON, which is parsed −const APIData = '[{"Name":"John", "Age":21}, {"Name":"David", "Age":24}, {"Name":"Bob", "Age" :20}]'; const getObject = jQuery.parseJSON(APIData);Now, let’s see the complete code and fetch the “Name” pairs −Example Live Demo Document const APIData = '[{"Name":"John", "Age":21}, {"Name":"David", "Age":24}, {"Name":"Bob", "Age":20}]'; const getObject = jQuery.parseJSON(APIData); $.each(getObject, function (k, obj) { console.log(obj['Name']); }); To run the above program, save the file name ... Read More
To find all subsets of a set, use reduce() along with map() in JavaScript. Let’s say, we are passing the set [8,9] and finding the subsets.Exampleconst findAllSubsetsoOfGivenSet = originalArrayValue => originalArrayValue.reduce( (givenSet, setValue) => givenSet.concat( givenSet.map(givenSet => [setValue,...givenSet]) ), [[]] ); console.log(findAllSubsetsoOfGivenSet([8,9]));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo36.js.OutputThis will produce the following output.PS C:\Users\Amit\JavaScript-code> node demo36.js [ [], [ 8 ], [ 9 ], [ 9, 8 ] ]
To convert an object into an array, use push() in JavaScript. Following is the code −ExamplestudentDetails= [ { studentName:"Chris",studentMarks:34}, { studentName:"David",studentMarks:89} ] var convertIntoArray = []; for (var i = 0; i < studentDetails.length; i++) { convertIntoArray.push(studentDetails[i].studentName); } console.log(convertIntoArray);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo35.js.OutputThis will produce the following output.PS C:\Users\Amit\JavaScript-code> node demo35.js [ 'Chris', 'David' ]