I want to create an array of sequences from "1" to a specified number.

Asked 1 years ago, Updated 1 years ago, 450 views

const createNum=(maxNum)=>{
constary_result=[];
for (let index=1; index<maxNum;index++){
    aary_result.push(index);
    if(index===maxNum){
        returnary_result;
    }
}
};
console.log(createNum(100));

// result
// [1, 2, 3, ..., 100]  

Specifies a number and inserts it into the function.
Then an array is created, and 1, 2, 3, and 4 are sequentially incorporated into the array.
I thought the number would be substituted and then stopped, and the array would be returned.

Thank you for your cooperation.

javascript array

2023-02-26 19:14

1 Answers

for (let index=1; index<maxNum;index++)

Because index is less than maxNum, the following logic is not performed:

if(index===maxNum){
        returnary_result;
    }

Change the for statement to the following and

for (let index=1; index<maxNum;index++)

You can revert at the end of the loop (returnary_result;).

const createNum=(maxNum)=>{
    constary_result=[];
    for (let index=1; index<=maxNum;index++) {
        aary_result.push(index);
    }
    returnary_result;
};
console.log(createNum(100)); 


2023-02-26 23:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.