Found 8591 Articles for Front End Technology

JavaScript - Exclude some values in average calculation

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

290 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

438 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

109 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

135 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

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

Default exports vs Named exports in JavaScript

AmitDiwan
Updated on 16-Aug-2023 16:35:06

964 Views

In this article, we will learn the difference between default exports and named exports in javascript, and how we can use them to effectively organize our code structure. In javascript, we can use default exports and named exports so as to have separate files or modules for separate pieces of code. This helps in enhancing code readability and tree shaking to a great extent. Default exports Named exports A default export allows us to export a single value or function as the default export of a module. A named export allows us to export multiple values ... Read More

How to convert Array to Set in JavaScript?

AmitDiwan
Updated on 16-Aug-2023 15:33:12

744 Views

This tutorial will teach you how to eliminate duplicate values from an array of items in Javascript by converting an array to a set. When storing distinct values from a collection of data in javascript, we can utilise the Set data structure to get rid of duplicate values. When we wish to eliminate duplicates from our data or take advantage of the special features offered by Sets data structure, converting an array to a Set can be helpful. Let’s look at some of the examples and methods to understand the concept better − Example 1 - Using the Set Constructor ... Read More

What are Promises in JavaScript?

AmitDiwan
Updated on 16-Aug-2023 16:04:39

881 Views

Promises, in JavaScript, can be used to simplify asynchronous programming, making it easier to handle asynchronous operations like I/O operations or communicate with an external system or machine. Promises are objects that represent the eventual completion or failure of an asynchronous operation and its resulting value. They provide an easy way to deal with asynchronous code, allowing us to write cleaner and more readable code. Here we will discover what are promises in javascript and how to use them to incorporate asynchronous programming behaviour into our control flow, in this tutorial. A promise has 3 states − Pending − ... Read More

How to use named arguments in JavaScript functions?

AmitDiwan
Updated on 16-Aug-2023 15:58:15

2K+ Views

In this article, we will learn how to use named arguments in javascript, and how we can use it to significantly enhance code readability and maintainability. Javascript allows us to use named arguments, which eliminate the need for the order of parameters to matter during function execution. We may then access the arguments by name inside the function definitions. Let’s look at some of the examples and methods to understand the concept better − Example 1 - Passing Objects as Arguments We can pass an object as an argument to the function to achieve named−argument functionality. This uses the ... Read More

Exports & Imports in JavaScript

AmitDiwan
Updated on 16-Aug-2023 15:55:32

363 Views

We will learn how to utilise imports and exports in javascript in this post, as well as how to use them to break our code into different modules in order to better organise it. To facilitate modular development in programming, which enables us to organise our code into reusable modules, JavaScript provides exports and imports. To achieve this sharing and reusability of code, we can use different types of exports and imports. Exports allow us to make functions, variables, objects, or any values available outside of a module. By marking components as exports, we can expose them for use in ... Read More

Advertisements