I'm completely in awe right now, I usually am able to figure this stuff out quickly but this just isn't making any sense to me.
setTimeout(
function(){
if (user1.length || pass1.length <= 6) {
document.getElementById('verified').innerHTML="Error: Username or password too short!";
alert('Running');
setTimeout(function(){location.reload()},1000);
}
if (user1.length && pass1.length >= 7) {
document.getElementById('verified').innerHTML="You've been verified!";
}
}
,2000);
For some reason even if they don't meet the rules, the first if statement is activated and the page reloads/alert pops up.
Both your If statements are wrong. I think what you are looking for is:
if (user1.length <= 6 || pass1.length <= 6)
and
if (user1.length >= 7 && pass1.length >= 7)
When you use If in javascript anything holding a value returns true.
So if you write:
if (user1.length)
it will allways return true. You need to remember <= 6 on both sides of ||
Hope this helps :)
user1.length looks suspicious to me. Are you sure it's Boolean? There is no operator or operand before the ||.
It's because this condition
user1.length || pass1.length <= 6
If user1.length is non-zero then the second part will not even be checked because of short-circuit evaluation.
Did you mean to use logical and &&?
If user1.length is nonzero, that is a sufficient condition alone for the 1st if statement to be executed.
If additionally pass1.length >= 7, then also the 2nd statement will be executed.
Seemingly simple logic statements can be tricky, but it pays to stay cool and thing through them.
I understand now why this wasn't working. I now have
if (user1.length <= 6 || pass1.length <= 6) {
document.getElementById('verified').innerHTML="Error: Username or password too short!";
setTimeout(function(){location.reload()},1000);
}
if (user1.length >= 7 && pass1.length >= 7) {
document.getElementById('verified').innerHTML="You've been verified!";
}
which is working fine. Thanks :)
Related
Just trying to understand on why Buzz doesn't appear in the newline after Fizz for 15.
Trying to learn JavaScript from Eloquent Javascript and just got into doing the FizzBuzz exercise. Note that I've included a commented out solution where it does work (although not elegantly) but the thing I've notice that some solutions searched online show their 15 appearing with Fizz but Buzz is on a newline while my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this? Just curious. The only thing I've noticed is when I use
if ((int%3 == 0) && (int%5 == 0))
either at the end or the beginning of the block is when the changes are visible.
Note:
I'm not asking for solutions. I just want an explanation to my question above. The commented solution does give me FizzBuzz for 15. Please do not misunderstand and thank you for taking your time to answer this.
My solution:
for(let int = 1; int <= 100; int++){
if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
/*if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
}
else if(int%3 == 0){
console.log('Fizz');
}
else if(int%5 == 0){
console.log('Buzz');
}*/
else{
console.log(int);
}
}
In you solution, the following block is dead code :
else if ((int%3 == 0) && (int%5 == 0)){
console.log('Fizz'+'Buzz');
This console.log('Fizz'+'Buzz') can never be reached because ((int%3 == 0) && (int%5 == 0)) would mean that (int%3 == 0) and so the first if is executed. Because of the meaning of else if, this later code block is never reached.
So to answer directly :
show their 15 appearing with Fizz but Buzz is on a newline
This probably is a coding error as FizzBuzz usually requires writing "Fizz Buzz" on a single line for 15. I would guess they did not use any "else if" - which you did.
my solution (which is not commented out) only shows Fizz.
Can anyone explain to me why does it do this.
Because else if blocks order is important, and you chose the wrong one.
If you remove else from else if(int%5 == 0) you will get your desired output I guess.
You should reverse the order of your if statements as you have in the commented out section. Otherwise, when int = 15, your code will match true for
if(int%3 == 0){
console.log('Fizz');
}
And it will never reach the other if statements.
I have a class assignment in which I must create a function that tests 4 integers for a combination lock. For example, let's call the parameters a, b, c, and d. In the function :
a can be 3, 5, or 7
b must be 2
c can be anywhere between 5 and 100, including 5 & 100
and d can be less than 9 or more than 20, but neither 9 not 20 will work
The function must test all of these numbers as "correct" for the combination to be correct. I've used switch statements to get the numbers to return properly, but is there a way I can test for all 4 numbers at once and for the code to stop when it reaches an incorrect number?
I tried something like the code below, but it returned as undefined. Probably because it is long and nonsensical.
checkLock = function(a,b,c,d) {
if (a === 3 || 5 || 7 && b === 2 && c >= 5 || c <= 100 && d < 6 || d >
20) {
return "correct";
} else {
return "incorrect";
}
};
I've tried using some switch statements and alternating if..else statements. But it doesn't catch incorrect combinations.
I've looked at these resources on W3 Schools about switch statements, this article about nesting if...else statements, and articles from Front Stuff and other stack overflow questions to try and figure it out. I'm at a brick wall now.
Since this is for a class assignment, I won't give you the code. Instead I will give you a hint.
Your if statement is invalid.
a === 3 || 5 || 7
You need a left hand assignment for the 5 and 7:
a === 3 || a === 5 || a === 7
You could also return early by first checking a. Something like:
if (a !== 3 && a !== 5 && a !== 7) {
return 'incorrect'
}
This way would make the code neater, and the code after that would be simpler since you now know that a is correct.
I'm trying to randomize objects in an object.
do{
e = enemyList[Math.floor(enemyList.length * Math.random())];
} while (e.level > (player.level + 5) && e.level < (player.level - 5));
return e;
How would I make it so "e" has to be between 5 levels above and 5 levels below in order for the loop to stop?
Yeah this is really easy, but my head hurts for some reason :p
I believe you should be using an or not an and. If it is 5 below OR if it is 5 above. Otherwise the condition will never be met.
You have the comparisons the wrong way round (+ should be - and vice versa):
} while (e.level > (player.level - 5) && e.level < (player.level + 5));
(The wording of your question is somewhat ambiguous; it could be that you should be using >= and <= instead of > and <.)
This code seems to loop through adding 1 to player1.score untill the score is === to whatever i put in the second if statement. Anyone know why?
pointScored: {
startNextSet: function(Scorer) {
if (gameController.bananasTaken < 3 && Scorer === "player1") {
console.log(gameController.player1.score);
gameController.player1.score += 1;
if (gameController.player1.score === 10 &&
gameController.bananasTaken === 0 &&
gameController.player1.bananaCount === 0) {
console.log(gameController.player1.score);
gameController.updatePlayerStats(gameController.Banana1, 20, gameController.canvas.height
- 20 - gameController.Banana1.height, gameController.player1, "left");
console.log("player 1's first point");
}
I'm currently learning about using a debugger but thought i'd leave this here to see if anyone knows why. Thanks.
There's a chance your values get evaluated as strings. The === operator doesn't do any type conversions, that's why its faster.
Consider changing your evaluation to use ==. The same issue has cropped up in another question.
I have refactored your code a bit & used the == notation I suggest above. Please try running it and tell me if it works.
pointScored:{
startNextSet: function(Scorer) {
gc=gameController; //to save thy fingers from typing ache
if (gc.bananasTaken > 2 || Scorer !== "player1")
return;
console.log(gc.player1.score); // this logs 6 times from 0 to 5
gc.player1.score += 1;
if (gc.player1.score == 5 && gc.bananasTaken == 0) {
alert(gc.player1.score); //*******!
if(gc.player1.bananaCount == 0) {
gc.updatePlayerStats(gc.Banana1, 20, gc.canvas.height - 20 - gc.Banana1.height, gc.player1, "left");
console.log("player 1's first point");
}
}
}
}
As I look at your function, it seems that this logic needs to be INSIDE the gameController object.
I want to validate that a given number is within a given range:
function validate(min, max, amount) {
return (min <= amount <= max);
}
But this does not seem to work properly. I know I can do it with two comparison and a logical AND, but I would like to it in one comparison. Is there any NATIVE javascript way to realize this?
Use this instead :
return (min <= amount && amount <= max);
There is no shortcut. And there is a good reason the JavaScript engine can't guess your intent : what you typed was valid, it just isn't interpreted as you'd like. You were testing, in fact
((min <= amount) <= max)
and the result of the first comparison, which is a boolean, is converted to 0 or 1 for the second one (more details here about operators precedence, those comparison operators are left-to-right).
If you really want a shortcut, you could do this :
Number.prototype.isin = function(m,M) {
return m<=this && this<=M;
};
console.log(0.5.isin(1,2)); // logs false
console.log(3..isin(2,5)); // logs true
but I personally would use the standard solution with && that everybody can read and which doesn't involve an additional function call.
Aside : I could have called my function in instead of isin but it might break ES3 browsers.
Operators ( == , <= , < , >= , == ) take only 2 arguments.
When there are more arguments it uses mathematical order of computing. So in fact your code behave like:
min <= amount // true
true <= max // this is illogical
It's also optimal, because when executing logical statements and finding something like:
if(false && (some computing))
It will ignore (some computing) because result will be always false
This is very common practive in every language. Test like this will not have NullPointerException error, because first argument is already false.
if(obj != null && obj.getValue() > 10) //C++,Java, etc.
if(obj !== undefined && obj.val() > 10) // javascript
if(obj.length != 0 && obj.val() > 10) //jQuery