程序运行后如何修复未定义的变量

时间:2019-03-31 01:34:29

标签: javascript html function

程序将要求所有输入并打印所有内容,但所有变量均未定义。

这是用于绑定到HTML文档的Web应用程序。运行时不会引发任何错误。

function driver(){
    var plan1Code = "S";
    var plan1Cost = 450;
    var plan1Hours = 2.5;
    var plan1Pics = 75;

    var plan2Code = "G";
    var plan2Cost = 750;
    var plan2Hours = 5;
    var plan2Pics = 125;

    var plan3Code = "P";
    var plan3Cost = 1000;
    var plan3Hours = 8;
    var plan3Pics = 225;

    var retName = getName();
    var retPlan = getPlan();
    var retHours = getHours();
    var retPics = getPics();

    var baseCost, totalCost, upchargeTime, upchargeTimeCost, upchargePics, upchargePicsCost;

    if (retPlan == plan1Code){
        baseCost = plan1Cost;
        upchargeTime, upchargeTimeCost = calcTimeUpcharge(retHours, plan1Hours);
        upchargePics, upchargePicsCost = calcPicsUpcharge(retPics, plan1Pics);
    }
    else if (retPlan == plan2Code){
        baseCost = plan2Cost;
        upchargeTime, upchargeTimeCost = calcTimeUpcharge(retHours, plan2Hours);
        upchargePics, upchargePicsCost = calcPicsUpcharge(retPics, plan2Pics);
    }
    else if (retPlan == plan3Code){
        baseCost = plan3Cost;
        upchargeTime, upchargeTimeCost = calcTimeUpcharge(retHours, plan3Hours);
        upchargePics, upchargePicsCost = calcPicsUpcharge(retPics, plan3Pics);
    }

    totalCost = calcTotalCost(baseCost, upchargeTimeCost, upchargePicsCost);
    print(retName, retPlan, baseCost, upchargeTime, upchargeTimeCost, upchargePics, upchargePicsCost, totalCost);

}

function getName(){
    var text;
    var name = prompt("Enter your name");
    if (name == null) {
        text = "Please enter a valid name";
    }
}

function getPlan(plan){
    var plan = prompt("Enter the selected package");
}

function getHours(hours){
    var hours = prompt("Enter anticipated coverage hours");
}

function getPics(pics){
    var pics = prompt("Enter anticipated number of pictures");
}

function calcTimeUpcharge(hours, baseHours){
    upchargeTime = hours - baseHours;
    var price;
    if (upchargeTime>0){
        var upchargeTimeUnits = Math.ceil((upchargeTime)/.5);
        upchargeTimeCost = upchargeTimeUnits * 50;
    }
    else {
        upchargeTime = 0;
        upchargeTimeCost = 0;
    }
    return upchargeTime, upchargeTimeCost;

}

function calcPicsUpcharge(pics, basePics){
    upchargePics = pics - basePics;
    if (upchargePics>0){
        upchargePicsunits = Math.ceil((upchargePics)/10);
        upchargePicsCost = upchargePicsunits*40;
    }
    else {
        upchargePics = 0;
        upchargePicsunits = 0;
    }
    return upchargePics, upchargePicsCost;
}

function calcTotalCost(baseCost, timeCost, picsCost){
    return baseCost + timeCost + picsCost;
}

function print(retName, retPlan, baseCost, upchargeTime, upchargeTimeCost, upchargePics, upchargePicsCost, totalCost){
    document.write(retName + ", thanks for using Photosarus!" + "\n");
    document.write("<br><br>");
    document.write("You selected plan " + retPlan + " at a cost of "+ baseCost );
    document.write("<br><br>");
    document.write(upchargeTime + "additional hours at a cost of "+ upchargeTimeCost);
    document.write("<br><br>");
    document.write(upchargePics + "additional pictures at a cost of "+ upchargePicsCost);
}

我希望输出显示

比尔,感谢您使用Photosarus!

您以450美元的费用选择了计划S

增加0小时,费用为$ 0

添加0张照片,费用为$ 0

但是我得到了

未定义,感谢您使用Photosarus!

您选择了未定义的计划,但未确定的代价

未定义的额外小时数,但未定义的费用

未定义的其他图片,费用为未定义

1 个答案:

答案 0 :(得分:2)

您需要return这些函数中的值:

function getName(){
    var text;
    var name = prompt("Enter your name");
    if (name == null) {
        text = "Please enter a valid name";
    }
    return name;
}

function getPlan(plan){
    var plan = prompt("Enter the selected package");
    return plan;
}

function getHours(hours){
    var hours = prompt("Enter anticipated coverage hours");
    return hours;
}

function getPics(pics){
    var pics = prompt("Enter anticipated number of pictures");
    return pics;
}