Found 9311 Articles for Object Oriented Programming

JavaScript - Exclude some values in average calculation

AmitDiwan
Updated on 14-Sep-2020 13:36:48

205 Views

Let’s say, we have an array of objects like this −data = [    {"Age":26, "Level":8},    {"Age":37, "Level":9},    {"Age":32, "Level":5},    {"Age":31, "Level":11},    {"Age":null, "Level":15},    {"Age":null, "Level":17},    {"Age":null, "Level":45} ];We are required to write a JavaScript function that calculates the average level for all the objects that have a truthy value for age propertyLet’s write the code for this function −ExampleFollowing is the code −data = [    {"Age":26, "Level":8},    {"Age":37, "Level":9},    {"Age":32, "Level":5},    {"Age":31, "Level":11},    {"Age":null, "Level":15},    {"Age":null, "Level":17},    {"Age":null, "Level":45} ]; const findAverage = arr => { ... Read More

Checking for co-prime numbers - JavaScript

AmitDiwan
Updated on 14-Sep-2020 13:33:39

331 Views

Two numbers are said to be co-primes if there exists no common prime factor amongst them (1 is not a prime number)For example −4 and 5 are co-primes 9 and 14 are co-primes 18 and 35 are co-primes 21 and 57 are not co-prime because they have 3 as the common prime factorWe are required to write a function that takes in two numbers and returns true if they are co-primes otherwise returns falseExampleLet’s write the code for this function −const areCoprimes = (num1, num2) => {    const smaller = num1 > num2 ? num1 : num2;    for(let ... Read More

Finding how many time a specific letter is appearing in the sentence using for loop, break, and continue - JavaScript

AmitDiwan
Updated on 14-Sep-2020 13:00:47

61 Views

We are required to write a JavaScript function that finds how many times a specific letter is appearing in the sentenceExampleLet’s write the code for this function −const string = 'This is just an example string for the program'; const countAppearances = (str, char) => {    let count = 0;    for(let i = 0; i < str.length; i++){    if(str[i] !== char){       // using continue to move to next iteration          continue;       };       // if we reached here it means that str[i] and char are same ... Read More

Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript

AmitDiwan
Updated on 14-Sep-2020 12:58:39

85 Views

We are required to write a JavaScript function that takes in three numbers A, B and N, and finds the total number of N-digit numbers whose sum of digits at even positions and odd positions are divisible by A and B respectively.ExampleLet’s write the code for this function −const indexSum = (num, sumOdd = 0, sumEven = 0, index = 0) => {    if(num){        if(index % 2 === 0){            sumEven += num % 10;        }else{            sumOdd += num % 10;       ... Read More

Rotating an array - JavaScript

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

465 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

250 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

Set the innerHTML with JavaScript

AmitDiwan
Updated on 14-Sep-2020 09:13:52

911 Views

The correct syntax to set the innerHTML is as follows −document.getElementById(“yourIdName”).innerHTML=”yourValue”;Let’s now see how to set the innerHTML −Example Live Demo Document Username:    document.getElementById('test').innerHTML = 'Enter Some value into the text box'; 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 do I trigger a function when the time reaches a specific time in JavaScript?

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

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

422 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

129 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

Advertisements