Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Why is this line of code not working?

RyuuSan

New Coder
[CODE title="Processing.JS code"]if( (keyPressed === true) && (key === 'a') ) {println("yes");}[/CODE]

So what I'm looking for is to be able to type the "a" key and get "yes" in the (I think it's called a) console thing-y. (If this is stupidly simple, sorry, I'm a beginner.)
 
Hey.

I think it'd be better if we got to see more of the code. Mainly where and how you've declared and initialised your variables. Also, I don't think that println() is actually a thing in JS, but maybe that's just me. I don't work with JS, but that doesn't mean that I cannot help you.
 
Master Yoda said:
Hi @RyuuSan perhaps trying removing the second pair of () from your condition. Not sure if that will work but perhaps it might.
No, that wouldn't change anything(At least, I don't think it would in JavaScript). I tested the same sort of thing in C and the syntax that he used(So, two pairs of parentheses inside a pair of parentheses), does work. Languages like JS inherit heavily from C, so if those two pairs of parentheses inside the condition were to be removed, it wouldn't change much at all, except for the way it was written.

Using something like pairs of parentheses inside a pair of parentheses is acceptable and it can possibly be used to make the code more readable, depending on the language, it's syntax, and the variables in use. Although, do note that there are times when a pair of parentheses inside a pair of parentheses are mandatory.
 
No, that wouldn't change anything(At least, I don't think it would in JavaScript). I tested the same sort of thing in C and the syntax that he used(So, two pairs of parentheses inside a pair of parentheses), does work. Languages like JS inherit heavily from C, so if those two pairs of parentheses inside the condition were to be removed, it wouldn't change much at all, except for the way it was written.

Using something like pairs of parentheses inside a pair of parentheses is acceptable and it can possibly be used to make the code more readable, depending on the language, it's syntax, and the variables in use. Although, do note that there are times when a pair of parentheses inside a pair of parentheses are mandatory.
It doesn’t hurt to try it..
 
if kePressed is boolean all you need to do is check it.

JavaScript:
keyPressed = true;

key = 'a';

if (keyPressed && key === 'a') {

    console.log('yes');

} else {

    console.log('no');

}
 
Last edited by a moderator:
if kePressed is boolean all you need to do is check it.

keyPressed = true;
key = 'a';
if (keyPressed && key === 'a') {
console.log('yes');
} else {
console.log('no');
}
Thank you for sharing your solution! However, just make sure you wrap your code BBCode. Makes it easier to look at :)
 
Most likely because the key pressed doesn't actually have a normal value.
So for example, in JS the key code for the letter 'a' would be '97'. This might be a problem if the key variable is grabbing the unicode value of the key instead of the letter "a".
 
Most likely because the key pressed doesn't actually have a normal value.
So for example, in JS the key code for the letter 'a' would be '97'. This might be a problem if the key variable is grabbing the unicode value of the key instead of the letter "a".
That honestly sounds like that could be it! Very neat. Thanks for sharing the link @Ghost.

@RyuuSan has this helped you?
 
Hey.

I think it'd be better if we got to see more of the code. Mainly where and how you've declared and initialised your variables. Also, I don't think that println() is actually a thing in JS, but maybe that's just me. I don't work with JS, but that doesn't mean that I cannot help you.

Oh, I'm coding with Processing.JS right now. I couldn't find it on the menu so I just picked JavaScript.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom