हमें एक फ़ंक्शन लिखना है जो एक एरे लेता है और उस एरे में मौजूद सभी जीरो को बिना किसी अतिरिक्त स्थान का उपयोग किए एरे के अंत में ले जाता है। हम यहां Array.prototype.forEach() मेथड का उपयोग Array.prototype.splice() और Array.prototype.push() के साथ करेंगे।
फ़ंक्शन के लिए कोड होगा -
उदाहरण
const arr = [34, 6, 76, 0, 0, 343, 90, 0, 32, 0, 34, 21, 54]; const moveZero = (arr) => { for(ind = 0; ind < arr.length; ind++){ const el = arr[ind]; if(el === 0){ arr.push(arr.splice(ind, 1)[0]); ind--; }; } }; moveZero(arr); console.log(arr);आउटपुट
कंसोल में आउटपुट होगा -
[34, 6, 76, 343, 90, 32, 34, 21, 54, 0, 0, 0, 0]