เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่มีสามอาร์กิวเมนต์ -
height --> no. of rows of the array width --> no. of columns of the array val --> initial value of each element of the 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' ] ]