do while doesn't work - javascript

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}

Related

One if statement doesn't work and the rest do (Javascript)

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.

How to show a div depending on the numbers of games

I have a question and I can't resolve it. Can you help me please?
So my php code :
$aTicketsGames = array(134, 137, 165, 136, 177, 181, 190);
$b_isRapidGame = false;
if(in_array($i_jeu, $aTicketsGames)){
$b_isRapidGame = true;
}
In .js after click on the button I do :
if(obj.google_analytics.is_rapid_game == true){
var i++;
if(i == 10){
document.getElementById('div1').setAttribute('class','display-block');
}
if(i==20){
document.getElementById('div2').setAttribute('class','display-block');
}
}
The idea is : suppose I have a variable i signifying the number of games played.
if i = 10,30,50,70,90.... show div1.
if i = 20,40,60,80,100.... show div2.
I'm newbie in .js and I have no idea how to impliment this. Help me please!!!!
Thx in advance.
Use style instead of class and use % for the if condition. Also, i should be set globally not locally.
var i = 0;
if(obj.google_analytics.is_rapid_game == true){
i++;
if(i % 20 == 10){
document.getElementById('div1').setAttribute('style','display-block');
}
else if(i % 20 == 0 && i > 0){
document.getElementById('div2').setAttribute('style','display-block');
}
}
See comments inline:
var i = 0; // Define it out of `if`.
if (obj.google_analytics.is_rapid_game === true) {
i++; // Increment by 1
if (i && (i % 20) === 0) { // If divisible by 20
$('#div2').show(); // Show `div2`
// OR
$('#div2').addClass('display-block'); // Add Class
} else if (i && (i % 10) === 0) { // If divisible by 10
$('#div1').hide(); // Hide `div1`
// OR
$('#div1').removeClass('display-block'); // Remove Class
}
}
var i = 0;
if (obj.google_analytics.is_rapid_game === true) {
i++;
if ((i % 20) === 0) {
$('#div2').show();**//or** $('#div2;).css('display','block');
$('#div1').hide();**//or** $('#div1;).css('display','none');
} else if ((i % 10) === 0) {
$('#div1').show();
$('#div2').hide();
}
}

If statements and conditions

I am trying to confirm two different things in an alert box, if statement. The first is that the user pressed the yes button and the second is a user input value on the page. See the code below. I'm still pretty green, any help would be greatly appreciated.
var cMsg = "You are about to reset this page!";
cMsg += "\n\nDo you want to continue?";
var nRtn = app.alert(cMsg,2,2,"Question Alert Box");
if(nRtn == 4) && (getField("MV").value == 5)
{
////// do this
}
else if(nRtn == 4) && (getField("MV").value == 6)
{
/////then do this
}
else if(nRtn == 4) && (getField("MV").value == 7)
{
/////then do this
}
}
else if(nRtn == 3)
{
console.println("Abort the submit operation");
}
else
{ //Unknown Response
console.println("The Response Was somthing other than Yes/No: " + nRtn);
}
Your if...else syntax is incorrect. Correct syntax
if (condition)
statement1
[else
statement2]
Use correct syntax
if (nRtn == 4 && getField("MV").value == 5) {
////// do this
} else if (nRtn == 4 && getField("MV").value == 6) {
/////then do this
} else if (nRtn == 4 && getField("MV").value == 7) {
/////then do this
} else if (nRtn == 3) {
console.println("Abort the submit operation");
} else { //Unknown Response
console.println("The Response Was somthing other than Yes/No: " + nRtn);
}
instead of
if (nRtn == 4) && (getField("MV").value == 5) {
////// do this
} else if (nRtn == 4) && (getField("MV").value == 6) {
/////then do this
} else if (nRtn == 4) && (getField("MV").value == 7) {
/////then do this
} <=== Remove this
} else if (nRtn == 3) {
console.println("Abort the submit operation");
} else { //Unknown Response
console.println("The Response Was somthing other than Yes/No: " + nRtn);
}
you are trying to evaluate two different conditions that is:
"nRtn" // value returned from app.alert(cMsg,2,2,"Question Alert Box");
and: getField("MV").value.
However, the compiler will only process the first condition because the braces end there. You should make sure to enclose all conditions within one brace. Individual conditions can and by convention should be in separate braces too within the main brace.
The right way therefore should be:
if ((nRtn == 4) && (getField("MV").value == 5))
//notice the initial if (( and terminating ))
//You could have 3 conditions as follows
// if (((condition1) && (condition2)) && (condition3)))
{
////// do this
}
else if((nRtn == 4) && (getField("MV").value == 6))
{
/////then do this
}
else if((nRtn == 4) && (getField("MV").value == 7))
{
/////then do this
}
}
else if(nRtn == 3)
{
console.println("Abort the submit operation");
}
else
{ //Unknown Response
console.println("The Response Was somthing other than Yes/No: " + nRtn);
}

Shortening if, else if, else if ... else using loops

I have the following lines of code:
$(function(){
$("div").scroll(function() {
function hpos(id) {
var pos = $("#" + id).position();
return pos.top;
}
function final(id) {
$("#header").html($("#" + id).html()),
$("h1").css("visibility","visible"),
$("#" + id).css("visibility","hidden");
}
if (hpos(5) < 0) {
final(5);
}
else if (hpos(4) < 0) {
final(4);
}
else if (hpos(3) < 0) {
final(3);
}
else if (hpos(2) < 0) {
final(2);
}
else {
final(1);
}
});
});
Shouldn't I be able to shorten it by using a loop instead of the else if statements? I can't find a way to make the loops work with my position().
for (var i = 5; i > 0; i--){
if (hpos(i) < 0) {
final(i);
break;
}
}
would something like this work? Not tested by the way
This should be shorter:
$.each([5,4,3,2], function(i,v) {
if( hpos(v) < 0 ) {
final(v);
return false;
} else if( v === 2 ) {
final(1);
}
});
An easier way to do lots of else if statements is to use the case method.
In case you need a while version:
:)
var elemId = 5;
while (elemId > 1) {
if (hpos(elemId) < 0) {
break;
}
elemId--;
}
final(elemId);

IF- ELSE condition - run the code in the ELSE section

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) {
...
...
...
...
...
}

Categories

Resources