Skip to content

#171 Repeat String

Adam Kubiś edited this page Oct 28, 2026 · 1 revision

171 Repeat String

Create a function that takes a string txt and a number n and returns the repeated string n number of times.

If given argument txt is not a string, return Not A String!!

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/171 Repeat String.js"

Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.

Current implementation and tests

This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.

//Create a function that takes a string txt and a number n and returns the repeated string n number of times.
//If given argument txt is not a string, return Not A String!!
function repeatString(txt, n) {
    return typeof txt === "string" ? txt.repeat(n) : "Not A String !!";
}
import { assertEquals } from '../testHelper.js';
assertEquals(repeatString("Mubashir", 2), "MubashirMubashir")
assertEquals(repeatString("Matt", 3), "MattMattMatt")
assertEquals(repeatString(1990, 7), "Not A String !!")
assertEquals(repeatString("*", 3), "***")
assertEquals(repeatString("Hello", 11), "HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello")
assertEquals(repeatString("243624", 22), "243624243624243624243624243624243624243624243624243624243624243624243624243624243624243624243624243624243624243624243624243624243624")
assertEquals(repeatString([], 3), "Not A String !!")
assertEquals(repeatString({}, 3), "Not A String !!")
assertEquals(repeatString(24, 3), "Not A String !!")
assertEquals(repeatString(true, 3), "Not A String !!")
assertEquals(repeatString("Hello", 0), "")

Clone this wiki locally