Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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
