displayKey(key) {
var element = document.getElementById("text")
element.innerHTML += key
navigator.vibrate(30)
}
// Usage
displayKey(a)
displayKey(b)
displayKey(c)
I would also look into extracting the getElementById out so it's only called once and then cached. Probably passed to the function if this is not made into a class.
var isn't broken here, though the prevailing standard is to use only let and const (and for good reason).
A couple key differences...
As you noted, var can create properties on the global object (on the web this is the Window). This happens for any var declaration appearing in the global scope (i.e., not inside a function)
var definitions are hoisted, meaning you can use vars before they are declared
var is function-scoped, while let and const are block-scoped (the latter more matches most people's expectations for scoping)
Rather const but doesn't really matter in an individual functions scope as far as I remember. I tend to use let in JS if const doesn't work basically. OP just used var
Honestly, it's very satisfying once you get it to work. I recently did a quick and dirty write up for a class that used a lot of similar functions with similar logic, but man when I put them all in an abstract class, and it worked, it was so satisfying lol.
That's all well and good until somebody somewhere in the codebase invokes the function in some bizzaro dynamic way, and you don't catch it til it's out on production...
//displaying "q" on click
function displayq() {
var q =
document.getElementById("text")
q.innerHTML += "q";
navigator.vibrate(30);
}
//displaying "w" on click
function displayw() {
var w =
document.getElementById("text")
w.innerHTML += "w";
navigator.vibrate(30);
}
//displaying "e" on click
function displaye() {
var e =
document.getElementById("text")
e.innerHTML += "e";
navigator.vibrate(30);
}
//displaying "r" on click
function displayr() {
var r =
document.getElementById("text")
r.innerHTML += "r";
navigator.vibrate(30);
}
//displaying "t" on click
function displayt() {
var t =
document.getElementById("text")
t.innerHTML += "t";
navigator.vibrate(30);
}
//displaying "y" on click
function displayy() {
var y =
document.getElementById("text")
y.innerHTML += "y";
navigator.vibrate(30);
}
//displaying "u" on click
function displayu() {
var u =
document.getElementById("text")
u.innerHTML += "u";
navigator.vibrate(30)
}
//displaying "i" on click
function displayi() {
var i =
document.getElementById("text")
i.innerHTML += "i";
navigator.vibrate(30);
}
//displaying "o" on clcik
function displayo() {
var o =
document.getElementById("text")
o.innerHTML += "o";
navigator.vibrate(30);
}
//displaying "p" on clcik
function displayp() {
var p =
document.getElementById("text")
p.innerHTML += "p";
navigator.vibrate(30);
}
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/BlackBearFTW, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
So does your virtual keyboard support characters like "<" or ">"? Because you're using innerhtml. You're allowing the user to inject code in the site depending on what keys they're allowed to enter.
You should instead just set the innertext of the element.
261
u/FlamboyantApproval16 Aug 06 '21
When you don't know what function parameters are in JavaScript:
58
u/SuperRLM Aug 06 '21
I refuse to believe this is real—
38
u/NetherFX Aug 06 '21
I love refactoring stuff like this
104
u/GunsDontRapePeople Aug 06 '21
When you get paid per line of code
67
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 06 '21 edited Aug 06 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
12
u/kakotrixis50 Aug 06 '21
Per hour contractors be like:
11
u/rmadlal Aug 06 '21
This is absolutely insane
11
u/dexterdd77 Aug 06 '21
The "clcik", the useless variables, the missing semicolons... Fucking kill me ರ_ರ
8
u/uhimc00l Aug 06 '21
displayy() tho
4
u/Notimecelduv Aug 06 '21
At least they commented their code. That's commendable.
4
u/BlackBearFTW Aug 06 '21
` const addKeyToDisplay = (key) => {
const displayElement = document.querySelector("text"); displayElement.innerHTML += key; navigator.vibrate(30)
} `
1
u/_default_username Aug 08 '21 edited Aug 08 '21
So does your virtual keyboard support characters like "<" or ">"? Because you're using innerhtml. You're allowing the user to inject code in the site depending on what keys they're allowed to enter.
You should instead just set the innertext of the element.
2
u/retrolasered Aug 06 '21
That's only the top row, I want it all
2
u/coolj492 Aug 06 '21
I knew exactly what this would be before I clicked on it
2
u/Fischchen Aug 06 '21
Someone managed to misspell click 2x
2
u/TomatoAppropriate Aug 06 '21
Lol this looks like something that i’d write because I don’t know how to program very well
1
u/gabrielesilinic Aug 06 '21
How to kill yourself by letting your brain rot:
1
u/arzen221 Aug 06 '21
This is better than half of the things I have seen in production to prevent users being aasholes
1
u/rynmgdlno Aug 07 '21
What kind of keyboard is this? Doesn’t even have all the letters /s
1
u/DarkBrave_ Aug 06 '21
I don't even do JavaScript, yet I still know that functions can be better used with parameters.