countdownString

Easy · Loops and Iterations · 3 test cases

Given a positive integer, create a string that counts down from that number to 1, with each number separated by a space. For example, countdownString(5) should return '5 4 3 2 1'.

Examples

countdownString(5) → '5 4 3 2 1'
countdownString(3) → '3 2 1'
countdownString(1) → '1'

Starter code

function countdownString(num) {

}

Complexity of the optimal solution

Time O(n), space O(n). The loop runs n times, and at each step, an element is pushed to an array. The `join` method then iterates through this array. Both time and space complexity are proportional to the input number n.

Solve countdownString in the browser