Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Your code is wrong because upon
cnt
exceedsn
, each element after the first occurrence of such cases will be continuously removed fromcopy
until the last element. Whilst the task asks us to remove elements until there are<= n
remaining in the original array.Not only you are modifying the original array which is bad practice, but also your conditional check is wrong. (Re-read the description)
This comment is hidden because it contains spoiler information about the solution
Don't modify the input.
Why was Voile's answer downvoted?
Voile is correct - the example returns 6 because from the input (-2, 1, -3, 4, -1, 2, 1, -5, 4) you have to find the sequence which yields the highest sum.
That (as Voile correctly states) is "4, -1, 2, 1" because 4 - 1 = 3, then 3 + 2 = 5, then 5 + 1 = 6. There are negative numbers before (-3) and after (-5) this sub-sequence so the sub-sequence in the result is the yields the highest sum.
[4, -1, 2, 1]
No, you admitted that you're writing code, having no idea what it's doing or why it's doing so. This is fine, and it's okay to ask people for help, but this is not an excuse for acting like an ass.
These are not my returns, these are your returns. I thought you'd understand this
unless you're new to understanding stuff as well.I think you don't really understand what you're doing O_o
Anyway:
Looks like the logic in your first loop is flawed, and you check which numbers (
even/odd
) make up the array incorrectly.Okay, pass these test cases then:
But your code only returns
true
if the string is empty after you cleared the letters (e.g"abcdefghijklmnopqrstuvwxyz1"
).Also note that you're returning
null
in the end, which is a wrong return value.Also note that your code fails for pangrams with the same letter occuring twice (e.g
"abcdefghijklmnopqrstuvwxyzz"
).In short, if you can't come up with the specific test cases that breaks your code, it will be very hard to debug ;-)
Why would you do that? ;-)
What if there are non-letters in the input? From the description:
Already told you you're confusing the tests values, write
console.log(a, b);
as the first line in your function, the console.log data comes before the test result message, so you're actually seeing the input values of the next test, not the one your function is failing with the code you wrote.I've changed the tests to be more specific, try now, after clicking reset.
Use spoiler tag when posting code like that and use proper markdown
Your code returns
[1, 2]
not[]
with the input you wrote. Maybe you're confusing the input log with the previous/next test. Useconsole.log
inside your function to see whata
andb
are. Mutating an array you're going through with a loop is a very bad idea, you skip values like that. Your code is wrong, fix it.