Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

look for a JavaScript Guru's opinion/help

Featured Replies

Hi,

 

I am trying to learn JavaScript and jQuery so I messing around with if and else.

 

After a lot of messing about I have come to the point where I am wondering if I am doing this correctly or if there is a better way or even a different way.

 

what I am trying to do is to create a variable that starts at 0 and counts to 3 and once its about to go past 3 (ie 4) it will reset the variable to 0 and start counting again

 

this is what I have

 

var i = 0;
$( 'a.test' ).click(function(){
  if ( i <= 3 ){
    //i would like you to do this
    alert ( i )
    //great you did that now add 1 to I
    i(++i);
} else {
   // reset variable i to 0 and run the function again
   i = 0
}

 

this works, every time I click on test i get the alert with the number incremented by 1

 

after the fourth click i == 3 so the if statement is true and it moves to the else which sets i back to 0 but no alert comes up

 

so after some tinkering around I came up with this

 

var i = 0;
$( 'a.test' ).click(function(){
  if ( i <= 3 ){
    //i would like you to do this
    alert ( i )
    //great you did that now add 1 to I
    i(++i);
} else {
   // reset variable i to 0 and run the function again
   i = 0
   alert ( i )
   i(++i);
}

 

while this does work I am not sure if there is a better way to do this.

 

So if you are experienced in JavaScript I would like to hear your opinion as I am learning and want to know if I am doing things correctly.

There is usually a number of different ways of doing anything in javascript and jQuery, and the best method to use really depends on the circumstances, but what you have used above is fine, it gets the job done nicely.

Just another approach with fewer lines of code:

 

$(document).ready(function(){


var i = 0,
max_i = 3;
$("a.test").click(function(){
	alert(i);
	if (i <= max_i){
		//Do something
	}
	i >= max_i ? i = 0 : i++;
});


});

Edited by andyl

  • Author

Just another approach with fewer lines of code:

 

$(document).ready(function(){


var i = 0,
max_i = 3;
$("a.test").click(function(){
	alert(i);
	if (i <= max_i){
		//Do something
	}
	i == max_i ? i = 0 : i++;
});


});

 

Thanks Andy!!

 

This is brilliant. I was trying to do it this way but could not get it to work, I now know what I was doing wrong!

 

thanks!!

  • Author

No problem, glad to help.

 

Thanks for my 200th rep point :drinks:

 

ahhhh cool congrats on the double century!!

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.