समस्या
हमें एक JavaScript फ़ंक्शन लिखना है जो संख्या और पूर्णांकों के स्ट्रिंग प्रतिनिधित्व के मिक्स्ड ऐरे लेता है। हमारा फ़ंक्शन स्ट्रिंग इंटीजर्स को जोड़ना चाहिए और इसे गैर-स्ट्रिंग इंटीजर्स के कुल से घटाना चाहिए।
उदाहरण
निम्नलिखिक कोड है −
const arr = [5, 2, '4', '7', '4', 2, 7, 9]; const integerDifference = (arr = []) => { let res = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(typeof el === 'number'){ res += el; }else if(typeof el === 'string' && +el){ res -= (+el); }; }; return res; }; console.log(integerDifference(arr));आउटपुट
कंसोल आउटपुट निम्नलिखिक है −
10