Found 10483 Articles for Web Development

Rotating an array - JavaScript

AmitDiwan
Updated on 14-Sep-2020 12:54:22

549 Views

Let’s say, we are required to write a JavaScript function that takes in an array and a number n and rotates the array by n elementsFor example: If the input array is −const arr = [12, 6, 43, 5, 7, 2, 5];and number n is 3, Then the output should be −const output = [5, 7, 2, 5, 12, 6, 43];Let’s write the code for this function −ExampleFollowing is the code −// rotation const arr = [12, 6, 43, 5, 7, 2, 5]; const rotateByOne = arr => {    for(let i = 0; i < arr.length-1; i++){     ... Read More

How can I check JavaScript arrays for empty strings?

AmitDiwan
Updated on 14-Sep-2020 09:15:50

323 Views

Let’s say the following is our array with non-empty and empty values −studentDetails[2] = "Smith"; studentDetails[3] = ""; studentDetails[4] = "UK"; function arrayHasEmptyStrings(studentDetails) {    for (var index = 0; index < studentDetails.length; index++) {To check arrays for empty strings, the syntax is as follows. Set such condition for checking −if(yourArrayObjectName[yourCurrentIndexvalue]==””){    // insert your statement } else{    // insert your statement }Examplevar studentDetails = new Array(); studentDetails[0] = "John"; studentDetails[1] = ""; studentDetails[2] = "Smith"; studentDetails[3] = ""; studentDetails[4] = "UK"; function arrayHasEmptyStrings(studentDetails) {    for (var index = 0; index < studentDetails.length; index++) {    if (studentDetails[index] ... Read More

How do I trigger a function when the time reaches a specific time in JavaScript?

AmitDiwan
Updated on 14-Sep-2020 09:11:07

3K+ Views

For this, extract the time of specific date time and call the function using setTimeout(). The code is as follows −Example Live Demo Document    function timeToAlert() {       alert("The time is 9:36 AM");    }    var timeIsBeing936 = new Date("08/09/2020 09:36:00 AM").getTime()    , currentTime = new Date().getTime()    , subtractMilliSecondsValue = timeIsBeing936 - currentTime;    setTimeout(timeToAlert, subtractMilliSecondsValue); 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 −

How to check for 'undefined' or 'null' in a JavaScript array and display only non-null values?

AmitDiwan
Updated on 14-Sep-2020 09:08:53

527 Views

Let’s say the following is our array with non-null, null and undefined values −var firstName=["John",null,"Mike","David","Bob",undefined];You can check for undefined or null cases by using the following code −Examplevar firstName=["John",null,"Mike","David","Bob",undefined]; for(var index=0;index node demo203.js John Mike David Bob

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

AmitDiwan
Updated on 14-Sep-2020 09:07:31

163 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

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

AmitDiwan
Updated on 14-Sep-2020 09:05:24

438 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 −

Check for illegal number with isNaN() in JavaScript

AmitDiwan
Updated on 14-Sep-2020 09:01:18

212 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
Updated on 14-Sep-2020 09:00:06

54 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
Updated on 14-Sep-2020 08:57:56

302 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
Updated on 14-Sep-2020 08:56:55

172 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

Advertisements