
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Filtering out primes from an array - JavaScript
We are required to write a JavaScript function that takes in the following array of numbers,
const arr = [34, 56, 3, 56, 4, 343, 68, 56, 34, 87, 8, 45, 34];
and returns a new filtered array that does not contain any prime number.
Example
Following is the code −
const arr = [34, 56, 3, 56, 4, 343, 68, 56, 34, 87, 8, 45, 34]; const isPrime = n => { if (n===1){ return false; }else if(n === 2){ return true; }else{ for(let x = 2; x < n; x++){ if(n % x === 0){ return false; } } return true; }; }; const filterPrime = arr => { const filtered = arr.filter(el => !isPrime(el)); return filtered; }; console.log(filterPrime(arr));
Output
Following is the output in the console −
[ 34, 56, 56, 4, 343, 68, 56, 34, 87, 8, 45, 34 ]
- Related Articles
- Filtering out numerals from string in JavaScript
- JavaScript - filtering array with an array
- Picking out uniques from an array in JavaScript
- Filtering out only null values in JavaScript
- Filtering array of objects in JavaScript
- Filtering array within a limit JavaScript
- JavaScript: How to filter out Non-Unique Values from an Array?
- Finding the only out of sequence number from an array using JavaScript
- Filtering array to contain palindrome elements in JavaScript
- Array filtering using first string letter in JavaScript
- Filtering out the non-unique value to appear only once in JavaScript
- Form a sequence out of an array in JavaScript
- Count number of primes in an array in C++
- Get the closest number out of an array in JavaScript
- Filtering of JavaScript object

Advertisements