skip eggs function
I'm doing a code challenge to test my skills as a fledgling coder trying to learn new concepts. The challenge is to write a function that will remove the first two strings 'egg' from the end of an array. I'm doing this from a youtube video and was wondering if my solution does the same thing that the youtube video had referenced as its correct answer. i see the same out put in console.log(): I will post my code first and then the "correct" answer second.
the youtubers github repo answer is below. I don't have anyone to give me feedback to see if i'm on the track or not. I've tried asking AI but between them getting confused and just straight up halucinating wrong values I need some human help. I think i got it right but because the code looks different i'm not sure.
the youtubers github repo answer is below. I don't have anyone to give me feedback to see if i'm on the track or not. I've tried asking AI but between them getting confused and just straight up halucinating wrong values I need some human help. I think i got it right but because the code looks different i'm not sure.
13 Replies
yea, your logic seems to be correct.
reverse the array and iterate trough it
- if the value is egg, check if you already have 2.
* if not: increase the counter and continue
* if so, push it to the new array
- if the value is not egg, push it to the new array
reverse the array back.
the example of the youtube vid combines the egg value + counter check.
- if it is an egg AND (&&) the counter is less then 2, increase counter and 'continue' (skip the following bit, go to next iteration)
- if not, add it to the array (else is not needed, because it won't reach this bit because of the continue)
i think it's better if u just reverse iterate the array so u don't need to reverse the original one and then reverse the result yet again
then the resulting array is in the wrong order
unless instead of push, you add it with unshift
so ig u just call the reverse on it?
that's still 1 less reverse xD
OHH ya
i forgot that unshift exists
but the question was if his logic was correct. there are multiple other methods and logic, but i kept it with the explanation of op's examples
well my idea was, if we could reverse iterate and just use splice to remove the egg from the arrag directly
we do have to take the index in consideration as now it changes as the element count changes
because it seems OP is still learning, don't overcomplicate it
but if we can do that then we can just move 2 eggs and just break the loop
ya true true
i was just kinda seeing the best possibilities xD
but ya that's right
it becomes too complicated
The author of the challenge repo referenced continue and break into some questions but didn't go over the material in the video. I believe i was meant to googled it but i skipped over it by accident. As a result I didn't understand the answer. I believe my nested if statements performed similar to his combined condition with continue but his is more cleaner. Thank you for referencing and explaining the syntax and code terminology I used instead of introducing new or unexplained concepts i haven't used before. I was curious if it was possible to reach the same answer by different means in code and you validated my hunch but again I wasn't sure because I'm learning on my own. So thanks again
no problem, glad to help
something to remember about break: it doesnt just mean to go to break the loop
it can also be used to break out of a switch case
also, continue and break can take the name of a label and jump to it
this contrived example shows the usage of the continue with a label
and yes, the loop continues from where it was before
if
i
was 3, it will go to 4 and continue normallyInteresting plug for C#, I asked AI to write a function for this and it gave a similar one that has been discussed here.
Then I wrote a simpler, easier to read, and more performant version of the function and asked the AI to compare the compute & memory cost of the 2 and came out a head. Everyone wants to act like we're crazy for being a Blazor C# web assembly shop instead of using a JS framework but I have fallen deeply in love with the C# capabilities in a front end web application.
I did have a typo in my variable name in the loop but you get the idea.
LastIndexOf()
returns -1
if it does not find anything, so that's what the return statement is checking for.