Search code examples
javascriptfunctionparametersprompt

Custom Times Table using Function Parameters


i am trying to do a task here involving function parameters but seem to be a little stuck, i was wondering if someone could give me some guidance.

I am suppose to make a function that takes as parameters the times table required and the values at which it should start and end. For example, you might try the four times table displayed starting with 4 * 4 and ending at 4 * 9.

this is my code so far:

var start = prompt("Which number would you like to begin with?");
console.log("Starting number is " + start);

var lowerLimit = prompt("Where in the times table would you like to begin?");
console.log("Lower Limit is " + lowerLimit);

var upperLimit = prompt("Where in the times table would you like to end?");
console.log("Upper Limit is " + upperLimit);

timesTable(start);



function timesTable( start ) {

    for (var i = lowerLimit; i <= upperLimit; i++) {
        var result = start * i;
        console.log(result);
        //document.write(result + '<br>');
    }

}

The three prompt values seem to be saving but i cant seem to get the output to work, appreciate it if someone could help, thanks.


Solution

  • prompt returns string value.

    So you should have something like this:

    upperLimit = parseInt(upperLimit,10);

    in order this for (var i = lowerLimit; i <= upperLimit; i++) to use numeric comparisons.