हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो एक कड़े गणितीय समीकरण को लेता है। फ़ंक्शन को फ़ंक्शन को दिए गए समीकरण का परिणाम वापस करना चाहिए।
उदाहरण के लिए:यदि समीकरण −
. हैconst str = '1+23+4+5-30';
तब आउटपुट 3
. होना चाहिएउदाहरण
इसके लिए कोड होगा -
const str = '1+23+4+5-30';
const compute = (str = '') => {
let total = 0;
str = str.match(/[+\−]*(\.\d+|\d+(\.\d+)?)/g) || [];
while (str.length) {
total += parseFloat(str.shift());
};
return total;
};
console.log(compute(str)); आउटपुट
और कंसोल में आउटपुट होगा -
3