I'm new to programming and math (only took Math 101 in college), so I'm finding it difficult to tackle this problem:
Develop a function that accepts a number as input and then returns an array containing that number repeated the same amount of times.
This is the code I've written up until now:
function numReturn(x) {
var newArray = [];
if (typeof x === "number") {
return newArray.push[x]* x;
} else {
return null;
}
}
My thought process behind the code is as follows:
- Create a function that can take in a number, say x.
- In that function, initialize an empty array to store values later.
- Verify whether the value entered for x is a number using the
typeof
method. If it's a number, add it to the end of the empty array. Otherwise, returnnull
.
When I test this in the Javascript console by providing a value, it returns as undefined. Can someone provide any guidance?