Add and subtract numbers with JavaScript


(Meghana Murthy) #1

How to we code in hype so that i can add coding to change the temperature digits when ressing the + and - sign


#2

Considering that mobile devices are more common these days, maybe don’t use keyboard commands. You can use something like sliders instead…

That’s a free template that shows how to use JavaScript to calculate the length of a hypotenuse, using the pythagorean theorem. That basic idea could be modified to use onscreen buttons or other types of input.

If you absolutely want to use the keyboard, you can use a key event in JavaScript. This tool might help you get the information you need…

…because there’s the old way of detecting key events… keyCodes …and the new way e.key.

window.addEventListener("keydown", move);

function move(e) {
    if (e.keyCode == "37" || e.keyCode == "97" || e.keyCode == "65") {
        // left
    } else if (e.keyCode == "38" || e.keyCode == "119" || e.keyCode == "87") {
        // up
    } else if (e.keyCode == "39" || e.keyCode == "100" || e.keyCode == "68") {
        // right
    } else if (e.keyCode == "40" || e.keyCode == "115" || e.keyCode == "83") {
        // down
    }

}

The above code us WASD and arrow key movement. If you wanted plus or minus, you could use 189 for Minus and 187 for Plus. The old way uses numbers. The new way… e.key instead of e.keyCode …lets you be more specific. The “Minus” key is different from the “NumpadSubtract” key. (Unfortunately, the newer method doesn’t have as good browser support, so maybe e.keyCode is good enough for what you’re doing.)

That’s a lot of information. If you’re confused, maybe just start with the basic idea of adding JavaScript to your project…


#3

Thanks @photics! – did this answer your question @meggi?


(Meghana Murthy) #4

nope but i got help in another forum but thanks for the help @Photics