6 kyu

Array Helpers

21,650 of 23,859BattleRattle
Description
Loading description...
Arrays
Fundamentals
Object-oriented Programming
  • Please sign in or sign up to leave a comment.
  • Grovre Avatar

    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?

  • htaust01 Avatar

    This comment has been hidden.

  • MrAlabama Avatar

    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?

  • RamadanCRaji Avatar

    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

  • ameenj Avatar

    Every function except square was working. I was about to lose my mind... I spelled prototype as protoype. No wonder why it wasn't working haha

  • ManuGC Avatar

    Really great kata, looking forward to more kata's like this one!

  • user4896126 Avatar

    a wonderful kata (daddy is back)

  • mo.mo. Avatar

    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, but

    this 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?

  • Eddie_Pulido Avatar

    So why don't arrow functions work for this?

  • anticher Avatar

    Делайте через prototype и this

  • andrew kimani Avatar

    This comment has been hidden.

  • Ilya758 Avatar

    Если кто-то испытывает трудности, Ваш ответ в реализации этих 6 методов - использование контекста вызова. Удачи!

  • EvgeniiLapotko Avatar

    [ 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

  • luiszaval Avatar

    This comment has been hidden.

  • zLuki Avatar

    Cool one!

  • reagentjs Avatar

    Tell me please, how to solve this kata without prototypes? Using only classes.

  • ykIsCoding Avatar

    hi how do I go about doing this? I kept getting error, numbers.square is not a function...

  • levels Avatar

    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.

  • JohanWiltink Avatar

    This kata is designed to test your ability to extend the functionality of built-in classes.

    Especially when extending Array, do NOT add enumerable properties to the prototype. ( OK, Object is worse still. ) This will break every use of for .. 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.

  • AlbertLorenzo Avatar

    This comment has been hidden.

  • arssonist Avatar

    This comment has been hidden.

  • Archaeologist03 Avatar

    Is. this. asking. me. to. mess. with. the. prototype??? Oh geez, man...eeiih.. i'm nervous.

  • rrogerthat Avatar

    Why is this question so easy compared to the rest at 6 kyu? Glad it is though for a break!

  • JaryWu Avatar

    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 :)

  • DealPete Avatar

    enjoyable Kata.

  • anter69 Avatar

    Ruby:

    • empty array is never tested
    • expected average is wrong (e.g. [2, 3] --> expected avg: 2)

    These could all be easily fixed, but would invalidate _all_ existing solutions (1300+)

  • goldsounds Avatar

    Great kata! I've learned a valuable skill to add array methods. Thanks :)

  • Unihedron Avatar

    Needs actual example test cases.

  • bestwebua Avatar

    Hi, @BattleRattle! Thanks for the nice kata! I would add some samples with empty array in test cases.

  • JessedeGit Avatar

    This comment has been hidden.

  • TmNguyen12 Avatar

    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.

  • mvgon4 Avatar

    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)".

  • nsieber Avatar

    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

  • HerrWert Avatar

    I found this kata a relatively simple way to become familiar with writing prototype methods.

  • acsauk Avatar

    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?

  • viparthasarathy Avatar

    This comment has been hidden.

  • Jwing28 Avatar

    This comment has been hidden.

  • patrickwalkowicz Avatar

    This comment has been hidden.

  • CookieRat Avatar

    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.

  • craecke Avatar

    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!

  • g964 Avatar

    must you not say that the arrays are arrays of integers?

  • computerguy103 Avatar

    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() == []

  • orenmizr Avatar

    This comment has been hidden.

  • lpommers Avatar

    This comment has been hidden.

  • lollek Avatar

    A bit too many functions, but otherwise very good.

  • user3765829 Avatar

    Very nice Kata, simple yet very informative for working with prototypes. Although, working with global objects makes me kinda sad.

  • jmtt89 Avatar

    Test Failed: Wrong average on empty array, expected NaN -.-

    I think is better 0 than NaN

  • kalpers Avatar

    This comment has been hidden.

  • easyCoder Avatar

    This comment has been hidden.

  • easyCoder Avatar

    I can pass the unit tests in my window but not on the Attempt button.....

  • JanPolasek Avatar

    Nice, this is exactly the sort of katas I like

  • Stormseeker Avatar

    Wow, I had no idea some of these functions existed for working with plain JS Arrays!

  • xDranik Avatar

    Nice Kata! :)

  • delfuego Avatar

    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.