6 kyu
Array Helpers
21,650 of 23,859BattleRattle
Loading description...
Arrays
Fundamentals
Object-oriented Programming
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.
Returning 0 but expected NaN in C#. There is no NaN for integers, only for floats. If you expect a float to be returned, then why are the results being truncated anyways?
The C# translation did not take into account the difference between C#'s '/' and JS's '/', which I have fixed and now waiting to approve. @dfhwze
ok
This comment has been hidden.
which one, I can't find it
In RandomData ShouldAverageCorrectly
they were all wrong, should be fixed
Why, when using toFixed in Vs Code, do I get 3 when asking from the average test, but in the cw compiler I get 4?
I appreciated the kata; it was well-constructed. However, I found the phrasing a bit ambiguous. As an enthusiast of OOP, when I encounter the word 'extend', especially in contexts like "extend the built-in Array class with the following methods...", my mind jumps to the concept of subclassing and inheritance. Perhaps a different term could be used to more clearly convey the intention
Then maybe it's a good opportunity to free the mind and get it out of the box of "the only right interpretation"? :D For example in C#, which is as OOP as it gets, it's extension methods which can be used to extend types specifically without inheritance.
I fully agree with you. I thought the same, when i was resolving this task. And i think the most part of programmers thought the same.
Every function except square was working. I was about to lose my mind... I spelled
prototype
asprotoype
. No wonder why it wasn't working hahatypos is a thing everyone can do. They boil your brain, haha.
The only thing standing between me and that 200k job is being able to spell 'function' correctly every time 😂
Really great kata, looking forward to more kata's like this one!
a wonderful kata (daddy is back)
Hey! Great kata!
I'm stuck on a
should even() and odd() correctly
sample tests are passing and I tested a various combinations with
.even().odd()
, and it seem to work properly, butthis particular test is failing... and I'm just wandering what could possibly went wrong?
Could you share a test code for case
should even() and odd() correctly
?Your code is returning the elements at
even
andodd
indices respectively whilst the task asks to returneven
andodd
numbers respectively.whoopsy :D my stupid error.
thanks!
So why don't arrow functions work for this?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Делайте через prototype и this
Хоть так делать в реальности и не желательно, странная kata
Спасибо! Благодаря вашей подсказке решил задание. Хотя, наверное, как и большинство, сначала делал через extends.
This comment has been hidden.
Если кто-то испытывает трудности, Ваш ответ в реализации этих 6 методов - использование контекста вызова. Удачи!
[ 52, 39, 23, 67, 51, 16, 85, 40, 69, 50, 63, 62, 12, 40, 51, 36, 12, 5, 100, 55 ] average() returnd wrong result: expected 50 to equal 46.4
How is the number obtained 46.4
by..averaging the numbers?
yes
I've been confused too... But there is no bug, you simply don'y need to round at all !:)
This comment has been hidden.
Your
this
is wrong with arrow function, use regular one instead.Cool one!
Tell me please, how to solve this kata without prototypes? Using only classes.
hi how do I go about doing this? I kept getting error, numbers.square is not a function...
You have to extend
Array.prototype
and useQuestion
notIssue
if you have a question.TypeError: numbers.square is not a function at /home/codewarrior/index.js:83:28 at /home/codewarrior/index.js:89:5 at Object.handleError
Using Javascript. I've extended the 'numbers' array with the desired methods (square, cube, sum, average, even, odd). They run fine in my local environment and produce the expected outputs. But when I run the sample tests/attempt through the codewars platform I recieve the above error message. I'm a novice developer and don't quite understand what's happening behind the scenes but figured I'd submit the issue here for the admin to take a look at.
And how did you extend it? We can't guess it. Post your code and mark your post as having spoiler content.
This comment has been hidden.
Doing it like that, you only add those properties to that particular array, you have to work with the class instead.
Especially when extending
Array
, do NOT add enumerable properties to the prototype. ( OK,Object
is worse still. ) This will break every use offor .. in
.This should be specified, tested for, and people who make
[]
have properties should be failed with a clear message.This is giving people a loaded gun and telling them how to pull the trigger, preferably while looking into the barrel to make sure the bullet comes out.
That last sentence LOL.
This comment has been hidden.
This comment has been hidden.
nerf ruby
Is. this. asking. me. to. mess. with. the. prototype??? Oh geez, man...eeiih.. i'm nervous.
Why is this question so easy compared to the rest at 6 kyu? Glad it is though for a break!
Very good excercise! I had problem with running the tests, because I did not know how to create function to run - but I forgot that we have to just make new methods of Array :)
enjoyable Kata.
Ruby:
[2, 3]
--> expected avg:2
)These could all be easily fixed, but would invalidate _all_ existing solutions (1300+)
Great kata! I've learned a valuable skill to add array methods. Thanks :)
Needs actual example test cases.
Added sample test to Ruby (JS already had some)
Hi, @BattleRattle! Thanks for the nice kata! I would add some samples with empty array in test cases.
updated description to say that empty array is not tested
This comment has been hidden.
You must use array prototyping to add these methods to the array class. The "numbers" array will change every time and sometimes won't even be called "numbers". That means you want to add a method to the array class, not the variable.
Is the test case broken? I created a TDD for each method and have passed all tests, but the "attempt" button will not change to submit.
Currently Codewars platform is experiencing issues.
It would be nice to add a little clarity in instruction: "even() must return an array of all even numbers (by value, not by index)".
Thank you! You saved me a lot of time!
Passed all the test cases. But when I submit I get: TypeError: Reduce of empty array with no initial value at Array.reduce at Array.sum at Array.average at Test.it at array at Test.describe
The code has passed through eslint with no problem. I'm not sure what they're getting at with this
Maybe if you post your code, we can provide some more helpful comments. As it stands, I'm not sure what you mean.
If you have [].reduce(fn) instead of [].reduce(fn,0) with empty arr you will get that error.
This comment has been hidden.
I found this kata a relatively simple way to become familiar with writing prototype methods.
I agree. I am pleased that this kata came up!
Going kind of crazy - I've defined all the methods and written the test cases which all pass but upon submission I get the error:
-e:83: syntax error, unexpected end-of-input, expecting keyword_end
This suggests to me that the authors test case code is missing an end somewhere or my code/test cases has thrown it out somehow. How do I find out whats wrong when I can't see the authors test case?
I'm not sure I can solve that problem, but as for seeing the author's test case, Javascript allows you to use console.log() to take a peek at pretty much anything in the data. Its output shows in the window that displays the results of the tests. I have found it very useful for debugging.
This comment has been hidden.
I'm wondering the same thing.
I can't reply to your comment for some reason -- hopefully you see this. After some more experience with Ruby, it seems to be the case that calling a Class function within a Class function doesn't require any reference to self. It seems like it's smart enough to know that from the fact the instance function is in its class means that any usage of the function refers to an instance of its class. That's just speculation though, maybe someone more familiar with nitty gritty Ruby details can give a proper explanation.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Hi. I've stucked with the similar problem: "Numbers is not defined". And I can't find out what I am missing cause "numbers" are declared at the beginning of the test process as I see. can someone give me the right direction to solve this problem?
Haha I like the array problems. Helps me get better at utilizing arrays and prototypes. I went one step further and made a "power" function to simplify the cube and even.
The tests for the
average
method are wrong; they don't handle results that aren't integers. I had to make my solution incorrect to make it pass. For the array[1, 2]
, the average is 1.5, but the tests are expecting 1 because they're throwing away the non-integer part of the number. This is a bug that needs to be fixed (that isn't an actual array from the tests, but they have something similar in which the correct answer isn't an integer). Of course, fixing the kata will immediately invalidate everybody's solutions. Fun!hacked the random tests so that the average will always be an integer
must you not say that the arrays are arrays of integers?
Please add even/odd test cases to make sure that those functions handle non-integers correctly.
2.5 is neither even nor odd. So you should expect this:
[2.5].even() == [] [2.5].odd() == []
This comment has been hidden.
it is a bad practice indeed, and has been raised as an issue
This comment has been hidden.
A bit too many functions, but otherwise very good.
Very nice Kata, simple yet very informative for working with prototypes. Although, working with global objects makes me kinda sad.
Test Failed: Wrong average on empty array, expected NaN -.-
I think is better 0 than NaN
Why should that be better? If you have no values, you can't build an average, so you need to return a special value, since 0 is a valid result for values.
only if sum also returns NaN, because I think it's the same case
It's another case: A sum is initialized with 0 and then all elements are added to the sum. The average, on the other hand, is defined by the sum of all elements divided by the number of all elements, which would mean 0 (the sum) divided by 0 (the number of elements). And division by 0 means: NaN.
This comment has been hidden.
This comment has been hidden.
I can pass the unit tests in my window but not on the Attempt button.....
it's for empty array, test []
Nice, this is exactly the sort of katas I like
Wow, I had no idea some of these functions existed for working with plain JS Arrays!
Nice Kata! :)
This is nice, but I think having both the square() and cube() methods and both the even() and odd() methods is overkill; the point is made with either of each pair.
On the other hand, I like that it is thorough.
I also found it a little bit boring to have to do so many similar functions. Would have been better to skip for example the square() and the even() methods.
I don't mind having to essentially re-type the similar functions. For me it just helps re-enforce the concept and help commit important methods (and how to use them) to memory.
Well one could write for example a power function with an input for the power of 2 for square and 3 for cube. Same with modulo and even/odd. Therefore I am okay with similar functions.