I just don't understand why does this function return an empty array instead of newArr = [1, 2, 3, etc.] depending on the length of the array.
function randomFunction(num) {
var newArr = [];
for(var i = 1; i < num.length; i++) {
newArr.push(i);
}
return newArr;
};
If num
is supposed to be the length of the new array, and the last number of the range of values, you have to use it directly, instead of using length
(which is meant to be used for an array):
function randomFunction(num) {
var newArr = [];
for(var i = 1; i <= num; i++) {
newArr.push(i);
}
return newArr;
};
var array = randomFunction(5);
console.log(array);
Also, you might want use <=
instead of <
, in case you want to start the value by 1 and go through n
, and not n - 1
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments