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;
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
add a comment |
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
add a comment |
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
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
javascript
asked Mar 24 at 23:03
Gergely MarkszGergely Marksz
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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'));
Thank you, I see what I've messed up now.
– Gergely Marksz
Mar 24 at 23:14
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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'));
Thank you, I see what I've messed up now.
– Gergely Marksz
Mar 24 at 23:14
add a comment |
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'));
Thank you, I see what I've messed up now.
– Gergely Marksz
Mar 24 at 23:14
add a comment |
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'));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'));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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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