seneca-in-practice
seneca-in-practice copied to clipboard
Can not convert string to number by using isFinite()
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
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.
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.