Uncaught SyntaxError: Unexpected identifier
Я извиняюсь, если вопрос тупой, я только учу JS. Вот программка Банкомат и в 32 строке в консольке выдаёт Uncaught SyntaxError: Unexpected identifier...:help: :help: :help:
var ATM = { is_auth: false, current_user: false, cash: 1000, users:[ {number: "0025", pin: "132", debet: 675, group: "user"}, {number: "0001", pin: "000", debet: 1000000, group: "user"}, {number: "1111", pin: "111", debet: 0, group: "admin"} ], getcash: function(amount) { if(this.is_auth){ if(amount > 0 && amount <= this.cash && this.users[this.current_user].debet >= amount){ this.cash -= amount; this.users[this.current_user].debet -= amount; return amount; } else { return "no money"; } }else{ return "go auth"; } }, auth: function(number, pin){ for(i=0; i < this.users.length; i++) { if(this.users[i].number == number && this.users[i].pin == pin) { this.is_auth = true; this.current_user = i; break; } } } loadcash: function(bablo){ if(this.is_auth){ if(bablo > 0 && this.users[i].number == number && this.users[i].pin == pin && this.users[i].group == "admin") { this.cash = this.cash + bablo; } else { return "you are not admin"; } }else{ return "go auth"; } }, } }; |
Понял, запятой нет, но код всё равно не работает(
|
Думаю, проблема где-то в отступах и else'ах.
Вот этот вариант работает: var ATM = { is_auth: false, current_user: false, cash: 1000, users:[ {number: "0025", pin: "132", debet: 675, group: "user"}, {number: "0001", pin: "000", debet: 1000000, group: "user"}, {number: "1111", pin: "111", debet: 0, group: "admin"} ], getcash: function(amount) { if (this.is_auth) { if(amount > 0 && amount <= this.cash && this.users[this.current_user].debet >= amount){ this.cash -= amount; this.users[this.current_user].debet -= amount; return amount; } else { return "no money"; } } else { return "go auth"; } }, auth: function(number, pin){ for (i=0; i < this.users.length; i++) { if (this.users[i].number == number && this.users[i].pin == pin) { this.is_auth = true; this.current_user = i; break; } } }, loadcash: function (bablo) { if (this.is_auth) { if (bablo > 0 && this.users[i].number == number && this.users[i].pin == pin && this.users[i].group == "admin") { this.cash = this.cash + bablo; } else { return "you are not admin"; } } else { return "go auth"; } } }; |
Кстати, в самой программе кое-где есть ошибки. Например, условие в loadcash использует несуществующую переменную number.
Вообще, такие длинные условия лучше разбивать на логические переменные. Например, так: … var user = this.users[i] babloEst = (bablo > 0), numberMatches = (user.number == number), pinMatches = (user.pin == pin), isAdmin = (user.group == "admin"); if (babloEst && numberMatches && pinMatches && isAdmin) { … |
Часовой пояс GMT +3, время: 10:05. |