0

another noob javascript question here. i have the following code, but i would like a way to append the Ids 'lost' and 'score' with a "I" every time the player either loses or wins (as in, you've guessed right 3 times and wrong 4 so there would be "III" in wins and "IIII" in loses). how can this best be done with javascript??

<html>
<head>
  <title>Sarah's Card Game</title>
  <script type="text/javascript">
  Array.prototype.shuffle = function() {
            var len = this.length;
            var i = len;
            while (i--) {
                var p = parseInt(Math.random()*len);
                var t = this[i];
            this[i] = this[p];
            this[p] = t;
            }
        };
    </script>
</head>
<body>
    <body onLoad="number=0"></p>
            <p>This is the fun card game, a number will appear, afterwards, guess the      card below        which matches the number shown!</p>

  <div id="click"><button onCLick="Choose()">Button</button></div>
    <div id="random"></div>
    <div id="score"></div>
    <div id="lost"></div>
    <script type="text/javascript">
        function Choose() {
                var array = [1,2,3,4];
                array.shuffle();
                number = array[0];
                document.getElementById("random").innerHTML=("Number is: " + number); 
                }

    </script>
    <div id="cards">
    <button onClick="Picker()">Card 1</button>
    <button onClick="Picker()">Card 2</button>
    <button onClick="Picker()">Card 3</button>
    <button onClick="Picker()">Card 4</button>
    <script type="text/javascript">
        function Picker() {
                var array2 = [1,2,3,4];
                array2.shuffle();
                number2 = array2[0];
                if (number==0) {
                    alert('please pick a number first!');
                }
                else if (number2==number)  {
                    alert('you won!');
                    var wins = document.getElementById('score');
                    wins.lastChild.nodeValue = wins.lastChild.nodeValue+ document.getElementById('score').value;
                } 
                else if (number2!=number){
                    alert('you lost!');
                    var loses = document.getElementById('lost');
                    loses.lastChild.nodeValue = loses.lastChild.nodeValue+ document.getElementById('lost').value;
                }
        }

    </script>
    </div>

1 Answer 1

3
document.getElementById("score").innerHTML += "I";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.