Javascript Übungen (3)

22.05.2017 11:31 Uhr

w3schools Übung while

w3schools Übung for

Beispiel für while:

    var choice1;
    var choice2;
    var choice3;
    var user;
    var computer
    var scoreU;
    var scoreC;

    var win;
    var lose;
    var tie;
    var ergebnis;


//Level 4: While-Schleife
function play() {

    //Userauswahl
    choice1 = document.getElementById('c1');
    choice2 = document.getElementById('c2');
    choice3 = document.getElementById('c3');
    user = "";
    scoreU = 0;
    scoreC = 0;

    win = "Du gewinnst!";
    lose = "Du verlierst!";
    tie = "Unentschieden!";

    choice1.onclick = function () {
        user = "Schere";
        showX();
    };
    choice2.onclick = function () {
        user = "Stein";
        showX();
    };
    choice3.onclick = function () {
        user = "Papier";
        showX();
    };
}



    function showX() {
        //Computerauswahl
        computer = Math.random();
        if (computer < 0.34) {
            computer = "Schere";
        } else if (computer <= 0.67) {
            computer = "Stein";
        } else {
            computer = "Papier";
        }
        compare();

  }


        //var compare = "";
        function compare() {
            if (user == computer) {
                //return ("Unentschieden!");
                ergebnis = tie;
            }
            else if (user == "Stein") {
                if (computer == "Schere") {
                    ergebnis = win;
                } else {
                    ergebnis = lose;
                }
            } else if (user == "Papier") {
                if (computer == "Stein") {
                    ergebnis = win;
                } else {
                    ergebnis = lose;
                }
            } else if (user == "Schere") {
                if (computer == "Stein") {
                    ergebnis = lose;
                } else {
                    ergebnis = win;
                }
            }
            if (ergebnis == win) {
                scoreU += 1;
            } else if (ergebnis == lose) {
                scoreC += 1;
            }

            document.getElementById("details").innerHTML = "User: " + user + "<br>PC: " + computer + "<br>" + ergebnis;
            document.getElementById("score").innerHTML = "Dein Ergebnis: " + scoreU + "<br>Computer: " + scoreC;


            while (scoreC == 5 || scoreU == 5) {
                if (scoreU > scoreC) {
                    document.getElementById("score").innerHTML = "Du bist der Held!";
                    break;
                } else if (scoreC == scoreU) {
                    document.getElementById("score").innerHTML = "Das war knapp!";
                    break;
                } else if (scoreU < scoreC) {
                    document.getElementById("score").innerHTML = "Leider verloren... Probier's nochmal!";
                    break;
                }
            }
        }

Zu den Sitzungsaufgaben SS17

Zu den Hausaufgaben SS17