
- 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
Append the current array with the squares of corresponding elements of the array in JavaScript
We have an array of Numbers like this −
const arr = [12, 19, 5, 7, 9, 11, 21, 4];
We have to write a function that takes in such an array and returns a new array with all the items of the original array appended by the squares of corresponding elements of the array.
For this sample array, the output should be −
[12, 19, 5, 7, 9, 11, 21, 4, 144, 361, 25, 49, 81, 121, 441, 16]
Example
const arr = [12, 19, 5, 7, 9, 11, 21, 4]; const multiplyArray = (arr) => { return arr.reduce((acc, val) => { return acc.concat(val * val); }, arr); }; console.log(multiplyArray(arr));
Output
The output in the console will be −
[ 12, 19, 5, 7, 9, 11, 21, 4, 144, 361, 25, 49, 81, 121, 441, 16 ]
- Related Articles
- Constructing an array of smaller elements than the corresponding elements based on input array in JavaScript
- How to duplicate elements of an array in the same array with JavaScript?
- Finding the product of array elements with reduce() in JavaScript
- Grouping array of array on the basis of elements in JavaScript
- Getting elements of an array depending on corresponding values of another JavaScript
- Sort the second array according to the elements of the first array in JavaScript
- Return an array of all the indices of minimum elements in the array in JavaScript
- How to replace elements in array with elements of another array in JavaScript?
- Compute the differences between consecutive elements and prepend & append array of numbers in Numpy
- Compute the differences between consecutive elements and append an array of numbers in Numpy
- Replace the Array Elements by Its Corresponding Rank in Java
- Finding sum of alternative elements of the array in JavaScript
- How to create permutation of array with the given number of elements in JavaScript
- 8086 program to determine product of corresponding elements of two array elements
- 8086 program to determine modulus of first array elements corresponding to another array elements\n

Advertisements