![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Changing positivity/negativity of Numbers in a list in JavaScript
We are required to write a JavaScript function that takes in an array of positive as well as negative Numbers and changes the positive numbers to corresponding negative numbers and the negative numbers to corresponding positive numbers in place.
Example
The code for this will be −
const arr = [12, 5, 3, -1, 54, -43, -2, 34, -1, 4, -4]; const changeSign = arr => { arr.forEach((el, ind) => { arr[ind] *= -1; }); }; changeSign(arr); console.log(arr);
Output
The output in the console −
[ -12, -5, -3, 1, -54, 43, 2, -34, 1, -4, 4 ]
Advertisements