Javascript Articles

Page 353 of 534

How Do I Find the Largest Number in a 3D JavaScript Array?

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 197 Views

Let’s say the following is our array;var theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]];Use the concept of flat() within the Math.max() to get the largest number.Examplevar theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]]; Array.prototype.findTheLargestNumberIn3dArray = function (){    return Math.max(...this.flat(Infinity)); } console.log("The largest number in 3D array is="); console.log(theValuesIn3DArray.findTheLargestNumberIn3dArray());To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo202.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo202.js The largest number in 3D array is= 99

Read More

Implement Onclick in JavaScript and allow web browser to go back to previous page?

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 490 Views

For reaching the back page on button click, use the concept of −window.history.go(-1)Example Live Demo Document 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 −After clicking the button “Click the button to Goto the Previous Page....”, you will reach the previous page as in the below screenshot −

Read More

Check for illegal number with isNaN() in JavaScript

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 241 Views

Following is the code −Examplefunction multiplication(firstValue, secondValue, callback) {    var res = firstValue * secondValue;    var err = isNaN(res) ? 'Something is wrong in input parameter' :    undefined;    callback(res, err); } multiplication(10, 50, function (result, error) {    console.log("The multiplication result="+result);    if (error) {       console.log(error);    } }); multiplication('Sam', 5, function (result, error) {    console.log("The multiplication result="+result);    if (error) {       console.log(error);    } });To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo201.js.OutputThis will produce the following output ...

Read More

Check if value of an object of certain class has been altered in JavaScript and update another value based on it?

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 81 Views

To check this, use the concept of getter, i.e. the get property. Following is the code −Exampleclass Student{    constructor(studentMarks1, studentMarks2){       this.studentMarks1 = studentMarks1       this.studentMarks2 = studentMarks2       var alteredValue = this;       this.getValues = {          get studentMarks1() {             return alteredValue.studentMarks1          },          get studentMarks2() {             return alteredValue.studentMarks2          }       }    } } var johnSmith = new Student(78, 79) console.log("Before ...

Read More

How to move all capital letters to the beginning of the string in JavaScript?

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 336 Views

Let’s say following is our string −my name is JOHN SMITHUse sort() along with regular expression /[A-Z]/ to move all capital letters to the beginning of the string/Examplevar moveAllCapitalLettersAtTheBeginning = [...' my name is JOHN SMITH '] .sort((value1, value2) => /[A-Z]/.test(value1) ? /[A-Z]/.test(value2) ? 0 : -1 : 0).join(' '); console.log("After moving the all capital letters at the beginning="); console.log(moveAllCapitalLettersAtTheBeginning);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo199.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo199.js After moving the all capital letters at the beginning= J O H N ...

Read More

JavaScript outsider function call and return the result

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 205 Views

Use the return keyword for outsider function call. Following is the code −Examplevar substractMethod = function () {    var firstValue =1000, thirdValue= 200;    var divideMethod = function (){       var secondValue =500;       console.log("The result of       divideMethod()="+(firstValue/secondValue));       return (firstValue-secondValue);    }    return divideMethod; } var result = subtractMethod(); console.log("The result of substractMethod()="+result());To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo198.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo198.js The result of divideMethod()=2 The result of subtractMethod()=500

Read More

Convert buffer to readable string in JavaScript?

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 772 Views

For this, use the concept of toString(‘utf8’). Following is the code −In the code below, there are detailed explanations about the buffer.Examplevar actualBufferObject = Buffer.from('[John Smith]', 'utf8') console.log("The actual buffer object="); console.log(JSON.stringify(actualBufferObject)) console.log("Get back the original object="); console.log(actualBufferObject.toString('utf8')); var myObjectValue = '[John Smith]'; console.log("The data you are getting from the buffer is equal to ASCII code equivalent...") for (var counter = 0; counter < myObjectValue.length; counter++) {    console.log("The ascii value of " + myObjectValue[counter] + " is ="    + (myObjectValue.charCodeAt(counter))); }To run the above program, you need to use the following command −node fileName.js.Here, my file name is ...

Read More

How to avoid inserting NULL values to a table with JavaScript?

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 651 Views

In order to get rid of null values inserted into a table, you need to check the condition while entering the value.The condition to check NULL must be as follows −while( !( yourVariableName1==null || yourVariableName2==null || yourVariableName3==null…...N){    // yourStatement1    .    .    N }The above logic will never allow inserting the null values.Now you can use for loop and insert value into the table without NULL. Following is the code −Example Live Demo Document Demo Of Inserting the value into the table This is demo on the javascript ...

Read More

Remove values in an array by comparing the items 0th index in JavaScript?

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 291 Views

Let’s say the following is our array &mius;var subjectNameAlongWithMarks = [    ["JavaScript", 78],    ["Java", 56],    ["JavaScript", 58],    ["MySQL", 77],    ["MongoDB", 75],    ["Java", 98] ]Above, we have repeated values, which we need to remove by comparing the duplicate values 0th index. Use the concept of Set() from JavaScript for this −Examplevar subjectNameAlongWithMarks = [    ["JavaScript", 78],    ["Java", 56],    ["JavaScript", 58],    ["MySQL", 77],    ["MongoDB", 75],    ["Java", 98] ] var distinctResult = subjectNameAlongWithMarks.filter(function ([value]){    return !this.has(value) && !!this.add(value) }, new Set()) console.log(distinctResult);To run the above program, you need to use ...

Read More

Multi-selection of Checkboxes on button click in jQuery?

AmitDiwan
AmitDiwan
Updated on 14-Sep-2020 512 Views

For this, use jQuery() with id property. Following is the code −Example Live Demo Document    .changeColor {       color: red    }; Javascript MySQL MongoDB Python    jQuery("#selectDemo").click(function () {       jQuery(this).toggleClass("changeColor");       if (jQuery(this).hasClass("changeColor")) {          jQuery(".isSelected").prop("checked", true);          jQuery(this).val("Want To UnSelect All Values");       } else {   ...

Read More
Showing 3521–3530 of 5,338 articles
« Prev 1 351 352 353 354 355 534 Next »
Advertisements