Reverse Array, Let Elements in New Array Equal Length of Original Array Elements - JavaScriptLength of a JavaScript objectHow do I check if an array includes an object in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?Deleting an element from an array in PHPHow to insert an item into an array at a specific index (JavaScript)?How do I empty an array in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?How can I add new array elements at the beginning of an array in Javascript?For-each over an array in JavaScript?
Does a windmilling propeller create more drag than a stopped propeller in an engine out scenario?
How is dynamic resistance of a diode modeled for large voltage variations?
What was the primary motivation for a historical figure like Xenophon to create an extensive collection of written material?
Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?
How to tease a romance without a cat and mouse chase?
What causes a person to remain in this world as a ghost?
How do you cope with rejection?
Barron states that 4.18×10⁸ joules equal 1 kcal, is this correct?
US F1 Visa grace period attending a conference
Gambler's Fallacy Dice
Eigenvalues of the Laplace-Beltrami operator on a compact Riemannnian manifold
Was Tyrion always a poor strategist?
Does the fact that we can only measure the two-way speed of light undermine the axiom of invariance?
List of lists elementwise greater/smaller than
What should I wear to go and sign an employment contract?
Is presenting a play showing Military characters in a bad light a crime in the US?
Removing Doubles Destroy Topology
Is there a word for pant sleeves?
Do seaplanes need to get clearance for takeoff?
How to draw with Tikz a chord parallel to AC that passes through a point?
3D Histogram / bar chart
Way of refund if scammed?
RAID 5/6 rebuild time calculation
Difference in 1 user doing 1000 iterations and 1000 users doing 1 iteration in Load testing
Reverse Array, Let Elements in New Array Equal Length of Original Array Elements - JavaScript
Length of a JavaScript objectHow do I check if an array includes an object in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?Deleting an element from an array in PHPHow to insert an item into an array at a specific index (JavaScript)?How do I empty an array in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?How can I add new array elements at the beginning of an array in Javascript?For-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm ultimately trying to solve this kata on Codewars.
I've been able to reverse the array into a string, but I don't know how to assign this string into individual elements of a specified length. I tried:
function ultimateReverse (array)
let newArray = array.join("").split("");
let reversedArray = newArray.reverse();
return reversedArray.join("");
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
//!eiltonnacIdnasttubgibekilI
But the outcome that we want is:
["!", "eilt", "onn", "acIdn", "ast", "t", "ubgibe", "kilI"]
So according to the original array, the length of the first element should be 1, the length of the second element should be 4, the third should be length 3, and so on.
Is there a way to split a string into array of elements, each of a specified length?
I thought of creating an array of the lengths of the items from the original array by doing:
function ultimateReverse (array)
let elementLengths = [];
let newArray = array.join("").split("");
let reversedArray = newArray.reverse().join("");
for (let i = 0; i < array.length; i++)
let element = array[i];
elementLengths.push(element.length);
return reversedArray + " " + elementLengths;
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
Now if I can just split the string into elements in an array based on the length of the original elements...
javascript arrays reverse string-length
add a comment |
I'm ultimately trying to solve this kata on Codewars.
I've been able to reverse the array into a string, but I don't know how to assign this string into individual elements of a specified length. I tried:
function ultimateReverse (array)
let newArray = array.join("").split("");
let reversedArray = newArray.reverse();
return reversedArray.join("");
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
//!eiltonnacIdnasttubgibekilI
But the outcome that we want is:
["!", "eilt", "onn", "acIdn", "ast", "t", "ubgibe", "kilI"]
So according to the original array, the length of the first element should be 1, the length of the second element should be 4, the third should be length 3, and so on.
Is there a way to split a string into array of elements, each of a specified length?
I thought of creating an array of the lengths of the items from the original array by doing:
function ultimateReverse (array)
let elementLengths = [];
let newArray = array.join("").split("");
let reversedArray = newArray.reverse().join("");
for (let i = 0; i < array.length; i++)
let element = array[i];
elementLengths.push(element.length);
return reversedArray + " " + elementLengths;
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
Now if I can just split the string into elements in an array based on the length of the original elements...
javascript arrays reverse string-length
add a comment |
I'm ultimately trying to solve this kata on Codewars.
I've been able to reverse the array into a string, but I don't know how to assign this string into individual elements of a specified length. I tried:
function ultimateReverse (array)
let newArray = array.join("").split("");
let reversedArray = newArray.reverse();
return reversedArray.join("");
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
//!eiltonnacIdnasttubgibekilI
But the outcome that we want is:
["!", "eilt", "onn", "acIdn", "ast", "t", "ubgibe", "kilI"]
So according to the original array, the length of the first element should be 1, the length of the second element should be 4, the third should be length 3, and so on.
Is there a way to split a string into array of elements, each of a specified length?
I thought of creating an array of the lengths of the items from the original array by doing:
function ultimateReverse (array)
let elementLengths = [];
let newArray = array.join("").split("");
let reversedArray = newArray.reverse().join("");
for (let i = 0; i < array.length; i++)
let element = array[i];
elementLengths.push(element.length);
return reversedArray + " " + elementLengths;
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
Now if I can just split the string into elements in an array based on the length of the original elements...
javascript arrays reverse string-length
I'm ultimately trying to solve this kata on Codewars.
I've been able to reverse the array into a string, but I don't know how to assign this string into individual elements of a specified length. I tried:
function ultimateReverse (array)
let newArray = array.join("").split("");
let reversedArray = newArray.reverse();
return reversedArray.join("");
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
//!eiltonnacIdnasttubgibekilI
But the outcome that we want is:
["!", "eilt", "onn", "acIdn", "ast", "t", "ubgibe", "kilI"]
So according to the original array, the length of the first element should be 1, the length of the second element should be 4, the third should be length 3, and so on.
Is there a way to split a string into array of elements, each of a specified length?
I thought of creating an array of the lengths of the items from the original array by doing:
function ultimateReverse (array)
let elementLengths = [];
let newArray = array.join("").split("");
let reversedArray = newArray.reverse().join("");
for (let i = 0; i < array.length; i++)
let element = array[i];
elementLengths.push(element.length);
return reversedArray + " " + elementLengths;
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
Now if I can just split the string into elements in an array based on the length of the original elements...
javascript arrays reverse string-length
javascript arrays reverse string-length
asked 3 hours ago
HappyHands31HappyHands31
1,12432138
1,12432138
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
First construct the full reversed string, eg
!eiltonnacIdnasttubgibekilI
Then, from an array of the initial lengths (which can be done with a .map
in advance), iterate over that array and slice
that length from the reversed string, and push to an array:
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
let reversedStr = [...array.join("")].reverse().join('');
const result = [];
lengths.forEach((length) =>
result.push(reversedStr.slice(0, length));
reversedStr = reversedStr.slice(length);
);
return result;
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
Pushing to a new array on every iteration is usually the place to use .map
, but I don't see a way to do it that elegantly, since you'd have to reference multiple outer variables, and change one of them:
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
const reversedStr = [...array.join("")].reverse().join('');
let startIndex = 0;
let endIndex = 0;
return lengths.map(length =>
startIndex = endIndex;
endIndex = startIndex + length;
return reversedStr.slice(startIndex, startIndex + length);
);
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
Thanks @CertainPerformance - it certainly works. Can you please explain a little more aboutlength
on line 2 - length is an object, in this case?
– HappyHands31
2 hours ago
2
That's destructuring.propName
inside a parameter list extracts the property namedpropName
from the item at that position in the parameter list, and puts it into a variable namedpropName
. The.length
of a string is an integer.
– CertainPerformance
2 hours ago
Please explain the reason why the parens wrappinglength
are needed. Otherwiseerror: unknown: Unexpected token, expected ","
– sandthorn
2 hours ago
@sandthorn I think it's because the parser seeslength
without parentheses as an object, and then chokes up when it sees=>
following the object. Not entirely sure, I'm going to look into it. If I don't see a good answer to it, I'll probably write up a Q+A myself once I figure out what's going on
– CertainPerformance
2 hours ago
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%2f56204534%2freverse-array-let-elements-in-new-array-equal-length-of-original-array-elements%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
First construct the full reversed string, eg
!eiltonnacIdnasttubgibekilI
Then, from an array of the initial lengths (which can be done with a .map
in advance), iterate over that array and slice
that length from the reversed string, and push to an array:
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
let reversedStr = [...array.join("")].reverse().join('');
const result = [];
lengths.forEach((length) =>
result.push(reversedStr.slice(0, length));
reversedStr = reversedStr.slice(length);
);
return result;
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
Pushing to a new array on every iteration is usually the place to use .map
, but I don't see a way to do it that elegantly, since you'd have to reference multiple outer variables, and change one of them:
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
const reversedStr = [...array.join("")].reverse().join('');
let startIndex = 0;
let endIndex = 0;
return lengths.map(length =>
startIndex = endIndex;
endIndex = startIndex + length;
return reversedStr.slice(startIndex, startIndex + length);
);
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
Thanks @CertainPerformance - it certainly works. Can you please explain a little more aboutlength
on line 2 - length is an object, in this case?
– HappyHands31
2 hours ago
2
That's destructuring.propName
inside a parameter list extracts the property namedpropName
from the item at that position in the parameter list, and puts it into a variable namedpropName
. The.length
of a string is an integer.
– CertainPerformance
2 hours ago
Please explain the reason why the parens wrappinglength
are needed. Otherwiseerror: unknown: Unexpected token, expected ","
– sandthorn
2 hours ago
@sandthorn I think it's because the parser seeslength
without parentheses as an object, and then chokes up when it sees=>
following the object. Not entirely sure, I'm going to look into it. If I don't see a good answer to it, I'll probably write up a Q+A myself once I figure out what's going on
– CertainPerformance
2 hours ago
add a comment |
First construct the full reversed string, eg
!eiltonnacIdnasttubgibekilI
Then, from an array of the initial lengths (which can be done with a .map
in advance), iterate over that array and slice
that length from the reversed string, and push to an array:
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
let reversedStr = [...array.join("")].reverse().join('');
const result = [];
lengths.forEach((length) =>
result.push(reversedStr.slice(0, length));
reversedStr = reversedStr.slice(length);
);
return result;
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
Pushing to a new array on every iteration is usually the place to use .map
, but I don't see a way to do it that elegantly, since you'd have to reference multiple outer variables, and change one of them:
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
const reversedStr = [...array.join("")].reverse().join('');
let startIndex = 0;
let endIndex = 0;
return lengths.map(length =>
startIndex = endIndex;
endIndex = startIndex + length;
return reversedStr.slice(startIndex, startIndex + length);
);
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
Thanks @CertainPerformance - it certainly works. Can you please explain a little more aboutlength
on line 2 - length is an object, in this case?
– HappyHands31
2 hours ago
2
That's destructuring.propName
inside a parameter list extracts the property namedpropName
from the item at that position in the parameter list, and puts it into a variable namedpropName
. The.length
of a string is an integer.
– CertainPerformance
2 hours ago
Please explain the reason why the parens wrappinglength
are needed. Otherwiseerror: unknown: Unexpected token, expected ","
– sandthorn
2 hours ago
@sandthorn I think it's because the parser seeslength
without parentheses as an object, and then chokes up when it sees=>
following the object. Not entirely sure, I'm going to look into it. If I don't see a good answer to it, I'll probably write up a Q+A myself once I figure out what's going on
– CertainPerformance
2 hours ago
add a comment |
First construct the full reversed string, eg
!eiltonnacIdnasttubgibekilI
Then, from an array of the initial lengths (which can be done with a .map
in advance), iterate over that array and slice
that length from the reversed string, and push to an array:
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
let reversedStr = [...array.join("")].reverse().join('');
const result = [];
lengths.forEach((length) =>
result.push(reversedStr.slice(0, length));
reversedStr = reversedStr.slice(length);
);
return result;
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
Pushing to a new array on every iteration is usually the place to use .map
, but I don't see a way to do it that elegantly, since you'd have to reference multiple outer variables, and change one of them:
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
const reversedStr = [...array.join("")].reverse().join('');
let startIndex = 0;
let endIndex = 0;
return lengths.map(length =>
startIndex = endIndex;
endIndex = startIndex + length;
return reversedStr.slice(startIndex, startIndex + length);
);
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
First construct the full reversed string, eg
!eiltonnacIdnasttubgibekilI
Then, from an array of the initial lengths (which can be done with a .map
in advance), iterate over that array and slice
that length from the reversed string, and push to an array:
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
let reversedStr = [...array.join("")].reverse().join('');
const result = [];
lengths.forEach((length) =>
result.push(reversedStr.slice(0, length));
reversedStr = reversedStr.slice(length);
);
return result;
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
Pushing to a new array on every iteration is usually the place to use .map
, but I don't see a way to do it that elegantly, since you'd have to reference multiple outer variables, and change one of them:
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
const reversedStr = [...array.join("")].reverse().join('');
let startIndex = 0;
let endIndex = 0;
return lengths.map(length =>
startIndex = endIndex;
endIndex = startIndex + length;
return reversedStr.slice(startIndex, startIndex + length);
);
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
let reversedStr = [...array.join("")].reverse().join('');
const result = [];
lengths.forEach((length) =>
result.push(reversedStr.slice(0, length));
reversedStr = reversedStr.slice(length);
);
return result;
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
let reversedStr = [...array.join("")].reverse().join('');
const result = [];
lengths.forEach((length) =>
result.push(reversedStr.slice(0, length));
reversedStr = reversedStr.slice(length);
);
return result;
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
const reversedStr = [...array.join("")].reverse().join('');
let startIndex = 0;
let endIndex = 0;
return lengths.map(length =>
startIndex = endIndex;
endIndex = startIndex + length;
return reversedStr.slice(startIndex, startIndex + length);
);
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
function ultimateReverse(array)
const lengths = array.map(( length ) => length);
const reversedStr = [...array.join("")].reverse().join('');
let startIndex = 0;
let endIndex = 0;
return lengths.map(length =>
startIndex = endIndex;
endIndex = startIndex + length;
return reversedStr.slice(startIndex, startIndex + length);
);
console.log(ultimateReverse(["I", "like", "big", "butts", "and", "I", "cannot", "lie!"]));
edited 3 hours ago
answered 3 hours ago
CertainPerformanceCertainPerformance
107k166999
107k166999
Thanks @CertainPerformance - it certainly works. Can you please explain a little more aboutlength
on line 2 - length is an object, in this case?
– HappyHands31
2 hours ago
2
That's destructuring.propName
inside a parameter list extracts the property namedpropName
from the item at that position in the parameter list, and puts it into a variable namedpropName
. The.length
of a string is an integer.
– CertainPerformance
2 hours ago
Please explain the reason why the parens wrappinglength
are needed. Otherwiseerror: unknown: Unexpected token, expected ","
– sandthorn
2 hours ago
@sandthorn I think it's because the parser seeslength
without parentheses as an object, and then chokes up when it sees=>
following the object. Not entirely sure, I'm going to look into it. If I don't see a good answer to it, I'll probably write up a Q+A myself once I figure out what's going on
– CertainPerformance
2 hours ago
add a comment |
Thanks @CertainPerformance - it certainly works. Can you please explain a little more aboutlength
on line 2 - length is an object, in this case?
– HappyHands31
2 hours ago
2
That's destructuring.propName
inside a parameter list extracts the property namedpropName
from the item at that position in the parameter list, and puts it into a variable namedpropName
. The.length
of a string is an integer.
– CertainPerformance
2 hours ago
Please explain the reason why the parens wrappinglength
are needed. Otherwiseerror: unknown: Unexpected token, expected ","
– sandthorn
2 hours ago
@sandthorn I think it's because the parser seeslength
without parentheses as an object, and then chokes up when it sees=>
following the object. Not entirely sure, I'm going to look into it. If I don't see a good answer to it, I'll probably write up a Q+A myself once I figure out what's going on
– CertainPerformance
2 hours ago
Thanks @CertainPerformance - it certainly works. Can you please explain a little more about
length
on line 2 - length is an object, in this case?– HappyHands31
2 hours ago
Thanks @CertainPerformance - it certainly works. Can you please explain a little more about
length
on line 2 - length is an object, in this case?– HappyHands31
2 hours ago
2
2
That's destructuring.
propName
inside a parameter list extracts the property named propName
from the item at that position in the parameter list, and puts it into a variable named propName
. The .length
of a string is an integer.– CertainPerformance
2 hours ago
That's destructuring.
propName
inside a parameter list extracts the property named propName
from the item at that position in the parameter list, and puts it into a variable named propName
. The .length
of a string is an integer.– CertainPerformance
2 hours ago
Please explain the reason why the parens wrapping
length
are needed. Otherwise error: unknown: Unexpected token, expected ","
– sandthorn
2 hours ago
Please explain the reason why the parens wrapping
length
are needed. Otherwise error: unknown: Unexpected token, expected ","
– sandthorn
2 hours ago
@sandthorn I think it's because the parser sees
length
without parentheses as an object, and then chokes up when it sees =>
following the object. Not entirely sure, I'm going to look into it. If I don't see a good answer to it, I'll probably write up a Q+A myself once I figure out what's going on– CertainPerformance
2 hours ago
@sandthorn I think it's because the parser sees
length
without parentheses as an object, and then chokes up when it sees =>
following the object. Not entirely sure, I'm going to look into it. If I don't see a good answer to it, I'll probably write up a Q+A myself once I figure out what's going on– CertainPerformance
2 hours ago
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%2f56204534%2freverse-array-let-elements-in-new-array-equal-length-of-original-array-elements%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