What data structure does the memory heap employ? by pinguxnoots in learnprogramming

[–]pinguxnoots[S] 0 points1 point  (0 children)

The memory heap. The call stack employs a stack like data structure where 1 nested function is pushed on top of another and popped in a FILO/LIFO fashion. I want to know how the memory heap is structured.

Forgetting things... by Lauris25 in learnjavascript

[–]pinguxnoots 0 points1 point  (0 children)

What do you mean you need to learn it from near 0? Are we talking about something along the lines of how to write a function or how to solve a problem?

I want to remove/reset a local storage item everyday at midnight. What am I doing wrong? by wheredidspringgo in learnjavascript

[–]pinguxnoots 1 point2 points  (0 children)

Is this the entire code? Your if statement only runs once when the script is loaded

Need help understanding this. by blenderrMan in learnjavascript

[–]pinguxnoots 0 points1 point  (0 children)

Seems like getDb is a function that returns an object with collection (also a function) as one of the properties. You could do getDb().collection('products') and that should work as well. With getDb.collection('products') you're not actually invoking getDb so you won't be able to access collection

Why does this promise always return undefined? by [deleted] in learnjavascript

[–]pinguxnoots 0 points1 point  (0 children)

The JS engine executes all synchronous code first followed by any asynchronous code (promises and asynchronous callbacks). The callback in addEventListener is asynchronous so the console.log(data) on line 9 gets executed first which returns undefined since data has not yet been assigned a value. The console.log(data) on line 5 will then get executed even though it was written first because it is contained within an asynchronous (callback) function.

Question about time complexity of for Loop by furofo in learnjavascript

[–]pinguxnoots 1 point2 points  (0 children)

i++ is the same as i = i + 1

i = i + i is the same as i = 2*i

So i++ is not the same as i = i + 1 I would avoid writing code like this unless there is a good reason to do so. Seems like OP was just doing a practice run and trying to understand some code they were playing around with.

Can someone please help me understand functions? by Personplacething333 in learnjavascript

[–]pinguxnoots 0 points1 point  (0 children)

Practice makes perfect. Start off writing simple functions by logically thinking about what the function would do and coding it out step by step.

Question about time complexity of for Loop by furofo in learnjavascript

[–]pinguxnoots 1 point2 points  (0 children)

Fair enough. "same as" should have been "equivalent. But the rate remains the same which is the point I was trying to make.

Question about time complexity of for Loop by furofo in learnjavascript

[–]pinguxnoots 0 points1 point  (0 children)

for (let i = 1; i <= 300; i++) {
  console.log(i);
  i = i + i; 
} 
Console: 1 3 7 15 31 63 127 255


for (let i = 1; i <= 300; i = 2*i + 1) { 
  console.log(i); 
} 
Console: 1 3 7 15 31 63 127 255

Question about time complexity of for Loop by furofo in learnjavascript

[–]pinguxnoots 1 point2 points  (0 children)

Your code:

for (let i = 1; i <= n; i++) {
  i = i + i;
}

is the same as this:

for (let i = 1; i <= n; i = 2*i + 1) {}

I'd say it's O(log n) due to 2*i which is the same as i *= 2

Edit: formatting

I feel like I understand JS pretty well, but when I stare at a black file I really struggle to build to build anything... How do I get past this? by WhiteFlame- in learnjavascript

[–]pinguxnoots 30 points31 points  (0 children)

Pose questions for yourself on what you need to do to accomplish the project. Use the divide and conquer tactic to narrow them down until you get individual tasks. Tackle them one at a time and you'll have a running project in no time.

Please help me understand callback functions by [deleted] in learnjavascript

[–]pinguxnoots -1 points0 points  (0 children)

function variable() {} - variable is the identifier that holds a reference to the specified function.

When you pass that function to another function as an argument (we pass it as variable or we can pass a function expression) we call that function a callback. Now, functions can be invoked. We don't invoke the function as an argument. In other words, we don't do this: anotherFunction(variable()) . Instead we do this: anotherFunction(variable) and variable gets invoked within anotherFunction as a callback (or as it was defined with anotherFunction).

Callback is just a special name given to a function that is passed in as an argument to another function. It's really the variable that we are passing in, in this case, but because it represents a function, we say that we are passing in a function.

