Trying to Write a function that takes a list of strings an prints them, one per line, in a rectangular frame

For example the list [“Hello”, “World”, “in”, “a”, “frame”] gets printed as:


  • Hello *
  • World *
  • in *
  • a *
  • frame *

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script language="JavaScript">

    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"]));
</script>
</body>
</html>

not working crome :frowning:

As i tested at chrome it works fine

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.