समस्या
हमें एक JavaScript फ़ंक्शन लिखना है जो इंटीजर्स (नेगेटिव और पॉजिटिव) का एक ऐरे लेता है। हमारा फ़ंक्शन सभी पॉजिटिव को नेगेटिव में और सभी नेगेटिव को पॉजिटिव में बदलकर परिणामी ऐरे लौटाना चाहिए।
उदाहरण
निम्नलिखित कोड है −
const arr = [5, 67, -4, 3, -45, -23, 67, 0]; const invertSigns = (arr = []) => { const res = []; for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(+el && el !== 0){ const inverted = el * -1; res.push(inverted); }else{ res.push(el); }; }; return res; }; console.log(invertSigns(arr));आउटपुट
[ -5, -67, 4, -3, 45, 23, -67, 0 ]