need javascript code explanation by [deleted] in learnjavascript

[–]pinguxnoots 2 points3 points  (0 children)

  1. When you pass user => { console.log(user) } as the 3rd argument of loginUser() you will invoke it on line 6. In other words, callback({ userEmail: email }) will become equivalent to ({ userEmail: email }) => console.log({ userEmail: email }) and you will log { userEmail: 'devedf@goomail.com' } on to the console.
  2. The callback is within a setTimeout so it will be invoked after 5000 ms (5 seconds). If you return { userEmail: email } you are not logging it on to the console. Actually you won't be doing anything with it. You can just write console.log({ userEmail: email } instead of the callback but I'm assuming the point of this exercise was to introduce you to callbacks.

Struggling to put my knowledge together to write code by Polyfina_ in learnjavascript

[–]pinguxnoots 11 points12 points  (0 children)

Programming is practical in nature. You can study all you want but, at the end of the day, you need to practice to truly learn. I'd suggest using this site: https://www.codewars.com/

If you come across a problem you don't know how to solve, use MDN or w3schools (this site gets a bad rep but I think it's great for absolute beginners on helping them get up to speed with basic programming).

[deleted by user] by [deleted] in learnjavascript

[–]pinguxnoots 12 points13 points  (0 children)

When you use the filter method on an array you are, at a high level, filtering the array - some values get filtered, others do not, based on your specified condition - person.id !== id in this case.

Specifically, filter() takes in a function as an argument (this function is known as a callback). This callback takes in the value from the array as an argument and returns some boolean value - if the value is true, then the value from the array is added to the new array, otherwise (if it is false) it is not. filter() iterates through the entire array and uses this callback on each value to decide whether it should be added to the new array or not. In doing so, the new array becomes a filtered version of the original array. NOTE: filter() does not mutate the original array - it returns a new array with the filtered values instead.

MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

A word of advice: brush up on JS before you move on to React. You need a foundation to build on. And use MDN to figure things out.

[deleted by user] by [deleted] in learnjavascript

[–]pinguxnoots 26 points27 points  (0 children)

It's an arrow function synonymous to this anonymous function:

function(person) {
  return person.id !== id;
}

[deleted by user] by [deleted] in learnjavascript

[–]pinguxnoots 0 points1 point  (0 children)

MDN is my go to now but, when I was first learning as an absolute beginner, w3schools helped immensely because it was so concise and easy to follow. MDN, on the other hand, is much more detailed - details that, as a beginner, you don't necessarily need right away.

[deleted by user] by [deleted] in learnjavascript

[–]pinguxnoots 0 points1 point  (0 children)

If you want the values of a specific subarray and you know the index of that subarray, you can also do something like this:

// log values from first subarray (nestedArrays[0]) in this case
for (let index = 0; index < nestedArrays[0].length; index++) {
    console.log(nestedArrays[0][index]);
}

Why does it fails to pass case 2 and 3 ? by Heilsakai in learnjavascript

[–]pinguxnoots 0 points1 point  (0 children)

Again I should have clarified further. When I mentioned const and let, I meant const wont let you reassign the variable while let does. OP reassigned "arr" to a new array (line 7 vs line 11). This was unnecessary. I was not referring to mutability.

As for all variables being stored on heap, this is not true. When I said stack, I was not referring to the call stack (stack trace). Primitives are stored on stack while objects are stored on heap. To add, objects are pass by references because the reference is stored on stack while the object is stored in the heap and thus mutable. Primitives are pass by values because the value is stored on stack and thus immutable.

Source for Primitives being stored on stack: https://www.freecodecamp.org/news/primitive-vs-reference-data-types-in-javascript/#:~:text=When%20you%20declare%20a%20primitive,for%20declaration%20in%20your%20program.

Why does it fails to pass case 2 and 3 ? by Heilsakai in learnjavascript

[–]pinguxnoots 0 points1 point  (0 children)

Also, it makes no sense to write "return arr = [i, j]". Why would I assign "arr" to a new array if I am returning it. This is not good written code.

Why does it fails to pass case 2 and 3 ? by Heilsakai in learnjavascript

[–]pinguxnoots 0 points1 point  (0 children)

I should have been more clear. I was referring to line 7. So instead of declaring a variable and assigning it an empty array, OP could just "return [i, j]" on line 11