Powers of 2
Complete the function that takes a non-negative integer n as input, and returns a list of all the powers of 2 with the exponent ranging from 0 to n ( inclusive ).
function powersOfTwo(n: number): number[] {
let acc: number[] = [];
for (let i = 0; i <= n; i++) {
acc.push(Math.pow(2, i));
}
return acc;
}
console.log(powersOfTwo(10));
package main
import "fmt"
func powersOfTwo(n int) []int {
acc := make([]int, n+1)
for i := 0; i <= n; i++ {
acc[i] = int(math.Pow(2, float64(i)))
}
return acc
}
func main() {
fmt.Println(powersOfTwo(10))
}
def powers_of_two(n):
acc = []
for i in range(n+1):
acc.append(2**i)
return acc
print(powers_of_two(10))
function powersOfTwo(int $n): array {
$acc = [];
for ($i = 0; $i <= $n; $i++) {
$acc[] = pow(2, $i);
}
return $acc;
}
print_r(powersOfTwo(10));
Go to Kata
Want us to give you some help with your business? No problem, here's a video on who we can help, and how, along with our calendar so you can book in a call to discuss us working together.
Let's see