admin管理员组

文章数量:1290226

I am new to coding and I have pleted Codecademy's HTML, CSS and Javascript courses, and now I am making a very simple game.

I am trying to add 5 to my variable score, but I don't know how! I can use ++score to add 1, but I don't know how to add 5.

My simplified code for adding 1 is:

<html>
<head>
<title>Webpage</title>
<script>
var score = 0;
function add1() {
    alert("Adding +1 to your score!");
    ++score;
    alert(score);
    };
</script>
</head>
<body>
<button onclick="add1()";> Add One </button>
</body>
</html>

I am new to coding and I have pleted Codecademy's HTML, CSS and Javascript courses, and now I am making a very simple game.

I am trying to add 5 to my variable score, but I don't know how! I can use ++score to add 1, but I don't know how to add 5.

My simplified code for adding 1 is:

<html>
<head>
<title>Webpage</title>
<script>
var score = 0;
function add1() {
    alert("Adding +1 to your score!");
    ++score;
    alert(score);
    };
</script>
</head>
<body>
<button onclick="add1()";> Add One </button>
</body>
</html>
Share Improve this question edited May 24, 2014 at 20:36 filype 8,39010 gold badges45 silver badges69 bronze badges asked May 24, 2014 at 20:30 user3672496user3672496 451 gold badge1 silver badge5 bronze badges 7
  • 1 did you try score + 5? – srrvnn Commented May 24, 2014 at 20:31
  • Try jQuery – JJJ Commented May 24, 2014 at 20:33
  • using [score + 5] instead of [++score] returns a value of 0. (it doesn't work) – user3672496 Commented May 24, 2014 at 20:34
  • 1 @LeeTaylor It was clearly a joke. And a bad one considering the OP is new to programming and likely has never even heard of JQuery. – Chris Cirefice Commented May 24, 2014 at 20:43
  • 1 Try with this: score += 5; – keypaul Commented May 24, 2014 at 20:45
 |  Show 2 more ments

4 Answers 4

Reset to default 4

shortest / easiest + 5 in Javascript

 score += 5;

add + 5 to the score and set that as the score

 score = score + 5;
function add1() {
    alert("Adding +5 to your score!");
    score = score + 5;
    alert(score);

    };

You can add this in.It's like the web storage Api if you disable the alert.Learn more

var score = 0;
document.getElementById("boy").innerHTML = +score;

function correct() {
alert("And that is correct!")
++score;
document.getElementById("boy").innerHTML = +score;

return true;
}

function wrong() {
alert("And that is wrong!")

return false;
}


var times = 0;
document.getElementById("hey").innerHTML = +score;

function yourname() {
++times;
document.getElementById("hey").innerHTML = +times;

}
<p>
Score:<span id="boy"></span>
</p>

<h2>Is javascript powerful?</h2>

<button onclick='correct()'>Yes</button>
<button onclick='wrong()'>No</button>

<h2>If you went into the w3schools link.This feels familar.</h2>

<p>
You have clicked the button:<span id="hey"></span> times
</p>

<button onclick="yourname()">Click me!!!</button>

You maybe will not see the snippet.

Javascript:

 var score = 0;
document.getElementById("boy").innerHTML = +score;

function correct() {
alert("And that is correct!")
++score;
document.getElementById("boy").innerHTML = +score;

return true;
}

function wrong() {
alert("And that is wrong!")

return false;
}


var times = 0;
document.getElementById("hey").innerHTML = +score;

function yourname() {
++times;
document.getElementById("hey").innerHTML = +times;

}

Html:

<p>
Score:<span id="boy"></span>
</p>

<h2>Is javascript powerful?</h2>

<button onclick='correct()'>Yes</button>
<button onclick='wrong()'>No</button>

<h2>If you went into the w3schools link.This feels familar.</h2>

<p>
You have clicked the button:<span id="hey"></span> times
</p>

<button onclick="yourname()">Click me!!!</button>

本文标签: javascriptAdding scores to a variableStack Overflow