Click here to Skip to main content
15,500,220 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to read data from STDIN and display data to STDOUT using javascript + nodejs.

Below are the problem statements .

1. Display the sum of the numbers using recursion from 1 till the number entered in command line.
Ex : if I enter 5 in command line then it should print the sum as 15.( 1 + 2 + 3 + 4 + 5)

2. Display the square of only positive real numbers in an array.
Ex : if I enter [-7,2.5,3,4] in command line, then I should get [9,16];

What I have tried:

For problem 1 , I have written the below code .

var n = process.argv[2];
recurSum(n);

recurSum(n)

{
if(n == 1)
console.log(1);
console.log(n + recurSum(n - 1));
}

But it throws error "maximum call stack size exceeded".


For problem 2 , I want to know how can i read data from the array which is entered in command line using javascript.
Posted
Updated 6-Aug-20 7:23am
Comments
Richard MacCutchan 6-Aug-20 15:19pm    
I do not think that Javascript can read from the console as it normally runs in the browser. There may be some test framework that allows it.

That is because your recursion never ends. You need to add the following changes:
JavaScript
recurSum(n)
{
    if (n > 0) // do nothing if n is less than 1
    {
        if(n == 1)
            console.log(1);
        else // only recurse if n is greater than 1
            console.log(n + recurSum(n - 1));
    }
}


[edit]
The above code has a simple flaw. The following is the correct implementation:
JavaScript
function recurSum(n) {
    if (n > 0) { // do nothing if n is less than 1
        if (n == 1) {
            return 1; // console.log(1);
        }
        else { // only recurse if n is greater than 1
            return n + recurSum(n - 1);
        }
    }
}

[/edit]
 
Share this answer
 
v2
Comments
AmitabhaGhosh123 7-Aug-20 3:34am     CRLF
hi, when i enter 1 in the command line , I get the sum as 1 which is correct. But when I enter 2 or any other number , I get the below output. 1 2undefined This is not the correct output which is wanted . Desired output : It should print the sum from 1 till the number entered in the console.
Richard MacCutchan 7-Aug-20 3:58am    
That is because the function does not return its calculated value. Se my updated code above.
AmitabhaGhosh123 7-Aug-20 5:21am     CRLF
the above solution is not working when in replace the return with console.log. It is giving me the below output 3 3undefined.
Richard MacCutchan 7-Aug-20 5:35am     CRLF
Why have you changed the code to make it stop working? A recursive function must have a return value in order to wrok properly. You should move your consol.log commands to the point after the function is called.
answer = recurSum(value);
console.log(answer);
AmitabhaGhosh123 7-Aug-20 5:23am     CRLF
i am taking the input from command line. i am calling the function like this. recurSum(process.argv[2]) where process.argv[2] is the number given in the command line.
Every recursive function needs a way to "bailout". Some way of determining that it should no longer call itself.

Your function has no bailout condition so it will call itself endlessly.

Properly indent your code and this would have become apparent. Your if statement only outputs 1 to the console log when n == 1. There's no exit of the function before calling itself again.
recurSum(n)
{
    if(n == 1)
        console.log(1);

    // See something wrong here?
    console.log(n + recurSum(n - 1));
}
 
Share this answer
 
v4
Comments
AmitabhaGhosh123 6-Aug-20 14:05pm     CRLF
Hi, Can you please answer my 2nd question also?
Dave Kreskowiak 6-Aug-20 14:32pm     CRLF
Nope. I don't do javascript very much at all. But, you can Google for "javascript read command line arguments" and "javascript parse string into array".
AmitabhaGhosh123 7-Aug-20 3:44am    
can you please help me to correct the code ?
Dave Kreskowiak 7-Aug-20 10:14am     CRLF
Nope. I'm not doing your homework for you. You wouldn't learn the things you need to learn to pass the class, and that would be thinking about the problem and the algorithm to solve it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900