
- 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
Positive integer square array JavaScript
Let’s say, we have an array that contains some numbers, positive, negative, decimals and integers. We have to write a function that takes in an array and returns an array of square of all the positive integers from the original array.
Let’s write the code for this function −
Example
const arr = [1, -4, 6.1, 0.1, 2.6, 5, -2, 1.9, 6, 8.75, -7, 5]; const squareSum = (arr) => { return arr.reduce((acc, val) => { //first condition checks for positivity and second for wholeness of the number if(val > 0 && val % 1 === 0){ acc += val*val; }; return acc; },0); } console.log(squareSum(arr));
Output
The output in the console will be −
87
- Related Articles
- Finding the smallest positive integer not present in an array in JavaScript
- Show that the square of any positive integer cannot be of the form $6m+2$ or $6m+5$ for any positive integer $m$.
- Minimum positive integer required to split the array equally in C++
- Show that the square of an odd positive integer is of the form $8q+1$, for some integer q.
- Convert integer array to string array in JavaScript?
- Prove that the square of any positive integer is of the form $4q$ or $4q+1$ for some integer q.
- How to negate the positive elements of an integer array in C#?
- Prove that the square of any positive integer is of the form $5q$, $5q+1$, $5q+4$ for some integer q.
- Show that the square of any positive integer is either of the form $4q$ or $4q + 1$ for some integer $q$.
- Show that square of any positive integer is of the form \( 5p, 5p+1, 5p+4 \).
- Prove that the square of any positive integer is of the form 4q or 4q$+$1 for some integer 'q'.
- Show that the square of any positive integer cannot be of the form $5q + 2$ or $5q + 3$ for any integer $q$.
- Show that the square of any positive integer cannot be of the form $6m+ 2$ or $6m + 5$ for any integer $m$.
- (a) Write a negative integer and a positive integer whose sum is $-5$.(b) Write a negative integer and a positive integer whose difference is $-3$.
- Add a positive integer constraint to an integer column in MySQL?

Advertisements