首页 > 解决方案 > JS 提示符和循环参数中的函数语法

问题描述

我很尴尬地发布了我在 You Don't Know JavaScript: Up and Going 中的第一个练习练习的尝试。” 但是,我希望有人能告诉我我在这里做错了什么(肯定是多件事)。我被困住了在工作中,所以我使用 AWS Cloud9 JS 编译器。我不断收到的当前错误与我的每个提示有关。它显示“ReferenceError:提示未定义。”我是否在 while 循环中使用了正确的语法?

另外,我觉得 StackOverflow 是针对更具体和更高级别的问题的。基本的愚蠢初学者问题在哪里?

// Declare the prices of the various phones& accessories in stock and tax 
rate.
const TAX_RATE = 0.09;
const IPHONE = 500;
const GALAXY = 469;
const WINDOZE_PHONE = 369;
const EL_CHEAPO = 69;
const SCREEN_PROTECTOR = 10;
const DONGLE = 12;
const CASE = 20;
const CAR_CHARGER = 20;

//initialize global variables.
var accessories =0;
var chosenPhone;

//create a function we can call each time we need to check which phone is 
selected.
function whichPhone(phonePrompt){
var phonePrompt = prompt("Enter 1, 2, 3, or 4 for an iPhone, Samsung 
Galaxy," + "Windows Phone, or Cheap Phone respectively.");

switch (phonePrompt){
        case (1):
            chosenPhone = IPHONE;
        case (2):
            chosenPhone = GALAXY;
        case (3):
            chosenPhone = WINDOZE_PHONE;
        case (4):
            chosenPhone = EL_CHEAPO;
            break;
        default:
            alert("You gotta enter a number 1 through 4. If not, ya broke it");
        }
    }

//create a function to add the price for each accessory added to the bill
function upsell(confirmUpsell){
    var confirmUpsell = confirm("Would you like to buy any acessories today?");

    if (confirmUpsell === true){
        var accessoryPrompt1 = confirm("Would you like a fancy screen protector?");
            if (accessoryPrompt1 === true){
                accessories += SCREEN_PROTECTOR;
            }
        var accessoryPrompt2 = confirm("Would you like a dongle?");
            if (accessoryPrompt2 === true){
                    accessories += DONGLE;
                }
        var accessoryPrompt3 = confirm("Would you like a protective case?");
            if (accessoryPrompt3 === true){
                accessories += CASE;
            }
        var accessoryPrompt4 = confirm("Would you like a car charger?");
            if (accessoryPrompt4 === true){
                accessories += CAR_CHARGER;
            }
    else { accessories = 0;
        }
    }
}

/*Endlessly buy phones and opt to buy accessories until your bank acocunt no
longer suffices for a phone. Be sure to include an option for being too broke
to buy any phone to begin with.
Determine which phone the user wants and see if they can afford it.
Then check if they can afford accessories.*/

var bankBalance = prompt("Enter your current bank balance.");

whichPhone();

    while (whichPhone.chosenPhone < bankBalance){
        upsell();
        var salePrice = ((chosenPhone + accessories) * TAX_RATE) + (chosenPhone + accessories);
        bankBalance -= salePrice;
        console.log("You have $" + bankBalance + "remaining.");
    }

标签: javascriptfunctionloopswhile-loop

解决方案


推荐阅读