简体   繁体   中英

Creating an array.push doesn't work

I'm doing a Javascript course and the excersise is:

Write a range function that takes two arguments, start and end , and returns an array containing all the numbers from start up to (and including) end.

So I wrote this nice peace of code:

var range= function(start, end) {  
    var numbers = [];
    for(start; start == end ; start++ ) {                            
        numbers.push(start);  
    };        
    return numbers;
}; 

console.log(range(3,10));

But the outcome is this: [ ] --> which looks like the array is empty

I think you'll need to change the for to:

for(; start <= end ; start++) {

The condition is an expression to be evaluated before each loop iteration, if this expression evaluates to true, statement is executed.

Change the for condition

 var range = function(start, end) { var numbers = []; for (start; start <= end; start++) { numbers.push(start); }; return numbers; }; console.log(range(3, 10));

你的for循环有一个错误的陈述:

 for(start; start <= end; start++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM