What is wrong with the logic of my character changing function?Is there an “exists” function for jQuery?What is the most efficient way to deep clone an object in JavaScript?How to change an element's class with JavaScript?var functionName = function() vs function functionName() What is the !! (not not) operator in JavaScript?What is the JavaScript version of sleep()?What does “use strict” do in JavaScript, and what is the reasoning behind it?What is the difference between call and apply?What is JSONP, and why was it created?Is it possible to apply CSS to half of a character?

Make Gimbap cutter

How to make a composition of functions prettier?

Was self-modifying code possible using BASIC?

Can I use 220 V outlets on a 15 ampere breaker and wire it up as 110 V?

In Pandemic, why take the extra step of eradicating a disease after you've cured it?

Forgot passport for Alaska cruise (Anchorage to Vancouver)

Who is "He that flies" in Lord of the Rings?

one-hot-encoding categorical data gives error

What plausible reason could I give for my FTL drive only working in space

What does "lit." mean in boiling point or melting point specification?

What is the theme of analysis?

Enchiridion, 16: Does a stoic moan, or not?

In The Incredibles 2, why does Screenslaver's name use a pun on something that doesn't exist in the 1950s pastiche?

After an 87 day stay in the USA, can I return for a long weekend? (ESTA)

Why did the World Bank set the global poverty line at $1.90?

What's the best way to quit a job mostly because of money?

Part of my house is inexplicably gone

Why can't we do three-way comparison in C++?

Does a single fopen introduce TOCTOU vulnerability?

Insert a smallest possible positive integer into an array of unique integers

Parsing text written the millitext font

Was planting UN flag on Moon ever discussed?

What exactly "triggers an additional time" in the interaction between Afterlife and Teysa Karlov?

As easy as Three, Two, One... How fast can you go from Five to Four?



What is wrong with the logic of my character changing function?


Is there an “exists” function for jQuery?What is the most efficient way to deep clone an object in JavaScript?How to change an element's class with JavaScript?var functionName = function() vs function functionName() What is the !! (not not) operator in JavaScript?What is the JavaScript version of sleep()?What does “use strict” do in JavaScript, and what is the reasoning behind it?What is the difference between call and apply?What is JSONP, and why was it created?Is it possible to apply CSS to half of a character?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I've tried to create a character changing function for strings, it suppose to change all the "-" to "_", and it only does it for the first character and leaves the rest. If someone could explain it would be grate.



function kebabToSnake(str) 
var idNum = str.length;
for(var i = 0; i <= idNum; i++)
var nStr = str.replace("-", "_");

return nStr;










share|improve this question




























    1















    I've tried to create a character changing function for strings, it suppose to change all the "-" to "_", and it only does it for the first character and leaves the rest. If someone could explain it would be grate.



    function kebabToSnake(str) 
    var idNum = str.length;
    for(var i = 0; i <= idNum; i++)
    var nStr = str.replace("-", "_");

    return nStr;










    share|improve this question
























      1












      1








      1








      I've tried to create a character changing function for strings, it suppose to change all the "-" to "_", and it only does it for the first character and leaves the rest. If someone could explain it would be grate.



      function kebabToSnake(str) 
      var idNum = str.length;
      for(var i = 0; i <= idNum; i++)
      var nStr = str.replace("-", "_");

      return nStr;










      share|improve this question














      I've tried to create a character changing function for strings, it suppose to change all the "-" to "_", and it only does it for the first character and leaves the rest. If someone could explain it would be grate.



      function kebabToSnake(str) 
      var idNum = str.length;
      for(var i = 0; i <= idNum; i++)
      var nStr = str.replace("-", "_");

      return nStr;







      javascript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 at 23:03









      Gergely MarkszGergely Marksz

      83




      83






















          1 Answer
          1






          active

          oldest

          votes


















          3














          var nStr = str.replace("-", "_");


          So, on each iteration, you're replacing the first found - character in the original string, not the string that you've already replaced characters from already. You can either call .replace on just one variable that you reassign:






          function kebabToSnake(str) 
          var idNum = str.length;
          for(var i = 0; i < idNum; i++)
          str = str.replace("-", "_");

          return str;

          console.log(kebabToSnake('ab-cd-ef'));





          (note that you should iterate from 0 to str.length - 1, not from 0 to str.length)



          Or, much, much more elegantly, use a global regular expression:






          function kebabToSnake(str) 
          return str.replace(/-/g, '_');

          console.log(kebabToSnake('ab-cd-ef'));








          share|improve this answer























          • Thank you, I see what I've messed up now.

            – Gergely Marksz
            Mar 24 at 23:14











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329425%2fwhat-is-wrong-with-the-logic-of-my-character-changing-function%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          var nStr = str.replace("-", "_");


          So, on each iteration, you're replacing the first found - character in the original string, not the string that you've already replaced characters from already. You can either call .replace on just one variable that you reassign:






          function kebabToSnake(str) 
          var idNum = str.length;
          for(var i = 0; i < idNum; i++)
          str = str.replace("-", "_");

          return str;

          console.log(kebabToSnake('ab-cd-ef'));





          (note that you should iterate from 0 to str.length - 1, not from 0 to str.length)



          Or, much, much more elegantly, use a global regular expression:






          function kebabToSnake(str) 
          return str.replace(/-/g, '_');

          console.log(kebabToSnake('ab-cd-ef'));








          share|improve this answer























          • Thank you, I see what I've messed up now.

            – Gergely Marksz
            Mar 24 at 23:14















          3














          var nStr = str.replace("-", "_");


          So, on each iteration, you're replacing the first found - character in the original string, not the string that you've already replaced characters from already. You can either call .replace on just one variable that you reassign:






          function kebabToSnake(str) 
          var idNum = str.length;
          for(var i = 0; i < idNum; i++)
          str = str.replace("-", "_");

          return str;

          console.log(kebabToSnake('ab-cd-ef'));





          (note that you should iterate from 0 to str.length - 1, not from 0 to str.length)



          Or, much, much more elegantly, use a global regular expression:






          function kebabToSnake(str) 
          return str.replace(/-/g, '_');

          console.log(kebabToSnake('ab-cd-ef'));








          share|improve this answer























          • Thank you, I see what I've messed up now.

            – Gergely Marksz
            Mar 24 at 23:14













          3












          3








          3







          var nStr = str.replace("-", "_");


          So, on each iteration, you're replacing the first found - character in the original string, not the string that you've already replaced characters from already. You can either call .replace on just one variable that you reassign:






          function kebabToSnake(str) 
          var idNum = str.length;
          for(var i = 0; i < idNum; i++)
          str = str.replace("-", "_");

          return str;

          console.log(kebabToSnake('ab-cd-ef'));





          (note that you should iterate from 0 to str.length - 1, not from 0 to str.length)



          Or, much, much more elegantly, use a global regular expression:






          function kebabToSnake(str) 
          return str.replace(/-/g, '_');

          console.log(kebabToSnake('ab-cd-ef'));








          share|improve this answer













          var nStr = str.replace("-", "_");


          So, on each iteration, you're replacing the first found - character in the original string, not the string that you've already replaced characters from already. You can either call .replace on just one variable that you reassign:






          function kebabToSnake(str) 
          var idNum = str.length;
          for(var i = 0; i < idNum; i++)
          str = str.replace("-", "_");

          return str;

          console.log(kebabToSnake('ab-cd-ef'));





          (note that you should iterate from 0 to str.length - 1, not from 0 to str.length)



          Or, much, much more elegantly, use a global regular expression:






          function kebabToSnake(str) 
          return str.replace(/-/g, '_');

          console.log(kebabToSnake('ab-cd-ef'));








          function kebabToSnake(str) 
          var idNum = str.length;
          for(var i = 0; i < idNum; i++)
          str = str.replace("-", "_");

          return str;

          console.log(kebabToSnake('ab-cd-ef'));





          function kebabToSnake(str) 
          var idNum = str.length;
          for(var i = 0; i < idNum; i++)
          str = str.replace("-", "_");

          return str;

          console.log(kebabToSnake('ab-cd-ef'));





          function kebabToSnake(str) 
          return str.replace(/-/g, '_');

          console.log(kebabToSnake('ab-cd-ef'));





          function kebabToSnake(str) 
          return str.replace(/-/g, '_');

          console.log(kebabToSnake('ab-cd-ef'));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 24 at 23:06









          CertainPerformanceCertainPerformance

          112k1673102




          112k1673102












          • Thank you, I see what I've messed up now.

            – Gergely Marksz
            Mar 24 at 23:14

















          • Thank you, I see what I've messed up now.

            – Gergely Marksz
            Mar 24 at 23:14
















          Thank you, I see what I've messed up now.

          – Gergely Marksz
          Mar 24 at 23:14





          Thank you, I see what I've messed up now.

          – Gergely Marksz
          Mar 24 at 23:14



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329425%2fwhat-is-wrong-with-the-logic-of-my-character-changing-function%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해