1

I'm trying to write a function which will return a rectangle of *'s. It keeps throwing up an error whenever I try to use \n. I assume i'm using it incorrectly but don't know how - can anyone help?

function makeRectangle(m, n) {
    return '*'.repeat(m) \n.repeat(n);
}

2 Answers 2

3

You must concat \n using + operator before next repeat

function makeRectangle(m, n) {
  return ("*".repeat(m) + "\n").repeat(n)
}

console.log(makeRectangle(3, 4))

Sign up to request clarification or add additional context in comments.

Comments

0

The newline needs to be enclosed as a string:

function makeRectangle(m, n) {
    return ('*'.repeat(m) + '\n').repeat(n);
}

or just for fun, a string literal version, to eliminate the '\n' entirely.

function makeRectangle(m, n) {
    return `${`${'*'.repeat(m)}
`.repeat(n)}`;
}

console.log(makeRectangle(5, 6))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.