How to return

Asked 2 years ago, Updated 2 years ago, 18 views

How to return

I am thinking about the ◆ part of the following two codes.

[1]
    function rangeOfNumber(start,end){      
        if(start===end){   
            return [startNum];
        } else {  
            var answer = rangeOfNumber(start, end-1);
         ◆ answer.push(end);
          ◆ return answer;
        }   
    }
    rangeOfNumber(5,8); // (4) [5,6,7,8]

.

[2]
   function rangeOfNumber(start,end){       
        if(start===end){   
            return [start];
        } else {  
            var answer = rangeOfNumber(start, end-1);
         ◆ return answer.push(end);
        }   
    }
    rangeOfNumber(5,8);

    /* Error VM2477:6 Uncaught TypeError: answer.push is not a function
        at rangeOfNumber(<anonymous>:6:17)
        at rangeOfNumber (<anonymous>:5:16)
        at<anonymous>:9:1*/

Here are some questions.

Why does [2] have an error?

I would appreciate your help.
I look forward to your kind cooperation.

javascript

2022-09-29 22:42

1 Answers

answer.push adds an item to the array and returns a 32-bit integer as its return value.
Array.push

return answer.push(end);

is

var length=answer.push(end);
return length;

Returns the same 32-bit integer value as the .

Therefore, the return value of the function, the number, is

var answer=rangeOfNumber(start, end-1);

Set to answer in

answer.push(end);

If so,

The push function does not exist in the numeric type, so
push is not a function error is displayed.


2022-09-29 22:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.