JavaScript Algorithm: Array Differences

JavaScript Algorithm: Array Differences

Spotting the differences between two arrays into another array.

We are going to write an array called mergeExclusive that will accept two arrays, arr1 and arr2.

The goal of the function is to return a new array containing numbers that exist in either arr1 or arr2 but not in both.

Example:

let arr1 = [1, 2, 3, 10, 5, 43, 14];
let arr2 = [1, 4, 5, 6, 14];

mergeExclusive(arr1, arr2)
//output: [2,3,10,43,4,6]

If you look at the numbers in both arr1 and arr2, you’ll see that numbers 1, 5, and 14 are excluded as they are found in both arrays.

The first thing we are going to do is use the filter() method to filter out every element that is in arr1 but also exist in arr2.

To check that, we use the includes() method which returns true if the arr2 array contains that value. If it does, we don’t want that so we include a NOT operator to negate the statement and toss it out.

We assign our new array to a variable called arrDiff1.

let arrDiff1 = arr1.filter(function(e){
    return !(arr2.includes(e));
});

javascript code coding

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.

What is JavaScript - Stackfindover - Blog

What is PHP: - Who invented PHP, how it works, answers to all such questions about PHP, and much other information, you are going to

Who Else Wants to Write Clean JavaScript Code?

Who Else Wants to Write Clean JavaScript Code? 7 Tips to Make Your Coworkers Fall in Love With Your Code.

Top 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript

Don’t waste time writing long code while you can make it short, yet clearer and easier to read. In this Javascript tutorial, we'll discuss 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript