Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. JavaScript
  4. Changing the last text of a loop

Changing the last text of a loop

Scheduled Pinned Locked Moved JavaScript
questionjavascript
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11816233
    wrote on last edited by
    #1

    So right now my countdown loop goes "5,4,3,2,1," Is there a way to add something so that when it goes to 1 there is no more comma? Thank you! I am taking my first Javascript class so I apologize if my question is very basic.

    L Z A 3 Replies Last reply
    0
    • U User 11816233

      So right now my countdown loop goes "5,4,3,2,1," Is there a way to add something so that when it goes to 1 there is no more comma? Thank you! I am taking my first Javascript class so I apologize if my question is very basic.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Yes you can use loops if-else tests etc. See http://www.w3schools.com/js/default.asp[^], where you can test online.

      1 Reply Last reply
      0
      • U User 11816233

        So right now my countdown loop goes "5,4,3,2,1," Is there a way to add something so that when it goes to 1 there is no more comma? Thank you! I am taking my first Javascript class so I apologize if my question is very basic.

        Z Offline
        Z Offline
        ZurdoDev
        wrote on last edited by
        #3

        You would have to show us the code you are currently using to do this. But my guess is you are just adding "," to some variable that is being decremented. So, just check if your variable is 1 first and if so, don't add the ","

        There are only 10 types of people in the world, those who understand binary and those who don't.

        1 Reply Last reply
        0
        • U User 11816233

          So right now my countdown loop goes "5,4,3,2,1," Is there a way to add something so that when it goes to 1 there is no more comma? Thank you! I am taking my first Javascript class so I apologize if my question is very basic.

          A Offline
          A Offline
          Anil Vaghasiya
          wrote on last edited by
          #4

          Hello, Just Check in Console it Works

          var str,i,counter="";
          for (i = 5 ; i >= 1; i--) {
          str =i+',';
          console.log(str);
          counter+=str;
          }
          console.log(counter.replace(/.$/,""));
          //console.log(counter.substring(0,counter.length-1));

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups