- Trending Categories
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
Neutralisation of strings - JavaScript
We are required to write a JavaScript function that takes in a string that contains only '+' or '-' and we have to return either '+' or '-' based on the whole neutralisation result of the string.
Like '++' results to '+' and '--' also results to '+' while '-+' or '+-' results to '-'.
Following is our string −
const str = '+++-+-++---+-+--+-';
Example
Following is the code −
const str = '+++-+-++---+-+--+-'; const netResult = (str = '') => { const strArr = str.split(''); return strArr.reduce((acc, val) => { if(acc === val){ return '+'; }; return '-'; }); }; console.log(netResult(str));
Output
Following is the output in the console −
-
Advertisements