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.
// My code starts here.
function noFuckingEggs(array) {
let emptyArray = [];
let counter = 0;

array.reverse();

for (let i = 0; i < array.length; i++) {
if (array[i] === "egg") {
counter++;
if (counter > 2) {
emptyArray.push(array[i]);
}
} else {
emptyArray.push(array[i]);
}
}
return emptyArray.reverse();
}
console.log(noFuckingEggs(["egg", "apple", "egg", "egg", "ham"]));
// My code starts here.
function noFuckingEggs(array) {
let emptyArray = [];
let counter = 0;

array.reverse();

for (let i = 0; i < array.length; i++) {
if (array[i] === "egg") {
counter++;
if (counter > 2) {
emptyArray.push(array[i]);
}
} else {
emptyArray.push(array[i]);
}
}
return emptyArray.reverse();
}
console.log(noFuckingEggs(["egg", "apple", "egg", "egg", "ham"]));
the youtubers github repo answer is below.
function removeEgg(foods) {
// To remove the last 2 'egg', reverse the array first.
const reversedFoods = foods.reverse();

const result = [];
let eggsRemoved = 0;

for (let i = 0; i < reversedFoods.length; i++) {
if (reversedFoods[i] === 'egg' && eggsRemoved < 2) {
eggsRemoved++;
continue;
}

result.push(reversedFoods[i]);
}

// At the end, remember to .reverse() back the result.
return result.reverse();
}

console.log(removeEgg(['egg', 'apple', 'egg', 'egg', 'ham']));
function removeEgg(foods) {
// To remove the last 2 'egg', reverse the array first.
const reversedFoods = foods.reverse();

const result = [];
let eggsRemoved = 0;

for (let i = 0; i < reversedFoods.length; i++) {
if (reversedFoods[i] === 'egg' && eggsRemoved < 2) {
eggsRemoved++;
continue;
}

result.push(reversedFoods[i]);
}

// At the end, remember to .reverse() back the result.
return result.reverse();
}

console.log(removeEgg(['egg', 'apple', 'egg', 'egg', 'ham']));
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
MarkBoots
MarkBoots2mo ago
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)
glutonium
glutonium2mo ago
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
for(let i = array.length - 1; i >= 0; i--) {
// code
}
for(let i = array.length - 1; i >= 0; i--) {
// code
}
MarkBoots
MarkBoots2mo ago
then the resulting array is in the wrong order unless instead of push, you add it with unshift
glutonium
glutonium2mo ago
so ig u just call the reverse on it? that's still 1 less reverse xD OHH ya i forgot that unshift exists
MarkBoots
MarkBoots2mo ago
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
glutonium
glutonium2mo ago
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
MarkBoots
MarkBoots2mo ago
because it seems OP is still learning, don't overcomplicate it
glutonium
glutonium2mo ago
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
Raskin
RaskinOP2mo ago
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
MarkBoots
MarkBoots2mo ago
no problem, glad to help
ἔρως
ἔρως2mo ago
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
next: for(let i = 0; i > 10; i++) {
for(let j = 0; j > 10; j++) {
if(i === j) {
continue next;
}

console.log(`${i} ${j}`);
}
}
next: for(let i = 0; i > 10; i++) {
for(let j = 0; j > 10; j++) {
if(i === j) {
continue next;
}

console.log(`${i} ${j}`);
}
}
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 normally
Rowe2ry
Rowe2ry2mo ago
Interesting 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.
No description
Rowe2ry
Rowe2ry2mo ago
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.
No description
Want results from more Discord servers?
Add your server