hamza9
December 7, 2020, 8:32pm
#1
Can you help me please to optimize this code:
function breakingRecords(scores) {
var arr1 = scores.filter((i, d) => d !== 0 ? Math.max(...scores.slice(0, d)) < i: i).length;
var arr2 = scores.filter((i, d) => d !== 0 ? Math.min(...scores.slice(0, d)) > i: i).length;
return (arr1 - 1).toString() + " " + (arr2 - 1).toString();
}
What are you trying to do with the code? To you have some sample inputs and outputs?
hamza9
December 7, 2020, 8:49pm
#4
sample input: [10, 5, 20, 20, 4, 5, 2, 25, 1]
sample output: 2 4
You titled your topic “Runtime Error”. What error are you getting?
hamza9
December 7, 2020, 8:55pm
#6
I am getting a Runtime Error.
Right, but a Runtime Error contains more information. What is the full error?
The instructions state for you to return an array with two elements. Is your function doing that?
hamza9
December 7, 2020, 9:00pm
#10
Yes, It does that. I am sure the problem is the time of execution
hamza9:
Yes, It does that.
Not according to the code you posted above. You might want to validate the type of data you are returning.
Can you validate what I last stated?
hamza9
December 7, 2020, 9:11pm
#14
I used multiple types of data as outputs such as string and integer … but It still doesn’t work
The above line returns a string, but the instructions state to return an array with 2 elements. Those two elements should be integers. First get your function’s return value to be what is expected, then we can work on the logic of your algorithm.
2 Likes