例如,列表["Hello", "World", "in", "a", "frame"]
打印为:
*********
* Hello *
* World *
* in *
* a *
* frame *
*********
答案 0 :(得分:0)
完全相同的功能:
function printFrame(arr) {
function fill (str, length, char) {
return (str.length < length) ? fill(str + char, length, char) : str;
}
let size = arr.map((str) => {
return str.length;
})
.reduce((a, b) => {
return Math.max(a, b);
});
let line = fill('', size + 4, '*');
arr = arr.map((element) => {
return '* '+ fill(element, size, ' ') + ' *';
})
arr.unshift(line);
arr.push(line);
return arr.join('\n');;
}
console.log(printFrame(["Hello", "World", "in", "a", "frame"]));
&#13;