Камень, ножницы, бумага.
Всем привет. Практикую 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>
|
Цитата:
|
Спасибо, додумался на какую мелочь время тратил:(
|
| Часовой пояс GMT +3, время: 18:08. |