将分数添加到变量

时间:2014-05-24 20:30:22

标签: javascript html

我是编码的新手,我已经完成了Codecademy的HTML,CSS和Javascript课程,现在我正在制作一个非常简单的游戏。

我正在尝试为我的变量分数添加5,但我不知道怎么做!我可以使用++score添加1,但我不知道如何添加5。

我添加1的简化代码是:

<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>

4 个答案:

答案 0 :(得分:4)

Javascript中最短/最简单+ 5

 score += 5;

答案 1 :(得分:2)

在乐谱中加上+ 5并将其设为分数

 score = score + 5;

答案 2 :(得分:0)

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

    };

答案 3 :(得分:0)

您可以添加它。如果您禁用警报,则就像Web存储Api。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>

您可能看不到代码段。

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>