JavaScript Algorithm: Return Positive Numbers

JavaScript Algorithm: Return Positive Numbers

A function that will return an array of positive numbers found in another array.

We are going to write a function called getPositives that will accept an array, ar, as an argument.

You are given an array containing both positive and negative numbers. The goal of the function is to output another array containing only the positive numbers found in the input array.


let numArr = [-5, 10, -3, 12, -9, 5, 90, 0, 1];

// output: [10,12,5,90,0,1]

There’s not much to explain here. All values that are greater than -1 remain goes into the output array.

There are a couple of ways to write this function but we will focus on one using the filter() method.

What the filter() method does is creates a new array containing all the elements in the array that passes the test within the provided callback function.

The array filters out numbers that don’t pass the test. The test we want the function to check for is if the value passed is greater than -1. All numbers less than 0 won’t go into the array.

We will put this new array into a variable called posArr.

const posArr = ar.filter(num => num > -1);

Our test function within the filter() method is equivalent to writing:

const posArr = ar.filter(function(num){
    return num > -1;

Now that we have our array containing nothing but positive numbers, we will return posArr.

return posArr;

Here is the rest of the function:

function getPositives(ar){
    const posArr = ar.filter(num => num > -1);
    return posArr;

javascript coding algorithms

Bootstrap 5 Complete Course with Examples

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

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Coding Interviews: Common Algorithms

I previously wrote about some common coding algorithms that you’ll run into on your job search. The article was received fairly well so I thought I’d do a couple more to help in your journey to becoming more familiar with algorithms.

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.

Coding Interviews: Common Algorithms

Today I am going to be sharing two common coding algorithms as well as their solutions and some links to resources that will help you expand your knowledge of algorithms.

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.

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.