7 kyu
Array.prototype.reverse()
2,671 of 2,690wthit56
Loading description...
Arrays
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Interesting kata.
The tests of this kata don't take into account empty array elements. E.g.
let arr = [0,1,2,,,5]; // [0, 1, 2, empty, empty, 5] arr.reverse(); // [5, empty, empty, 2, 1, 0]
With a simple exchange of values, we will get the value undefined instead of empty elements:
arr.reverse(); // [5, undefined, undefined, 2, 1, 0]
Please cure the tests, "assertSimilar is deprecated, use assertDeepEquals"
Fixed.
This comment has been hidden.
That would be easier, wouldn't it?
I'm not sure it makes it more interesting myself...
CoffeeScript translation Kumited! Please accept :D
Well done! Accepted.
This comment has been hidden.
The reversing is meant to be done in-place:
reverses the order of the items in the array it is called on, and then returns that same array
. So the original array is reversed, then the original array is returned. It is a difficult thing to describe, so I've had this problem a lot. I'll try to reword.Even if it is working correctly and i return the same array (this) the fourth test it logs "Original array should be returned." Can someone give me a hint?
The fourth test, onward are the randomly-generated tests. Could you past your code in a reply (within a code block, preferably), so I can try and figure out what's going wrong?
This comment has been hidden.
I noticed that in my solution I can log the variable "Test" and get a number (2) back if reverse method was called. However for the fourth test it logs the number BUT wants the original array. Can someone please help me see what I'm missing here? I don't understand why a reverse method would be called just to not be reversed.
I'm not really sure what you mean in your description, @piece. The
.reverse()
method should reverse the items in that same array, and return that same array. It's all done in place, and thenthis
is returned, to allow method chaining (eg.[1, 2, 3].reverse().push(4)
).This comment has been hidden.
Unfortunately you cannot guarantee reversal with a sorting algorithm. Sorting works by testing two values; if the returned number is > 0, it will swap the values, if <= 0 it will leave them as-is (a generalisation, but you get the idea). The only case where your code would work is if the first and last values are tested (and swapped), then the second and second-to-last values are tested (and swapped), and so on. This tends not to be the case, however. It will test each value a number of times, against different values until it finds the right place for it.
I had originally tried a similar solution to yours, but found it severely lacking, unfortunately. (And I'm the author! ;P)
The reason it seems as though it's the "last" test that fails is, because of how the tests are written, when any test fails the testing will stop. So any failing test will look like the last test. There are 3 basic tests, and then 10 randomly-generated ones; perhaps those are giving you problems?
This is a good kata!
Well thank you for saying so! ;P
This comment has been hidden.
This comment has been hidden.
Wow! Very smart use of hoisting there. I've added some code to see if this hack has been used and, if so, tells you off about it. ;P
Thanks for your help, man. Much appreciated.
"Hoisting", eh? Figures, they named it and nobody bothered to tell me. ;)
I'm not sure if I'm mistaken, but you're saying original array should be returned when called without reverse, yet you expect to be reversed in place. The protoype I wrote does not modify in place, if you want the original array.. Either something is wrong or I'm reading it the wrong way.
I'm not sure what you mean by "when called without reverse". As in the example, the returned array (the original array) is reversed. The reversal should be done in-place, modifying the original array and returning it.
Well, then I misinterpreted the meaning of 'original'. For me, when I read 'original', it means the exact same array (same information, same order etc), so not in-place modification. I understand now, thanks.
Ah, I see. Sorry about that. "In place" usually means "by modifying the original object". And "original object" usually means "the object containing the data"; that data or object could be modified, but it would be modified on the same object, rather than making a new one and adding all the data there.
Thanks for your patience.
Can you put some sample test cases?
Added. ^^
I am getting the following error: Max Buffer reached: Too much information has been written to stdout.
This happens even when I'm not using console.log to add anything to the output. So my solution won't validate.
Looks like the site runners have added that feature. I would expect a whole ton of katas to have the same problem, and hopefully they'll fix it sooner rather than later.
I've posted a message to the bug reporting forum; hopefully they'll see it soon. Thanks for bringing it to my attention.
Wow! Our limit is 400,000 characters. Thats... a lot of characters :) Perhaps we can tone down the random test cases a bit. I don't think anyone is going to hardcode that many answers.
Oh, wow. Where did you find that out?
I've had a look, and I'm only generating 10 tests; not that many. It's because of Test.assertSimilar logging the values (I'm generating floats using
Math.random()
, which makes > 10 character numbers). Would suck to have to write my own assertSimilar function just to get round this. Strangely, when editing the kata, the stdout restriction doesn't come up.Yeah thats strange. Its now working for me also. I think Math.random is partially based off of the current time, so perhaps larger numbers are given at a certain time of day?
random
doesnt always return a number with the same number of decimals. Maybe we can reduce/truncate the decimal size so that the results are always the same decimal length.It's working for me now too. Perhaps we should try it again tomorrow to see if it recurs.
FYI, I did take out a superfluous assertSimilar, so there will be a half the logging of values going on; perhaps that's why it's working for you now? I did try to test using this change, but it probably hadn't gone through yet, so I still got the same problem.
Is this issue now resolved?
I think so. I haven't seen the problem again so I unflagged it.
Cool. Thanks.
This comment has been hidden.
I've tweaked it a little; is it clearer for you now?
Did you mark this kata as having issues? Are they resolved now, or can I help you with something?
Sorry, I was away, I marked it as ready
Thanks
You might want to test odd-length arrays, should someone come up with an algorithm that would choke on them. I don't know, maybe it's not such a big issue.
Good idea. Added.
Do you want to mark this suggestion as resolved or whatever?
Sorry, didn't know you could also resolve suggestions.
No worries ;P
IT is not clear that we should return this and not a copy, wich would be the more intuitive reasoning.
How about now? I've added a brief explanation. As well as the example code.
I don't know if it is very intuitive for beginners to use 'this'.
maybe you can add "return this" at the end of the solution setup.
I feel like it's a pretty fundamental concept for someone learning JavaScript. And this isn't really aimed at pure beginners with no experience.
Did you mark this kata as having issues? Are they resolved now, or can I help you with something?
I did not.
Okay, no worries.
I think you need to clarify if the returned object is the this object or a copy of it.
I don't mention any copying of the array. The method would be called on the array itself, so that's the only array being talked about. Could you show how someone could reason that it means a copy of the array?
My reading of this is that surtich believes it unclear whether the method should mutate the array.
It seems to be your intent that it does because you say it should reverse it 'in place' and also require us to reimplement the original method, which is destructive.
I agree with @surtich, and you also need to test to enforce it's the original array returned.
Okay; I've given an example instead of the explanation. Not sure if it makes it easier to understand or not, though?
It's more clear to me this way.
Cool. Glad it makes more sense...
@OverZealous - I've now added checks as suggested. Thanks for your help!
Is this issue resolved now, @surtich?
To me is OK now. Thanks.
Awesome. Could you mark the issue as resolved for me then, please? ^^
It would really help me out if you could mark this issue as "resolved". The kata will be stuck in Beta until all issues are resolved.
Hello,
Ok done. Sorry, I thought it was enough to make the kata as ready.
No problem. Thanks, surtich ^^