2017-08-12 55 views
0

我正在爲一個機械網站製作一個JavaScript閃存卡遊戲。因爲我想在卡片上添加方程式,所以我需要使用增量(Δ)符號。三角洲標誌破解JavaScript代碼

一張卡片可能有:「一方的功率方程」和另一方的「P = W /Δt」。如果卡從第一面開始,當按下空格鍵或按下翻蓋按鈕時,它將翻轉。但是,如果它從第二面開始,即帶有Δ符號的那一面,當按下「翻轉」按鈕或空格鍵時,它不會翻轉。

我曾嘗試寫作Δ不同的方式:

Δ Δ Δ 

沒有這些工作的。 我的代碼是:

//Copyright Attribution-ShareAlike 4.0 International 2017 Hazel Meehan 
 

 

 
//array containing all options 
 
var options = [ //counts from 0 
 
    "Joules(J)", "Measure of Work", 
 
    "Watts(W)", "Measure of Power", 
 
    "Ek", "Kinetic Energy", 
 
    "Ep", "Potential Energy", 
 
    "Newtons(N)", "Measure of Force", 
 
    "Pascals(Pa)", "Pressure", 
 
    "ms-1", "Metres per Second", 
 
    "ms-2", "Metres per Second Gained", 
 
    "10N", "Value of Gravity", 
 
    "Weight is a", "Force beginning with W", 
 
    "The capacity to do work", "Energy is", 
 
    "d = ΔvΔt is the equation for", "The equation for distance", 
 
    "W=FΔd is the equation for", "The equation for work", 
 
    "P=W/Δt is the equation for", "The equation for power" 
 
]; 
 

 
//initialize variables 
 
var randomNum = 0; 
 
var sideOne = " "; 
 
var sideTwo = " "; 
 

 
//choose new card using random number 
 
var newCard = function() { //runs on 'next' 
 
    var randomNum = Math.floor(Math.random() * options.length); 
 
    sideOne = options[randomNum]; 
 
    if (randomNum % 2 == 0) { //is the number even 
 
    sideTwo = options[randomNum + 1]; 
 
    } else { 
 
    sideTwo = options[randomNum - 1]; 
 
    } 
 
    document.getElementById("card").innerHTML = sideOne; 
 
}; 
 

 
//show other side of card 
 
var flip = function() { //runs on 'flip' 
 
    if (document.getElementById("card").innerHTML == sideOne) { 
 
    document.getElementById("card").innerHTML = sideTwo; 
 
    } else { 
 
    document.getElementById("card").innerHTML = sideOne; 
 
    } 
 
}; 
 

 
//change card on key down 
 
document.onkeydown = function(e) { 
 
    e = e || window.event; 
 
    if (e.keyCode == '39') { //right arow key 
 
    newCard(); 
 
    } else if (e.keyCode == '32') { //space bar 
 
    flip(); 
 
    } 
 
}
<article> 
 
    <h2>Flashcards</h2> 
 
    <center><button id="flashButton" onclick="newCard()">Next</button></center> 
 
    <br> 
 
    <center><button id="card"></button></center> 
 
    <br> 
 
    <center><button id="flashButton" onclick="flip()">Flip</button></center> 
 
</article>

+1

它在[SO](https://stackoverflow.com)上工作,你應該爲你的html添加一個''標籤以便看到它們。 –

+0

適合我。你使用的是什麼瀏覽器?任何記錄在控制檯? – marekful

+0

我可以重現這個錯誤,當我用'Δ'替換'Δ'時,它會消失。 – ASDFGerte

回答

1

何時檢查元素的innerHtmlif (document.getElementById("card").innerHTML == sideOne)),之前設置的字符串被解釋爲HTML和檢索時,該&Delta;現在將Δ。對比是那麼假:

"d = &Delta;v&Delta;t is the equation for" !== "d = ΔvΔt is the equation for"

因此,再次設置innerHtml同一側。