Neutralisation of strings - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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 −

-
raja
Published on 16-Sep-2020 09:51:07
Advertisements