how to interwoven the given string using function
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
add a comment |
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
"ab12".replace(/(?=)/g, (_,index,str) => str.charAt(str.length-index))
– Thomas
Jan 2 at 17:42
add a comment |
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
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
javascript
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
add a comment |
"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
add a comment |
3 Answers
3
active
oldest
votes
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'));
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
add a comment |
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'))
Thanks@Dave it really helpful
– Abhirock
Jan 2 at 17:46
add a comment |
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');
Thanks@rpivovar it really helpful
– Abhirock
Jan 2 at 17:45
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%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
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'));
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
add a comment |
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'));
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
add a comment |
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'));
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'));
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
add a comment |
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
add a comment |
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'))
Thanks@Dave it really helpful
– Abhirock
Jan 2 at 17:46
add a comment |
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'))
Thanks@Dave it really helpful
– Abhirock
Jan 2 at 17:46
add a comment |
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'))
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'))
answered Jan 2 at 17:35


davedave
36.5k13965
36.5k13965
Thanks@Dave it really helpful
– Abhirock
Jan 2 at 17:46
add a comment |
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
add a comment |
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');
Thanks@rpivovar it really helpful
– Abhirock
Jan 2 at 17:45
add a comment |
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');
Thanks@rpivovar it really helpful
– Abhirock
Jan 2 at 17:45
add a comment |
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');
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');
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
add a comment |
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
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%2f54010621%2fhow-to-interwoven-the-given-string-using-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
"ab12".replace(/(?=)/g, (_,index,str) => str.charAt(str.length-index))
– Thomas
Jan 2 at 17:42