Results 1 to 2 of 2

Thread: Changing If statement to switch

  1. #1
    Join Date
    Jan 2017
    Posts
    29
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Changing If statement to switch

    Hi,

    I'm trying to change this if statement to a switch like this:

    Before

    Code:
    if ($(this).attr('id') === 'right') {
    			if (tabindex === slides) {
    				tabindex = 1;
    				move = '+=' + windowWidth * (slides - 1);
    			} else {
    				tabindex += 1;
    				move = '-=' + windowWidth;
    			}
    		} else {
    			if (tabindex === 1) {
    				tabindex = slides;
    				move = '-=' + windowWidth * (slides - 1);
    			} else {
    				tabindex -= 1;
    				move = '+=' + windowWidth;
    			}
    		}
    After

    Code:
    if ($(this).attr('id') === 'right') {
    
    			switch (tabindex) {
    			case (tabindex === slides):
    				tabindex = 1;
    				move = '+=' + windowWidth * (slides - 1);
    				break;
    			default:
    				tabindex += 1;
    				move = '-=' + windowWidth;
    			}
    
    		} else {
    			switch (tabindex) {
    			case (tabindex === 1):
    				tabindex = slides;
    				move = '-=' + windowWidth * (slides - 1);
    				break;
    			default:
    				tabindex -= 1;
    				move = '+=' + windowWidth;
    			}
    But the result is not the same.
    What am I doing wrong? What is the right solution?

    Thanks

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You're putting too much code into the first line of the case statements. Those need to be (or evaluate to) single values which are automatically compared to the value being switched. See also:

    https://www.w3schools.com/js/js_switch.asp

    This works in the console:

    Code:
    if ($(this).attr('id') === 'right') {
    			switch(tabindex){
    			case slides:
    				tabindex = 1;
    				move = '+=' + windowWidth * (slides - 1);
    				break;
    			default:
    				tabindex += 1;
    				move = '-=' + windowWidth;
    			}
    		} else {
    			switch(tabindex){
    			case 1:
    				tabindex = slides;
    				move = '-=' + windowWidth * (slides - 1);
    				break;
    			default:
    				tabindex -= 1;
    				move = '+=' + windowWidth;
    			}
    		}
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 2
    Last Post: 10-28-2015, 03:38 PM
  2. Resolved switch statement with no page found option
    By ?foru in forum PHP
    Replies: 7
    Last Post: 03-08-2014, 04:38 PM
  3. Replies: 6
    Last Post: 09-08-2013, 01:02 AM
  4. Changing Switch Content Headers
    By portkey in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 09-05-2007, 10:35 AM
  5. Replies: 4
    Last Post: 12-16-2005, 06:44 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •