how to interwoven the given string using function












0















I am very new to javascript programming and I have one puzzle as mention below.



How function to take in a string and return a string that is itself with the reverse of it interwoven together. For example, "ab12" would be"ab12" and "21ba" interwoven to make: "a2b11b2a"



I trying this way which give error.any help will appreciate.
Thanks



function interwoven(str) {
var newString = "";
for (var i = str.length - 1; i >= 0; i--) {
for (var j = str.length; i <= 0; i++)
newString += str[j+i];
}
return newString;
}
interwoven('ab12');









share|improve this question























  • "ab12".replace(/(?=)/g, (_,index,str) => str.charAt(str.length-index))

    – Thomas
    Jan 2 at 17:42


















0















I am very new to javascript programming and I have one puzzle as mention below.



How function to take in a string and return a string that is itself with the reverse of it interwoven together. For example, "ab12" would be"ab12" and "21ba" interwoven to make: "a2b11b2a"



I trying this way which give error.any help will appreciate.
Thanks



function interwoven(str) {
var newString = "";
for (var i = str.length - 1; i >= 0; i--) {
for (var j = str.length; i <= 0; i++)
newString += str[j+i];
}
return newString;
}
interwoven('ab12');









share|improve this question























  • "ab12".replace(/(?=)/g, (_,index,str) => str.charAt(str.length-index))

    – Thomas
    Jan 2 at 17:42
















0












0








0








I am very new to javascript programming and I have one puzzle as mention below.



How function to take in a string and return a string that is itself with the reverse of it interwoven together. For example, "ab12" would be"ab12" and "21ba" interwoven to make: "a2b11b2a"



I trying this way which give error.any help will appreciate.
Thanks



function interwoven(str) {
var newString = "";
for (var i = str.length - 1; i >= 0; i--) {
for (var j = str.length; i <= 0; i++)
newString += str[j+i];
}
return newString;
}
interwoven('ab12');









share|improve this question














I am very new to javascript programming and I have one puzzle as mention below.



How function to take in a string and return a string that is itself with the reverse of it interwoven together. For example, "ab12" would be"ab12" and "21ba" interwoven to make: "a2b11b2a"



I trying this way which give error.any help will appreciate.
Thanks



function interwoven(str) {
var newString = "";
for (var i = str.length - 1; i >= 0; i--) {
for (var j = str.length; i <= 0; i++)
newString += str[j+i];
}
return newString;
}
interwoven('ab12');






javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 17:26









AbhirockAbhirock

121




121













  • "ab12".replace(/(?=)/g, (_,index,str) => str.charAt(str.length-index))

    – Thomas
    Jan 2 at 17:42





















  • "ab12".replace(/(?=)/g, (_,index,str) => str.charAt(str.length-index))

    – Thomas
    Jan 2 at 17:42



















"ab12".replace(/(?=)/g, (_,index,str) => str.charAt(str.length-index))

– Thomas
Jan 2 at 17:42







"ab12".replace(/(?=)/g, (_,index,str) => str.charAt(str.length-index))

– Thomas
Jan 2 at 17:42














3 Answers
3






active

oldest

votes


















2














You could use two indices i that increments and j that decrements.






function interwoven(str) {
var i = 0, len = str.length, j = len - 1, newString = "";
for (; i < len; i++, j--) {
newString += str[i] + str[j]
}
return newString;
}
console.log(interwoven('ab12'));








share|improve this answer



















  • 1





    Thanks@Yury its awesome. you make my life easy

    – Abhirock
    Jan 2 at 17:45











  • @Abhirock Glad I could help.

    – Yury Tarabanko
    Jan 2 at 17:49











  • sorry to ask one query Is it my question is wrong or inappropriate as its given -1?

    – Abhirock
    Jan 2 at 17:51











  • @Abhirock Who knows :) Mb the downvoter assumes you haven't done your research or whatever.

    – Yury Tarabanko
    Jan 2 at 18:04











  • Ok Thanks I will take care about it:-)

    – Abhirock
    Jan 2 at 18:11



















0














I would just do this:






    function interwoven(str) {
return str.split('').reduce((acc, cur, idx, src) =>
acc + cur + src[src.length - (idx + 1)], '');
}

console.log(interwoven('ab12'))








share|improve this answer
























  • Thanks@Dave it really helpful

    – Abhirock
    Jan 2 at 17:46



















0














    function interwoven(string) {
// store string in array string-array
let string-array = string.split('');
// copy string-array, reverse, and store in string-array-reverse
let string-array-reverse = string-array.reverse();
let interwoven-array = ;
let index;
for (index = 0; index < string-array.length; index++) {
interwoven-array.push(string-array[index]);
interwoven-array.push(string-array-reverse[index]);
}

return interwoven-array;
}

interwoven('ab12');





share|improve this answer


























  • Thanks@rpivovar it really helpful

    – Abhirock
    Jan 2 at 17:45












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%2f54010621%2fhow-to-interwoven-the-given-string-using-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You could use two indices i that increments and j that decrements.






function interwoven(str) {
var i = 0, len = str.length, j = len - 1, newString = "";
for (; i < len; i++, j--) {
newString += str[i] + str[j]
}
return newString;
}
console.log(interwoven('ab12'));








share|improve this answer



















  • 1





    Thanks@Yury its awesome. you make my life easy

    – Abhirock
    Jan 2 at 17:45











  • @Abhirock Glad I could help.

    – Yury Tarabanko
    Jan 2 at 17:49











  • sorry to ask one query Is it my question is wrong or inappropriate as its given -1?

    – Abhirock
    Jan 2 at 17:51











  • @Abhirock Who knows :) Mb the downvoter assumes you haven't done your research or whatever.

    – Yury Tarabanko
    Jan 2 at 18:04











  • Ok Thanks I will take care about it:-)

    – Abhirock
    Jan 2 at 18:11
















2














You could use two indices i that increments and j that decrements.






function interwoven(str) {
var i = 0, len = str.length, j = len - 1, newString = "";
for (; i < len; i++, j--) {
newString += str[i] + str[j]
}
return newString;
}
console.log(interwoven('ab12'));








share|improve this answer



















  • 1





    Thanks@Yury its awesome. you make my life easy

    – Abhirock
    Jan 2 at 17:45











  • @Abhirock Glad I could help.

    – Yury Tarabanko
    Jan 2 at 17:49











  • sorry to ask one query Is it my question is wrong or inappropriate as its given -1?

    – Abhirock
    Jan 2 at 17:51











  • @Abhirock Who knows :) Mb the downvoter assumes you haven't done your research or whatever.

    – Yury Tarabanko
    Jan 2 at 18:04











  • Ok Thanks I will take care about it:-)

    – Abhirock
    Jan 2 at 18:11














2












2








2







You could use two indices i that increments and j that decrements.






function interwoven(str) {
var i = 0, len = str.length, j = len - 1, newString = "";
for (; i < len; i++, j--) {
newString += str[i] + str[j]
}
return newString;
}
console.log(interwoven('ab12'));








share|improve this answer













You could use two indices i that increments and j that decrements.






function interwoven(str) {
var i = 0, len = str.length, j = len - 1, newString = "";
for (; i < len; i++, j--) {
newString += str[i] + str[j]
}
return newString;
}
console.log(interwoven('ab12'));








function interwoven(str) {
var i = 0, len = str.length, j = len - 1, newString = "";
for (; i < len; i++, j--) {
newString += str[i] + str[j]
}
return newString;
}
console.log(interwoven('ab12'));





function interwoven(str) {
var i = 0, len = str.length, j = len - 1, newString = "";
for (; i < len; i++, j--) {
newString += str[i] + str[j]
}
return newString;
}
console.log(interwoven('ab12'));






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 17:30









Yury TarabankoYury Tarabanko

31.1k64668




31.1k64668








  • 1





    Thanks@Yury its awesome. you make my life easy

    – Abhirock
    Jan 2 at 17:45











  • @Abhirock Glad I could help.

    – Yury Tarabanko
    Jan 2 at 17:49











  • sorry to ask one query Is it my question is wrong or inappropriate as its given -1?

    – Abhirock
    Jan 2 at 17:51











  • @Abhirock Who knows :) Mb the downvoter assumes you haven't done your research or whatever.

    – Yury Tarabanko
    Jan 2 at 18:04











  • Ok Thanks I will take care about it:-)

    – Abhirock
    Jan 2 at 18:11














  • 1





    Thanks@Yury its awesome. you make my life easy

    – Abhirock
    Jan 2 at 17:45











  • @Abhirock Glad I could help.

    – Yury Tarabanko
    Jan 2 at 17:49











  • sorry to ask one query Is it my question is wrong or inappropriate as its given -1?

    – Abhirock
    Jan 2 at 17:51











  • @Abhirock Who knows :) Mb the downvoter assumes you haven't done your research or whatever.

    – Yury Tarabanko
    Jan 2 at 18:04











  • Ok Thanks I will take care about it:-)

    – Abhirock
    Jan 2 at 18:11








1




1





Thanks@Yury its awesome. you make my life easy

– Abhirock
Jan 2 at 17:45





Thanks@Yury its awesome. you make my life easy

– Abhirock
Jan 2 at 17:45













@Abhirock Glad I could help.

– Yury Tarabanko
Jan 2 at 17:49





@Abhirock Glad I could help.

– Yury Tarabanko
Jan 2 at 17:49













sorry to ask one query Is it my question is wrong or inappropriate as its given -1?

– Abhirock
Jan 2 at 17:51





sorry to ask one query Is it my question is wrong or inappropriate as its given -1?

– Abhirock
Jan 2 at 17:51













@Abhirock Who knows :) Mb the downvoter assumes you haven't done your research or whatever.

– Yury Tarabanko
Jan 2 at 18:04





@Abhirock Who knows :) Mb the downvoter assumes you haven't done your research or whatever.

– Yury Tarabanko
Jan 2 at 18:04













Ok Thanks I will take care about it:-)

– Abhirock
Jan 2 at 18:11





Ok Thanks I will take care about it:-)

– Abhirock
Jan 2 at 18:11













0














I would just do this:






    function interwoven(str) {
return str.split('').reduce((acc, cur, idx, src) =>
acc + cur + src[src.length - (idx + 1)], '');
}

console.log(interwoven('ab12'))








share|improve this answer
























  • Thanks@Dave it really helpful

    – Abhirock
    Jan 2 at 17:46
















0














I would just do this:






    function interwoven(str) {
return str.split('').reduce((acc, cur, idx, src) =>
acc + cur + src[src.length - (idx + 1)], '');
}

console.log(interwoven('ab12'))








share|improve this answer
























  • Thanks@Dave it really helpful

    – Abhirock
    Jan 2 at 17:46














0












0








0







I would just do this:






    function interwoven(str) {
return str.split('').reduce((acc, cur, idx, src) =>
acc + cur + src[src.length - (idx + 1)], '');
}

console.log(interwoven('ab12'))








share|improve this answer













I would just do this:






    function interwoven(str) {
return str.split('').reduce((acc, cur, idx, src) =>
acc + cur + src[src.length - (idx + 1)], '');
}

console.log(interwoven('ab12'))








    function interwoven(str) {
return str.split('').reduce((acc, cur, idx, src) =>
acc + cur + src[src.length - (idx + 1)], '');
}

console.log(interwoven('ab12'))





    function interwoven(str) {
return str.split('').reduce((acc, cur, idx, src) =>
acc + cur + src[src.length - (idx + 1)], '');
}

console.log(interwoven('ab12'))






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 17:35









davedave

36.5k13965




36.5k13965













  • Thanks@Dave it really helpful

    – Abhirock
    Jan 2 at 17:46



















  • Thanks@Dave it really helpful

    – Abhirock
    Jan 2 at 17:46

















Thanks@Dave it really helpful

– Abhirock
Jan 2 at 17:46





Thanks@Dave it really helpful

– Abhirock
Jan 2 at 17:46











0














    function interwoven(string) {
// store string in array string-array
let string-array = string.split('');
// copy string-array, reverse, and store in string-array-reverse
let string-array-reverse = string-array.reverse();
let interwoven-array = ;
let index;
for (index = 0; index < string-array.length; index++) {
interwoven-array.push(string-array[index]);
interwoven-array.push(string-array-reverse[index]);
}

return interwoven-array;
}

interwoven('ab12');





share|improve this answer


























  • Thanks@rpivovar it really helpful

    – Abhirock
    Jan 2 at 17:45
















0














    function interwoven(string) {
// store string in array string-array
let string-array = string.split('');
// copy string-array, reverse, and store in string-array-reverse
let string-array-reverse = string-array.reverse();
let interwoven-array = ;
let index;
for (index = 0; index < string-array.length; index++) {
interwoven-array.push(string-array[index]);
interwoven-array.push(string-array-reverse[index]);
}

return interwoven-array;
}

interwoven('ab12');





share|improve this answer


























  • Thanks@rpivovar it really helpful

    – Abhirock
    Jan 2 at 17:45














0












0








0







    function interwoven(string) {
// store string in array string-array
let string-array = string.split('');
// copy string-array, reverse, and store in string-array-reverse
let string-array-reverse = string-array.reverse();
let interwoven-array = ;
let index;
for (index = 0; index < string-array.length; index++) {
interwoven-array.push(string-array[index]);
interwoven-array.push(string-array-reverse[index]);
}

return interwoven-array;
}

interwoven('ab12');





share|improve this answer















    function interwoven(string) {
// store string in array string-array
let string-array = string.split('');
// copy string-array, reverse, and store in string-array-reverse
let string-array-reverse = string-array.reverse();
let interwoven-array = ;
let index;
for (index = 0; index < string-array.length; index++) {
interwoven-array.push(string-array[index]);
interwoven-array.push(string-array-reverse[index]);
}

return interwoven-array;
}

interwoven('ab12');






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 17:45

























answered Jan 2 at 17:39









rpivovarrpivovar

81311031




81311031













  • Thanks@rpivovar it really helpful

    – Abhirock
    Jan 2 at 17:45



















  • Thanks@rpivovar it really helpful

    – Abhirock
    Jan 2 at 17:45

















Thanks@rpivovar it really helpful

– Abhirock
Jan 2 at 17:45





Thanks@rpivovar it really helpful

– Abhirock
Jan 2 at 17:45


















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%2f54010621%2fhow-to-interwoven-the-given-string-using-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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory