mike vervair
mike vervair

Reputation: 33

Javascript prompt within a function not returning the value I expect

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

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

perimeterBox(justAsk);

When I run it the prompt pops up. However when I enter the length, width (ex:7,3) I get a value of null. Thought?

Upvotes: 1

Views: 141

Answers (1)

Tibos
Tibos

Reputation: 27823

You need to extract the values from the string inputted by the user:

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);

Upvotes: 1

Related Questions