I’ve challenged myself to 30 days of codewars, one a day, time myself, and learn how I could have done it better.
Today I did: https://www.codewars.com/kata/moving-zeros-to-the-end/
Time: 10 minutes
What I’ve learnt: So this was a 5KYU and labeled as an interview question. It didn’t look that difficult, but I thought it must be harder. Happy to say, I must be improving because it was not very hard.
I took the array we were given in, and filtered this based on values !== 0
and assigned this to a variable to use later on. Then I created a for loop to iterate as many times as the difference in length between the original array, to the newly filtered array, and passed in 0 to a newly created array assigned to it’s own variable.
And then I returned the two arrays combined with the spread operator `return […newArray, …zeroArray]`
What I learned from other people’s code: Ok, so silly me, I didn’t need a for loop for the 0’s, I could have just used two filters, one for everything but 0’s and then one for only 0’s and concatenated these two all in one line! But more or less the same idea! I thought someone would do something with regex, but I didn’t see any.
See you tomorrow!