当我使用--l = 4和--w = 4运行solve-3时,函数rectangle.perimeter和rectangle.area输出NaN。为什么? 对我来说,似乎输入的整数被转换为字符串,因此为什么我添加了Number(),但这并没有改变任何东西。
文件1:rect-2.js
module.exports = function(l,w,callback) {
try {
if (l < 0 || w < 0) {
throw new Error("Rectangle dimensions should be greater than zero: l = " + l + ", and w = " + w);
}
else
callback(null, {
perimeter: function(l,w) {
return (2*(l+w));
},
area: function(l,w) {
return (l*w);
}
});
}
catch (error) {
callback(error,null);
}
}
文件2:solve-3.js
var argv = require('yargs')
.usage('Usage: node $0 --l=[number] --w=[number]')
.demand(['l','w'])
.argv;
var rect = require('./rect-2');
function solveRect(l,w) {
console.log("Solving for rectangle with length: " + l + " and width: " + w);
rect(l,w, function(err,rectangle) {
if (err) {
console.log(err);
}
else {
console.log("The area of a rectangle with length = "+l+" and width = "+w+" is "+rectangle.area());
console.log("The perimeter of a rectangle with length = "+l+" and width = "+w+" is "+rectangle.perimeter());
}
});
};
solveRect(Number(argv.l),Number(argv.w));
答案 0 :(得分:0)
查看您定义的功能:
a = {'A': {'B': 1, 'C': 3}}
您希望此函数采用两个参数。
然后你没有参数调用它:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
int x, y;
float a,t;
//Inputs
printf("What is the speed that the jet is traveling in km/hr? \nWhat is the distance traveled in meters? \n");
scanf("%d , %d", &x, &y );
//Calculations
a = x * 1 / 60 * 1 / 60 * 1 / 60 * 1000 ;
t = sqrt( y * a / 2 ) ;
//Outputs
printf("The acceleration of the jet is %f meters per second squared. \n", a);
printf("The time it takes for the jet to reach takeoff speed is %f seconds. \n", t);
return 0;
}
您定义的参数是在rect-2文件的较高范围内隐藏的参数。一个简单的解决方法就是删除函数参数:
perimeter: function(l,w) {
return (2*(l+w));
},
现在,这两个函数被称为“关闭”较高范围内的rectangle.perimeter()
和perimeter: function() {
return (2*(l+w));
},
area: function() {
return (l*w);
}
变量。
虽然在l
中包装字符串在这种情况下在技术上会起作用,但请注意w
允许您在命令行上指定用户输入值的类型,例如.number()语法:
Number()