Search posts, tags, users, and pages
Really thorough tutorial! A couple of points, though:
Math.floor( Math.random() * 100 ) + 0 where 100 is the upper bound and 0 is the lower bound.setTimeout for example, in the browser) to do async tasks. However, this claim is wrong. The reason why we have these async functions is because JavaScript is sync and some tasks are blocking. while( true ) for nothing else but readability. ' in the line First of all, what\'s your name?, you can use double-quotes which allow single quotes to co-exist in the string: "First of all, what's your name?"elseif block is much better than isolated conditions. Hi,
thank you for your feedback!
The randomization algorithm is too complex
Depends on the perspective. Game-dev is very math-y, so my intention was to introduce something many people don't even know, which might become important for situations, in which JS's Math API is not available (for example shaders) 😉
With a third-party package
Let me repeat: 50 lines of JS do not need a dependency.
since as the game grows
How big do you intend guess-the-number to grow? I am pretty sure that even with thousands of failed attempts (the range of numbers isn't even that big...), the game will still be just as responsive 🙃 On average, btw, I needed 6 attempts to solve the game.
JavaScript is sync and some tasks are blocking
So what you say is, that every time we want to do IPO (which is what every usual application does), JS is async. So my claim stands. JS, in the application programming world, is async, and we have to deal with it, and I guess we agree on that...
I'd suggest using while( true )
Good to have alternatives. Ask 10 people and everyone will have a different preference, though, and mine is for(;;) 🙂
Instead of escaping [..] you can use double-quotes
True, however I love ' and I love being consistent. You are quite the knit-picker, huh 😁
For the if-then-else ladder, using an elseif block is much better
Actually, I don't see why. It would just add an else without any single benefit... Could you elaborate?
Edit: By the way, this article is mostly just a port of my Rust article, so I understand that for the JS side, some things might work differently. I added a bit of clarification and changed some parts which are purely preference-based. I don't really have a bias, if it's easier to read like that.
Depends on the perspective.
Not quite. There is not need to over-complicate things when they are not required. For the randomization, you might as well use LCG, EE-PRNG, or the LFSR approach. However, owing to the simplicity of the game, you do not need to. If, however, the situation arises that this is used for a very specific purpose, then only should you use it. KISS.
With a third-party package...
Agreed. My bad.
...since as the game grows...
I thought you might add something later. Pardon me for assuming. However, streams are the way you should use anything like pushing. It's a pretty standard JS practice (primarily in Node.)
JavaScript is sync and some tasks are blocking..
JavaScript, by design, is a synchronous language which is executed top-to-bottom. If a code block takes times, the next line will not be executed.
for( ;; );
console.log( 'Hello World!' ); // won't work
You exact statement is:
JavaScript is the same. Except, it's not. It's all async.
It's not all async at all. You use async programming techniques to get pseudo async. I am just talking about what your statement was and its exact implication.
I'd suggest using while( true )
Alright. You can't loop while true is true. So, +1 for semantics. ;)
Instead of escaping [..] you can use double-quotes
Alright.
For the if-then-else ladder, using an
elseifblock is much better
Separate if statements are used when the condition is to be evaluated multiple times. For example:
const caseOne = 'test';
const errors = [];
const warnings = [];
if ( caseOne === 'test' ) {
errors.push( 'Error!' );
} else if ( caseOne === 'not-test' ) {
errors.push( 'Warning!' );
}
Here, only one block should execute. Or more specifically, you only need one of these conditions to be true.
const myArr = [ 'test' ];
const errors = [];
const warnings = [];
if ( myArr.length <= 1 ) {
errors.push( 'Can not execute with just one parameter' );
}
if ( myArr[ 0 ] === 'test' ) {
warnings.push( 'test is not allowed in the argument chain.' );
}
You are achieving the same thing in your code, however, the way you are doing that is semantically incorrect. It's a bad programming practice.
Anyway, further arguing about this is a common waste of time. To one their own! :)
KISS.
might be ok for an application, however not for a tutorial introducing different tools. While the content is simple, I want to add more complex concepts, so that it does not feel overwhelming later on.
You exact statement is
under the headline "Input, Processing, Output". You are tearing my statement out of context.
It's a bad programming practice.
Never heard of that one, but I understand your concern. To be honest, though, I really like Jason's idea with the do..while and ternary operator. I guess we both missed that obvious one, which is pretty dumb haha
further arguing about this is a common waste of time
I don't think so. I do publish articles and write answers in order to learn what others think. I love to argue! And I also update the article with your feedback, because it's valuable.
It's the reason I am on Hashnode. i don't have a mentor myself, and I work with web dev all day, so I sometimes stop thinking and just do dumb things. And I need someone who tells me where to improve 🙂 So thank you for the discussion!
Your last block really excited me! Most of the times, I am shunned because people think I am being argumentative for no apparent reason. Since I started as a self-taught developer, we are hard-wired to argue about what, why, when and where. So, high-five!
KISS
I am a tutor for JavaScript, full stack development and applied cryptography and I have, over the course of some years, realized that sometimes when we teach people advanced concepts, they think it's the go-to method or they don't venture out their comfort zone. Some do, most don't. I have seen it in my own students; and hence I wanted to point it out. I agree with you introducing it right now so that later it's not that daunting, but I prefer to start simple and only go into complex thing if and when required. For example, just for this, I explain the students why this is a bad way to get random number for a secure operation (entropy, etc.) Unless I encounter that, I do not introduce any thing which is complicated in anyway whatsoever. But well, everyone has their own teaching style. If you prefer this method, your wish.
under the headline "Input, Processing, Output".
I am not "tearing it out of context." If you said - "however, when operating - especially in I/O processing - most of JavaScript is modeled to act asynchronously" I would not comment. I know you understand the difference; I am just asking you to rephrase the statement as it is a little ambiguous (the reason why we argued over it);.
It's a bad programming practice.
Jason's idea is honestly something we, I don't know how, missed. However, with tertiary operators, don't you think the code will be a little confusing? And, it implements exactly what I told you. It's an if-elseif ladder. ;)
However, with tertiary operators, don't you think the code will be a little confusing?
Ternary.
... I don't see how, so long as you keep it cleanly formatted it should be reasonable to assume that if the first line is a condition followed by a ?, it's not rocket science to figure out the rest.
But then I've been programming for so long, much of the stuff people have "problems" with leaves me scratching my head, as I didn't have those types of comprehension issues 40 years ago as a pre-teen. Much less the three decades ago when I started writing software in a professional capacity.
... and then I stare at python or ruby confused out of my gourd as to how anyone could call that simple or easy... YMMV.
Pardon my error.
But I guess since this is a beginner tutorial, I have seen people scratch their heads when they encounter nested ternary operators. Do a quick search on SO and you will find thousands of people trying to make sense of something which is pretty every-day for the rest of us. However, with proper explanation I guess it shouldn't be a problem.
and then I stare at python or ruby...
You aren't the only one, trust me.