So, I'm working on a game and the way to level up is through the money variable. I decided to use an interval and run the function upc every 0.099 seconds and when I run the code, you can get up to about at the point where it says if(money > 1000005 && Cookies.get*("modal2alrshown"). The Cookies.get is just a JS library.
function upc(){
if(money > 1000){
irp.style.display = "inline";
irptxt.style.display = "inline";
irpbtn.style.display="inline";
} else {
return false
}
if(money > 1005 && Cookies.get("modal1alrshown") != "yep"){
npamodal("./assets/iron_pickaxe.png","Iron Pickaxe")
Cookies.set("modal1alrshown","yep");
}
if(money > 1000000){
gp.style.display = "inline";
gptxt.style.display = "inline";
gpbtn.style.display="inline"
} else {
return false;
}
if(money > 1000005 && Cookies.get("modal2alrshown") != "yep"){
npamodal("./assets/gold_pickaxe.png","Gold Pickaxe")
Cookies.set("modal2alrshown","yep");
} else {
return false;
}
if(money > 5000000){
bgep.style.display = "inline";
bgptxt.style.display = "inline";
bgpbtn.style.display="inline";
} else {
return false;
}
if(money > 5000005 ){
upcontent.innerHTML = "<div id='up1-close'>To exit, click on sky</div><h1 style='text-align:center;'>New Pickaxe Availible!</h1><img src='./assets/baguettepick.png' height='120px'>Baguette Pickaxe<br>And you got a <br><h3>Travel Ticket<img src='./assets/travelticket.png' height='20px'></h3>"
$("#up1-content, #up1-background").toggleClass("active");
Cookies.set("modal3alrshown","yep");
Cookies.set("travelTicket","1");
} else {
return false;
}
}
It could be that one of your if statements that return from the function have a false expression
// Potentially this is the faulty expression
if(money > 1000005 && Cookies.get("modal2alrshown") != "yep"){
....
} else {
// Causing this return to fire, exiting the function
return false;
}
If statement will always work. You need to debug that either money > 1000005 to be true or Cookies.get("modal2alrshown") != "yep" should be true.
Related
This is the sample of my code:
Function isEven(number) {
(number == 1) {
return false;
}
else if (number == 0) {
return true;
}
else {
number += 2;
return isEven(-number);
}
};
This is the result I keep getting:
Console.log(isEven(50)); // undefined
Try using;
function isEven(number) {
if(number == 1) {
return false;
}
else {
if (number == 0) {
return true;
}
else {
number -= 2;
return isEven(number);
}
}
}
console.log(isEven(50));
You had a lot of syntax error as well as logical error. Note this logic only works for positive number. Hope it helps.
I'm building a website but a JavaScript part doesn't work.
Look here the script:
do {
if (percen === 0) {
console.log();
} else if (percen === 1) {
document.getElementById("percen").innerHTML = "Text"
} else if (percen === 2) {
document.getElementById("percen").innerHTML = "Text"
} else if (clicks === 3) {
++percen;
} else if (clicks === 4) {
++percen;
} else if (clicks === 5) {
++percen;
} else if (clicks === 6) {
++percen;
} else if (clicks === 7) {
var percen = 0;
}
}
But when I run it in a HTML file it will not loop. The var "percen" will ++ when you use a button.
Try adding the 'while' portion of the 'do while' loop at the end:
do {
if (percen === 0) {
console.log();
}
// rest of your code
// ...
}
while (percen < 100);
You forgot the condition at the end.. It is while(condition) remember that the condition is checked at the end of the script so it will enter 1 time. If you want you can do a while syntax while(cond){yourscript}
Return false isn't doing quite what I want it to do. I want it to prevent the form submit if any one of the conditionals passes, but I still want it to proceed down the tree of conditionals. Right now if the first conditional if ($(".item.active").length == 0) { passes, it hits the return false; and stops the later conditionals from checking.
How can I rewrite this to work better?
$('#go').click(function() {
function invalidBtn(){
$('#go').addClass('invalid');
setTimeout(function() {
$('#go').removeClass('invalid');
}, 5000)
}
$('.error').remove();
$('.invalid').removeClass('invalid');
if ($(".item.active").length == 0) {
$(".item:first-of-type").before('<h5 class="error">Select a shirt type</h5>');
invalidBtn();
return false;
} else {
if ( $(".item.active .size-select .active").length == 0) {
$('.item.active .size-select').before("<div class='error'>Select a size</div>").addClass("invalid");
//$(this).addClass('invalid');
invalidBtn();
return false;
}
if ($(".item.active .gender-select").length > 0 ) {
if ( $(".item.active .gender-select .active").length == 0 ){
$('.item.active .gender-select').before("<div class='error'>Select a gender</div>").addClass("invalid");
//$(this).addClass('invalid');
invalidBtn();
return false;
}
}
}
if ( !$('#fn-field').val() ) {
$('#fn-field').before("<div class='error'>Enter your first name</div>").addClass("invalid");
//$(this).addClass('invalid');
invalidBtn();
return false;
}
if ( !$('#ln-field').val() ) {
$('#ln-field').before("<div class='error'>Enter your last name</div>").addClass("invalid");
//$(this).addClass('invalid');
invalidBtn();
return false;
}
});
Thanks so much
Simply replace all of your return false to change a boolean, and return the boolean, like this:
$('#go').click(function() {
function invalidBtn(){
$('#go').addClass('invalid');
setTimeout(function() {
$('#go').removeClass('invalid');
}, 5000)
}
var retVal = true;
$('.error').remove();
$('.invalid').removeClass('invalid');
if ($(".item.active").length == 0) {
$(".item:first-of-type").before('<h5 class="error">Select a shirt type</h5>');
invalidBtn();
retVal = false;
} else {
if ( $(".item.active .size-select .active").length == 0) {
$('.item.active .size-select').before("<div class='error'>Select a size</div>").addClass("invalid");
//$(this).addClass('invalid');
invalidBtn();
retVal = false;
}
if ($(".item.active .gender-select").length > 0 ) {
if ( $(".item.active .gender-select .active").length == 0 ){
$('.item.active .gender-select').before("<div class='error'>Select a gender</div>").addClass("invalid");
//$(this).addClass('invalid');
invalidBtn();
retVal = false;
}
}
}
if ( !$('#fn-field').val() ) {
$('#fn-field').before("<div class='error'>Enter your first name</div>").addClass("invalid");
//$(this).addClass('invalid');
invalidBtn();
retVal = false;
}
if ( !$('#ln-field').val() ) {
$('#ln-field').before("<div class='error'>Enter your last name</div>").addClass("invalid");
//$(this).addClass('invalid');
invalidBtn();
retVal = false;
}
return retVal;
});
Return false isn't doing quite what I want it to do. I want it to prevent the form submit if any one of the conditionals passes, but I still want it to proceed down the tree of conditionals. Right now if the first conditional if ($(".item.active").length == 0) { passes, it hits the return false; and stops the later conditionals from checking.
Right, because return exits the function.
If you want to keep going through the code following it, don't use return, set a variable you return at the end. E.g.:
var valid = true;
if (someInvalidCondition) {
// ...do anyting condition-specific...
valid = false;
}
if (someOtherInvalidCondition) {
// ...do anyting condition-specific...
valid = false;
}
// Rinse, repeat
// Done
return valid;
Or I usually like to return a count of the errors. Or a list (array) of the errors. Etc.
I am trying to combine 2 IF/Else statements to create one validation function. This is my code for the 2 separate validations:
function validateCarsMin(v){
if (tfRateLoc1.getValue() > 0 || tfRateLoc2.getValue() > 0){
if (tfRateLoc3.getValue() > 0){
return '1B cannot contain a value if CW is entered';
}
} else return true
}
function validateRateLoc3(v){
if (v != ''){
if(tfRateLoc3.getValue() < tfRateLoc4.getValue()){
return true;
} else {
return 'This Value is not valid';
}}
}
I did not know if there was a best practice for this and it so, what would it be?
Thanks for the help on the last question I had.
Change the functions to return either true or false. You can push the msgs to an array to be used later.
var errorMsgs = [];
function validateCarsMin(){
if (tfRateLoc1.getValue() > 0 || tfRateLoc2.getValue() > 0){
if (tfRateLoc3.getValue() > 0){
errorMsgs.push('1B cannot contain a value if CW is entered');
return false;
}
} else{
return true;
}
}
function validateRateLoc3(){
if(tfRateLoc3.getValue() < tfRateLoc4.getValue()){
return true;
} else {
errorMsgs.push('This Value is not valid');
return false;
}};
}
function validateForm(){
var isValid = false;
isValid = validateCarsMin();
isValid = (!isValid) ? isValid:validateRateLoc3();
return isValid;
}
Note I removed the v parameter because it seemed irrelevant. It is not used in the first function and it creates a syntax error in the second.
I have got the following IF condition code:
if ((depth <= min_depth ) && (leaf_colour == "red")){
for (i = 0; i < array_2D.length; i++) {
var leaf_size = array_2D[i][1];
if (leaf_size == 10 || leaf_size == 11){
alert("Error message.");
break; // we found an error, displayed error message and now leave the loop
}
else{ go to the next else section }
}
}//end of if condition
else{
...
...
...
...
...
}
Inside the 'FOR' loop, if (leaf_size == 10 || leaf_size == 11), we break the loop and do nothing but if this is not the case, i would like to run the code in the next ELSE section.
I do not want to copy the whole block of code and paste it inside the 'else' section of the for loop as it's quite long.
Is there a way of running the code in the second else section?
You will need to move the code from your second else block into a separate function. You can then call that function wherever you need to run that code:
function newFunction() {
//Shared code. This is executed whenever newFunction is called
}
if(someCondition) {
if(someOtherCondition) {
//Do stuff
}
else {
newFunction();
}
}
else {
newFunction();
}
var ok = (depth <= min_depth ) && (leaf_colour == "red");
if (ok){
for (i = 0; i < array_2D.length; i++) {
var leaf_size = array_2D[i][1];
if (leaf_size == 10 || leaf_size == 11){
alert("Error message.");
ok = false;
break;
}
else{
ok = true;
break;
}
}
}//end of if condition
if(!ok) {
...
...
...
...
...
}