mabbashm110 / codeacademyjsapi Goto Github PK
View Code? Open in Web Editor NEWCode Academy JavaScript API
Code Academy JavaScript API
This is a function reference, not a function invocation! so the condition is always true
.
At line 140 you set the constant ensureValues
to be a function reference (a truthy value).
You need to invoke the method here, so that it gets evaluated...
if (ensureValues()) {
...
The evaluation of an expression in a boolean context (such as this) yields a boolean value.
This code can be simplified to:
const ensureGameOptions = (playerMoves) =>{
return gameOptions.indexOf(playerMoves) > -1;
};
... and, remembering that function expressions with a single parameter do not require the parentheses, and also using the "implicit return" form, can also drop the braces, can be simplified further:
const ensureGameOptions = playerMoves => gameOptions.indexOf(playerMoves) > -1;
However, I would also suggest renaming the function and variables to make it clearer as to what they are. Note also, you can assign the array to a constant, since it should never change during the run of the program...
const moveTypes = ["rock", "paper", "scissors"];
const isValidType = type => moveTypes.indexOf(type) > -1;
As a final improvement, using the .includes()
array method would improve readability:
const isValidType = type => moveTypes.includes(type);
This is "quite a mouthful" and difficult to verify for correctness at a glance. Also, notice because you have used "positive" tests, each test adds a level of indentation.
My suggestion would be to reverse the sense of the tests, so you can exit the function as soon as you determine that the input is bad (this is known as "fail fast"). This has the benefit of reducing the levels of indentation required. Also, abstract the tests into helper methods, to aid readability of the code.
Note too, that I've chosen shorter names for the parameters to the function.
For example:
const setPlayerMoves = (playerName, m1type, m1value, m2type, m2value, m3type, m3value) => {
if (!m1type || !m1value || !m2type || !m2value || !m3type || !m3value) {
return null;
}
if (!validTypes(m1type, m2type, m3type) || !validValues(m1value, m2value, m3value)) {
return null;
}
// at this point, we know the types and values are good...
// ...
};
const validTypes = (t1, t2, t3) => isValidType(t1) && isValidType(t2) && isValidType(t3);
const validValues = (v1, v2, v3) => ...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.