Click here to Skip to main content
14,642,320 members
Rate this:
Please Sign up or sign in to vote.
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 6: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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

That is because your recursion never ends. You need to add the following changes:
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:
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]
   
v2
Comments
AmitabhaGhosh123 7-Aug-20 3:34am
   
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
   
the above solution is not working when in replace the return with console.log.
It is giving me the below output
3
3undefined.
AmitabhaGhosh123 7-Aug-20 5:23am
   
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.
Richard MacCutchan 7-Aug-20 5:35am
   
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 10:20am
   
it is working now , but it is not summing up the values.
if I enter 2 , it is printing 21 instead of 3.

that is the below line is concatenating the values, not returning the sum.
return (n + recurSum(n - 1)).
Richard MacCutchan 7-Aug-20 10:45am
   
Please use the Improve question link above, and show the code that you are now using. The code I have posted works correctly, and I have no idea what you have changed.
AmitabhaGhosh123 7-Aug-20 11:00am
   
Actually I am taking the input from STDIN so I had to change it to Number before sending it to the recurSum function. Now it is working correctly.
AmitabhaGhosh123 7-Aug-20 11:01am
   
One more question .

Suppose I enter in the terminal node <filename> [2,3,4]

Do you have any idea how to change that into array?
Richard MacCutchan 7-Aug-20 11:30am
   
You would need to create an array and then add the items one by one using the push method. Javascript is not really designed to work in a console type environment so it gets rather difficult.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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));
}
   
v4
Comments
AmitabhaGhosh123 6-Aug-20 14:05pm
   
Hi,

Can you please answer my 2nd question also?
Dave Kreskowiak 6-Aug-20 14:32pm
   
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
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100