-1

所以我有一个任务,我现在已经做了几个小时,并且非常坚持其中的一些部分。因此,我坚持的部分是必须使用循环来验证放入提示中的信息,并使用数组中的信息与另一个函数中的变量相吻合,最后显示所有这些信息。

所以我已经设置好了所有东西,但是如果有人介意帮助我指出正确的方向,我不知道我到底出了什么问题?哦,我可能应该提到我试图让第二个函数与数组一起使用,因此当用户输入一个数字(1 到 4)时,它与数组中的价格匹配。

function numSeats() {
        //var amountSeat=document.getElementById("price");
        var amountSeat=prompt("Enter the amount of seats you would like");
            amountSeat=parseInt(amountSeat);
                for (i=7; i<amountSeat; i++){
                    if (amountSeat<1 || amountSeat>6) {
                        alert("Check the value of " + amountSeat);
                        location.reload(true);
                    }else{
                        alert("Thank You");}
                    }

        return amountSeat;}

        function seatingChoice() {
        //var seatChoice=document.getElementById("table").innerHTML;
        var seatChoice=prompt("Enter the seat location you want.");
            seatChoice=parseInt(seatChoice);
                for (i=7; i<seatChoice; i++){
                    if (seatChoice<1 || seatChoice>4) {
                        alert("Check what you entered for " + seatChoice);
                        location.reload(true);
                    }else{
                        alert("Thank You")}
                    }

        return seatChoice;}



  var price=new Array(60, 50, 40, 30);
        var name=prompt("Please enter your name.");
            if (name==null || name=="")
                {
                    alert("You did not enter a name, try again");
                    location.reload(true);
                }
            else 
                {
                    alert("Thank You");
                }

        document.write(name + " ordered " + numSeats() + " for a total dollar amount of " + seatingChoice(

));

4

1 回答 1

1

在我看来,您在numSeats和中都重复了相同的错误seatingChoice

让我们看看你在用你的循环做什么

var amountSeat = prompt("Enter the amount of seats you would like");
for (i=7; i<amountSeat.length; i++) {/* amountSeat[i] */}
  • prompt向客户询问String,所以amountSeatString
  • amountSeat.length因此是String中的字符数。
  • 您从 开始循环i = 7,因此amountSeat[i]从 中的7第 th 个字符开始amountSeat(假设 中至少有7字符amountSeat

在我看来,您更像是想从提示中获取数字;

// string
var amountSeat = prompt("Enter the amount of seats you would like");
// to number
amountSeat = parseInt(amountSeat, 10); // radix of 10 for base-10 input

接下来,考虑您的if

if (amountSeat[i]<1 && amountSeat[i]>6) {

这就是说if 小于1 AND 大于 6。没有一个数字可以同时是这两种状态,所以它总是false。看起来您想使用OR||

// do your check
if (amountSeat < 1 || amountSeat > 6) { /* .. */ }

最后,您似乎想通过一些逻辑来计算价格,但您没有包括在内。但是,我确信它将基于numSeatsseatingChoice因此您需要保留对这些选择的引用。

于 2013-10-28T03:08:34.040 回答