*********
* Hello *
* World *
* in *
* a *
* frame *
*********
my code
var x = "hello\nworld\nin\na\nframe";
function star(str) {
let arr = [];
arr = str.split("\n");
for (let index = 0; index < 1; index++) {
console.log("*******");
for (let j = 0; j <= arr.length; j++) {
arr == arr[j].split(",");
console.log("*" + arr[j] + "*" );
}
}
console.log("******");
return arr;
}
console.log(star(x));
This is how I would do it, but I'm sure there are better ways.
function makeStarBox(arr){
const longest = arr.reduce((a, b) => a.length <= b.length ? b : a);
const box_width = longest.length + 2;
console.log("*".repeat(box_width));
arr.map(str => console.log("*" + str + " ".repeat(box_width - (str.length + 2)) + "*"));
console.log("*".repeat(box_width));
}
It does seem like you are posting an actual homework/exam question though, so make sure to study the code and learn for yourself what it actually does. If you don't know what map, reduce or repeat does, check out the documentation and learn:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat