2

I'm trying to solve a challenge on Codewars which requires you to reverse an array in JavaScript, in 16 characters or less. Using .reverse() is not an option.

The maximum number of characters allowed in your code is 28, which includes the function name weirdReverse, so that leaves you with just 16 characters to solve it in. The constraint -

Your code needs to be as short as possible, in fact not longer than 28 characters

Sample input and output -

Input: an array containing data of any types. Ex: [1,2,3,'a','b','c',[]]

Output: [[],'c','b','a',3,2,1]

The starter code given is -

weirdReverse=a=>

My solution (29 characters) is -

weirdReverse=a=>a.sort(()=>1)

which of course fails -

Code length should less or equal to 28 characters.

your code length = 29 - Expected: 'code length <= 28', instead got: 'code length > 28'

I'm not sure what else to truncate here.

Note - I did think about posting this question on CodeGolf SE, but I felt it wouldn't be a good fit there, due to the limited scope.

Community
  • 1
  • 1
Manish Giri
  • 3,562
  • 8
  • 45
  • 81
  • https://codegolf.stackexchange.com/ – adiga Sep 07 '17 at 08:59
  • You can do it by using for loop like for (var i= array.length-1; i >=0; i--{ /// do your staff here } – MMRahman Sep 07 '17 at 09:00
  • 1
    To the people above, please read the question fully. I feel like if this question was restructured/reworded it would fit nicely with codeGolf rather than on SO, but I don't know their policy on using them to answer challenges on another site. – George Sep 07 '17 at 09:03
  • 3
    I'm voting to close this question as off-topic because this belongs on https://codegolf.stackexchange.com – Cerbrus Sep 07 '17 at 09:03
  • Cheers for the site btw, signed up. Looks fun! – George Sep 07 '17 at 09:15
  • `f=a=>a.length?[a.pop(),...f(a)]:[]` – Bergi Sep 07 '17 at 09:17

1 Answers1

6

I'd like to give you a hint, without giving you the answer:

You're close, but you can save characters by not using something you need to add in your code.

By adding the thing you won't use, you can remove ().


Spoiler (answer):

// Note: this only really works for this specific case.
// Never EVER use this in a real-life scenario.

var a = [1,2,3,'a','b','c',[]]

weirdReverse=a=>a.sort(x=>1)
//                     ^ That's 1 character shorter than ()

console.log(weirdReverse(a))
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • Got it! Thanks for the hint! – Manish Giri Sep 07 '17 at 09:12
  • Except that this [absolutely won't work](https://stackoverflow.com/q/24080785/1048572)? Try with an array longer than 10, which will use a different sorting algorithm... – Bergi Sep 07 '17 at 09:16
  • @Bergi: Code golf exercises like there are more about _"What works for this specific case"_ (which it does) – Cerbrus Sep 07 '17 at 09:27
  • @Cerbrus Meh, I could solve specific cases by just returning a constant. The test suite provided by the challenge author looks much like he was asking for a generic solution. There is no mention of "needs to work only on arrays shorter than 10 items, only with V8". – Bergi Sep 07 '17 at 09:46
  • 1
    @Bergi: Try the answer's code in the codewars challenge. It's accepted. Apparently, it's generig enough. I don't know of a different (just as short) method to reverse an array. Now, what point are you trying to make here? That this isn't code you'd put in production? – Cerbrus Sep 07 '17 at 09:55
  • 1
    @Cerbrus Yes, exactly that. I fear that not even the original author was aware of this limitation, and instead thinks of it as a clever trick. Before it becomes popular, someone should place a clear disclaimer on it. – Bergi Sep 08 '17 at 08:09
  • @Bergi: I added a disclaimer in the code. Better? :-) – Cerbrus Sep 08 '17 at 08:56
  • @Cerbrus Thanks! – Bergi Sep 08 '17 at 08:57