हमें एक JavaScript फंक्शन लिखना है जो तीन आर्गुमेंट लेता है:
height --> array की पंक्तियों की संख्या width --> array के कॉलम की संख्या val --> array के प्रत्येक एलिमेंट का प्रारंभिक मान
फिर फंक्शन को इन मानदंडों के आधार पर बने नए ऐरे को लौटाना चाहिए।
उदाहरण
इसके लिए कोड होगा:
const rows = 4, cols = 5, val = 'Example';
const fillArray = (width, height, value) => {
const arr = Array.apply(null, { length: height }).map(el => {
return Array.apply(null, { length: width }).map(element => {
return value;
});
});
return arr;
};
console.log(fillArray(cols, rows, val));
आउटपुट
कंसोल में आउटपुट होगा:
[
[ 'Example', 'Example', 'Example', 'Example', 'Example' ],
[ 'Example', 'Example', 'Example', 'Example', 'Example' ],
[ 'Example', 'Example', 'Example', 'Example', 'Example' ],
[ 'Example', 'Example', 'Example', 'Example', 'Example' ]
]