函数内的Javascript提示没有返回我期望的值

时间:2014-02-11 18:05:48

标签: javascript

var perimeterBox = function(length, width) {
    return (length * 2) + (width * 2);
};

var justAsk = prompt("what is the length, width?");

perimeterBox(justAsk);

当我运行它时会弹出提示。但是当我输入长度,宽度(例如:7,3)时,我得到一个null值。以为?

1 个答案:

答案 0 :(得分:1)

您需要从用户输入的字符串中提取值:

var perimeterBox = function(length, width) {
    return (length * 2) + (width * 2);
};

var justAsk = prompt("what is the length, width?"); // justAsk is "7,3"

var values = justAsk.match(/\d+/g); // values is ["7", "3"]
var width = parseInt(values[0]); // width is 7
var height = parseInt(values[1]); // height is 3

perimeterBox(width, height);