Wednesday, 7 June 2017

How can i use process.stdout for multiplying numbers?

I am doing an online assessment where the employer is requesting me to use process.stdout.write for my code instead of console.log. I'm not really understanding how to use this. For instance if a question reads, create a function that takes in a number and prints the square of this number, how can I do this?

Normally with console.log I would do...

function squarer(n){
   console.log(n*n);
}

With this I have tried:

function squarer(n){
       process.stdout.write(n*n);
    }

But this is giving me errors, can someone please tell me how to use this?

EDIT:

Here is the error:

    > function squarer(n){
...        process.stdout.write(n*n);
...     }
undefined
> squarer(3);
TypeError: Invalid data, chunk must be a string or buffer, not number
    at WriteStream.Socket.write (net.js:655:11)
    at squarer (repl:2:16)
    at repl:1:1
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:96:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:340:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:538:10)
> 



via mohammad chughtai

No comments:

Post a Comment