(Javascript提示应用程序)switchcase内部的switchcase将不会运行



我正在制作一个显示为提示和警报的应用程序(idk该怎么称呼它(。

然而,我的代码似乎无法在switchcase中运行switchcase,我使用switchcase将订单添加到购物车中,并显示购物车的内容。

另一种说法是我的问题:

  1. 我无法将项目添加到我的购物车

  2. 我无法访问我的购物车(提示自动关闭(

为了更清楚,我将在下面包含我的代码以及它的代码笔链接,我将评论我认为问题的位置。

非常感谢所有的投入。

谢谢!

arrayCart =[];
totalBill = parseInt(0);
cartContent = arrayCart.length;
for(;;)
{
userInput = parseInt(prompt('1. Menun2. Your Cartn3. Paymentn4. Exit'))
switch(userInput)
{
// This is to go to Menu
case 1:
inputPesanan = prompt('Silahkan pilih menu yang diinginkan:n1. Paket Bento An2. Paket Bento Bn3. Paket Bento C')

// I think the 1st problem starts here
switch(inputPesanan)
{ 
case 1:
arrayCart.push('Paket Bento A - Rp20.000n');
totalBill += parseInt(20000);
break;
case 2:
arrayCart.push('Paket Bento B - Rp25.000n');
totalBill += parseInt(25000);
break;
case 3:
arrayCart.push('Paket Bento C - Rp30.000n');
totalBill += parseInt(30000);
break;
}
break;
// and the 1st problem ends here
// This is to check the Cart's content
case 2:
// And I think the 2nd problem starts here
inputKeranjang = alert('Isi Keranjang Andan' + arrayCart + 'nn' + 'Total Tagihan Anda: n' + totalBIll)   
break;
// and it ends here
//----------- everything under this line seems to be working fine ---------------------------------------
// This is to input how much money you would like to pay with and calculate the change or deficit (if any)
case 3:    
inputPayment = parseInt(prompt('Total Tagihan Anda :nRp' + totalBill + 'nnBerapa uang yang Anda akan bayarkan?'));
switch(true)
{
case inputPayment<totalBill:
alert('Uang Anda kurang sebesar Rp ' + parseInt(totalBill-inputPayment));
break;
case inputPayment>totalBill:
alert('Anda akan mendapat kembalian sebesar Rp' + parseInt(inputPayment-totalBill));
break;
case inputPayment=totalBill:
alert('Uang Anda pas');
break;
}
break;
}
// This is to end the infinite loop and close the app
if(userInput === 4)
{
break;
}
}

我不是javascript开发人员,但就switch语句而言,

这就是它的工作原理:

  1. 开关表达式求值一次
  2. 将表达式的值与每种情况的值进行比较
  3. 如果存在匹配,则执行相关的代码块

所以无论你把Int作为输入传递给开关("YouInput"(,它都会在所有情况下检查这一点,所以如果你在第一个开关中传递Int作为你的输入,比如说1,那么情况1:如果你传递100,就会执行情况100:如果没有匹配的情况默认会执行,如果你传递字符串,比如说"Yosia",那么如果有任何情况,Yosia:就会执行

因此,在您的第二个开关案例中,您正在检查案例1:,案例2,但我不认为您的"inputPesanan"是int类型的,因为在其他任何地方您都在使用一些parseIn,但不在那里,所以可能会打印并检查您的"InputPesann",它不会是1、2或您正在寻找的案例。

最新更新