Камень, ножницы, бумага.
Всем привет. Практикую js на кодакадеми. Вчера писал скрипт и он работает нормально.
<script> var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); if (computerChoice < 0.34) { computerChoice = "rock"; } else if(computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } console.log("Computer " + computerChoice); var choice1 = userChoice; var choice2 = computerChoice; var compare = function (choice1, choice2) { if (choice1 === choice2) { return "The result is a tie!"; } else if(choice1 === "rock") { if(choice2 === "scissors") { return "rock wins"; } else { return "paper wins"; } } else if(choice1 === "paper") { if(choice2 === "rock") { return "paper wins"; } else { return "scissors wins"; } } else if(choice1 === "scissors") { if(choice2 === "paper") { return "scissors wins"; } else { return "rock wins"; } } } console.log(compare(choice1, choice2)); </script> Сегодня решил по памяти написать. Но скрипт выдает undefined. Сравнил и не могу понять где ошибка. <script> var question = prompt("Камень, ножницы или бумага?"); var rndnubmer = Math.random(); if (rndnubmer < 0.34) { rndnubmer = "Камень"; } else if (rndnubmer <= 0.67) { rndnubmer = "Ножницы"; } else { rndnubmer = "Бумага"; } console.log("Компьютер: " + rndnubmer); var choice1 = question; var choice2 = rndnubmer; var complete = function(choice1, choice2) { if (choice1 === choice2) { return "Никто не победил!"; } else if (choice1 === "Камень") { if (choice2 === "Ножницы") { return "Камень победил!" } else { return "Бумага победила!"; } } else if (choice1 === "Бумага") { if (choice2 === "Камень") { return "Бумага победила!" } else { return "Ножницы победили!"; } } else if (choice1 === "Ножницы") { if (choice2 === "Бумага") { return "Ножницы победили!" } else { return "Камень победил!"; } } } console.log(complete(choice1, choice2)); </script> |
hhh,
вы наверно что-то забыли поставить выше вашего скрипта |
Цитата:
|
Цитата:
|
hhh,
запустите ваш скрипт здесь и убедитесь что он работает - подумайте почему здесь работает а у вас нет |
скорее всего просто разные кодировки, так как вы сравниваете русские символы, возможно страница у вас в win-1251 а вводите utf-8 или наоборот, в итоге ни одно условие в функции complete не проходит, о того и возвращается undefined
|
Цитата:
Цитата:
|
hhh,
<!DOCTYPE HTML> <html> <head> <title>Untitled</title> <meta charset="utf-8"> </head> <body> <script> var question = prompt("Камень, ножницы или бумага?", "Камень Ножницы Бумага"); var rndnubmer = Math.random(); if (rndnubmer < 0.34) { rndnubmer = "Камень"; } else if (rndnubmer <= 0.67) { rndnubmer = "Ножницы"; } else { rndnubmer = "Бумага"; } console.log("Компьютер: " + rndnubmer); var choice1 = question; var choice2 = rndnubmer; var complete = function(choice1, choice2) { if (choice1 === choice2) { return "Никто не победил!"; } else if (choice1 === "Камень") { if (choice2 === "Ножницы") { return "Камень победил!" } else { return "Бумага победила!"; } } else if (choice1 === "Бумага") { if (choice2 === "Камень") { return "Бумага победила!" } else { return "Ножницы победили!"; } } else if (choice1 === "Ножницы") { if (choice2 === "Бумага") { return "Ножницы победили!" } else { return "Камень победил!"; } } } alert(complete(choice1, choice2)); </script> </body> </html> |
Цитата:
|
Спасибо, додумался на какую мелочь время тратил:(
|
моя версия :)
<p>Камень? Ножницы? Бумага?</p> <select> <option>Не выбрано</option> <option>Камень</option> <option>Ножницы</option> <option>Бумага</option> </select> <script> document.querySelector("select").onchange = function () { var rndnubmer = Math.random(); if ( rndnubmer < 0.34 ) { rndnubmer = "Камень"; } else if ( rndnubmer <= 0.67 ) { rndnubmer = "Ножницы"; } else { rndnubmer = "Бумага"; } var choice1 = this.options[this.selectedIndex].text; var choice2 = rndnubmer; var mas = [ "Камень", "Бумага", "Ножницы" ] var index1 = mas.indexOf(choice1); var index2 = mas.indexOf(choice2); var dif = index1 - index2; console.log(index1 + " ты - " + choice1 + " : " + index2 + " компьютер - " + choice2); var result; if (dif == 0) { result = "Ничья!"; } else if (dif == 1 || dif == -2) { result = choice1 + " : " + choice2 + " - ты победил!"; } else { result = choice1 + " : " + choice2 + " - ты проиграл!"; } console.log(result); this.selectedIndex = 0; } </script> <p>Камень? Ножницы? Бумага?</p> <script> (function () { var mas = [ "Камень", "Бумага", "Ножницы" ]; var select = document.createElement("select"); select.innerHTML = "<option>Не выбрано</option>" for ( var i = 0, len = mas.length; i < len; i++ ) { select.innerHTML += "<option>" + mas[i] + "</option>"; } document.body.appendChild(select); select.onchange = function () { var rndnubmer = Math.random(); if ( rndnubmer < 0.34 ) { rndnubmer = "Камень"; } else if ( rndnubmer <= 0.67 ) { rndnubmer = "Ножницы"; } else { rndnubmer = "Бумага"; } var choice1 = this.options[this.selectedIndex].text; var choice2 = rndnubmer; var index1 = mas.indexOf(choice1); var index2 = mas.indexOf(choice2); var dif = index1 - index2; var result = choice1 + " : " + choice2; if (dif == 0) { result += " - Ничья!"; } else if (dif == 1 || dif == -2) { result += " - ты победил!"; } else { result += " - ты проиграл!"; } console.log(result); this.selectedIndex = 0; } })(); </script> |
bes,
:write: <p>Камень? Ножницы? Бумага?</p> <select> <option>Не выбрано</option> <option value=0>Камень</option> <option value=1>Ножницы</option> <option value=2>Бумага</option> </select> <script> document.querySelector("select").onchange = function() { var mas = ["Камень", "Бумага", "Ножницы"] var index1 = Math.floor(3 * Math.random());; var index2 = this.value; var dif = index1 - index2; var result; if (dif == 0) { result = " Ничья!"; } else if (dif == 1 || dif == -2) { result = " - ты победил!"; } else { result = " - ты проиграл!"; } alert(" ты - " + mas[index1] + " : компьютер - " + mas[index2] + "\n" + result); this.selectedIndex = 0; } </script> |
рони,
Ну тогда можно еще результирующие условия в тернарные превратить :) |
Супер короткий код!
|
Цитата:
|
Часовой пояс GMT +3, время: 23:07. |