Confusing solution

Tell us what’s happening:
hey everyone,
so my issues with this line of code is

while(arr.length > 0 && !func(arr[0])){
    console.log(func(arr[0]))
    arr.shift()
  }

condition: [1, 2, 3, 4], function(n) {return n >= 3;}
so for first iteration : n=1
arr.length is true, 1 is smaller than 3 so its FALSE. condition in while loop is opposite of the return so false of false is true which means the loop continues ??!
then second iteration: n=2 (same thing as first iteration)
arr.length is true, 1 is smaller than 3 so its FALSE. condition in while loop is opposite of the return so false of false is true which means the loop continues ??!
then third iteration, n=3
arr.length is true, && 3 is equal to 3, meaning func(arr[n]) is true. but in the while loop the false of true is false, so does that mean the function stops here ??!

point is I guess what exactly does it mean with !func(arr[0])

  **Your code so far**

function dropElements(arr, func) {
while(arr.length > 0 && !func(arr[0])){
  console.log(func(arr[0]))
  arr.shift()
}
return arr;
}

dropElements([1, 2, 3, 4], function(n) {return n >= 3;});
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36

Challenge: Drop it

Link to the challenge:

Do you have your own solution that you wrote handy so we can help you compare the two?

Not really, trying to understand how it works. For the while loop to continue, does it have to get both TRUE values, so the whole reason with !func(arr[0]) is to get a true value , however in an array like [1,2,3,4] with condition n >3 , how would it continue to push the last element in the array which is 4. Wouldn’t one of the conditions be fulfilled with number 3 which means the whole loop would stop ?!

It is so much easier to understand how it works if you write your own solution first. Reading someone else’s code doesn’t really help you learn how to write code yourself.

appreciate if you don’t make assumptions, I only post to the form when I am completely stuck. It’s my last resort .

and that’s what I do, I time every problem, 30 mins each is the average unless I got carried away and got stuck. then I look at the solution to understand how to solve it, then come back a couple of days later to solve it.

things like you are not going to learn code on your own if you just looked at some else’s code is disrespectful and annoying, I am up grinding for 6 or 7 hrs now. I will take any useful advice but not that type

This is useful advice. Looking at solutions will not help you learn how to write solutions. It may feel really important to get the check marks, but it is far, far more important to go through the problem solving process.

You should post to the forum before looking at the solutions. We can help you take what you have and get you unstuck so you still gain the benefit of the problem solving process.

Struggling alone for 6-7 hours doesn’t really help you if you stop and copy the answer. Struggling for half an hour and then getting some hints that let you finish your solution is much more effective at building your problem solving skills and a much more efficient use of time.

Me walking you through someone else’s solution doesn’t help you write a solution on your own in the future.

Solutions are best saved for looking at alternate approaches ones you have generated a solution yourself.


So, where did you get stuck. What did you have before you copied the solution? We can help you get from where you got stuck to the solution.

well thanks for responding after my angry comment, I had my own code but it’s erased now I replaced it with the solution, what i would do with the solution even before posting (most of the times) is that I would write it down line by line and console.log and play with it to get a better understanding.
and I get your point , and thanks for the advice

1 Like

This is the operator you were talking about by the way:

It converts truthy values to false and falsy values to true.

1 Like

and to add to this discussion, looking at solutions can be extremely beneficial depending on how you process information. people learn differently. does not mean by any way to go around just looking at solutions and not trying but to spend too much time on just one problem until you crack it might not be as beneficial as you think. it can end up with you wasting more time over something you already knew and have overlooked inadvertently at that certain problem

Looking at solutions after you have solved the problem is very useful, yes. Learning how to understand someone else’s code is an important skill. It is, however, a different skill from learning how to write code.

Again, I don’t think spending tons of time stuck on a problem is a good idea. I think talking to other people to brainstorm and problem solve is a great thing when you are stuck. Working with someone else to help you get unstuck is good. It is different than looking at a solution.

Debugging and problem solving is the essence of programming… Professional programming is all about trying things, getting stuck, and working through it.

I don’t see practicing problem solving and talking to others as a waste of time at all. That’s just another way to say ‘practicing programming’.

sure, but certain situations dictate other approaches. your response time is great. however many times I would post a question (not necessarily here, I have unanswered questions on stack overflow/reddit/… as others do as well) , so you don’t want to get out of the zone of that question so the immediate reaction is to tackle the solution.

Sure, but getting the answer is, frankly, not the point. The green check marks aren’t the point. The point is the problem solving skills that you develop. Copying the answer means you have one fewer opportunity to develop those skills. It’s been my experience that once someone gets in the habit of copying the solutions, they don’t build all the skills they need for the projects, or the skills to be job ready (or to do the hobby projects they want).

Best way to understand solutions you dont understand is to pin point the exact bits you dont get and look further what they mean(google etc). The solution you provide is quite full with different techniques and you must be clear with yourself what part you dont get. Is it what the while condition means, is it where or what the arr[0] elements stand for, or maybe you are not aware how shift() mutates the array its executed on. There is also a callback function, which sort of works as a condition criteria and working with callbacks initially can be very confusing. And dont take it as an ultimate goal to understand right now how things happen in the code, as this can only happen with time and practice and i guarantee you few months coding from now, you might look back at this code by chance and realize you can read it without much effort and this would be the benefit of experience. Just spend time doing what you do and it will come to you.

2 Likes

thanks, I agree with you. that’s mostly the approach I use. and I know that I am going through the intermediate algos part right now and that i am not fully understanding everything but I know that I will come back to that section back to test myself.

1 Like

Setting aside @JeremyLT’s excellent advice on approaching problems, let me directly address your questions and source of confusion.

For a while loop to continue, the conditional must evaluate to TRUE (or something truthy, since this is Javascript). For logical AND (the && operator), both clauses must evaluate to TRUE. As @JeremyLT pointed out the, logical NOT operator (!) inverts the logical value of its operand.

Now…what does the code do? Well…it filters and mutates its input according to the filter passed in func(). In the example you posted, the filter is for n>=3, so yeah, the loop stops on the third iteration.

jrm

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.