seneca-in-practice icon indicating copy to clipboard operation
seneca-in-practice copied to clipboard

Can not convert string to number by using isFinite()

Open ChopperLee2011 opened this issue 7 years ago • 2 comments

In the override Exercise, the tutorial suggests using isFinite() method to convert msg.left and msg.right to number and check if they are numbers, then sum values here. but after I run the code, it still concat two strings like '1' and '0' to '10', not 1. And I make a example code here

ChopperLee2011 avatar Jun 28 '17 07:06 ChopperLee2011

if I install this lib from npm, the test code in the seneca-override-executor.js is

 seneca.act({role: 'math', cmd: 'sum', left: process.argv[3], right: process.argv[4]}, function (err, result) {
    if (err) return console.error(err)
    console.log(result)
  })

which is not the same here.

ChopperLee2011 avatar Jun 28 '17 07:06 ChopperLee2011

isFinite doesn't convert and return the string to number it just tests whether the string you provided is a finite number and return True or False.

to finish this exercise you have to convert the left and right arguments to numbers in the sum microservice using Number() global function.

this.add({role: 'math', cmd: 'sum'}, (msg, reply) => {
    const sum = Number(msg.left) + Number(msg.right);
    reply(null, {answer: sum})
  });

In an exercise after you will create an override microservice that convert left and right for all your math plugin microservices.

mmayla avatar Oct 10 '17 15:10 mmayla