I tried the following. And the console printed out a 4 instead of the names ( including Pete ).
function addFriend(userObj, friend) {
// change code below this line
return userObj.data.friends.push(friend);
// change code above this line
}
console.log(addFriend(user, 'Pete'));
If I separate things like -
function addFriend(userObj, friend) {
// change code below this line
userObj.data.friends.push(friend);
return userObj.data.friends;
// change code above this line
}
The above, returns the array contents as expected. What’s happening under the hood to cause the differences in what console.log prints out?
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.
this seems something inherent to push() itself. Not sure why this would be something useful, though. But I guess it’s just how it’s implemented? (or should I try to dig deeper here?)
Returns the new length property of the object upon which the method was called.
You’re right. It doesn’t make too much sense to return that, javascript was made in a hurry, so there’s a lot of bad/weird parts. They can’t just change it though, image how many codes may depend on that.
It’s not wierd or bad behaviour, this is absolutely expected, and how it works in a very large number of languages. It needs to work that way because it is how you implement stacks and queues.
How is returning the length of the array helpful to implementing stacks and queues?
And is that something we/I will get to learn in any detail through this JS course/curriculum? I am not feeling confident about what I am learning here compared to what I have heard software developers learn and work upon. I was hoping what I learn here for DS/Algo would transfer to other languages too, but feeling less confident about that now But I guess will keep going through this and keep learning.
In Java it does, dequeues and linkedlists and stacks. In Python it does, dequeues. C# same. C++ same. Etc etc. Almost all languages with a good selection of data structures out of the box implement it on the relevant data structure.
Arrays in JS are not like arrays in many other languages, they are just a variation in the only data structure available in the language. There is no dequeue data structure or stack data structure available out of the box, so everything is dumped on array. So same as Perl, which is similar to JS in that respect (where JS lent this from afaik). Ruby is same as Perl. Both of those have push/pop etc on arrays.
Not really, this isn’t a CS course. But conversely, if you do a CS course, then you are unlikely to be learning much of what a software developer actually does day-to-day most of the time. The knowledge is fully transferrable. There is just a great deal of it, and FCC, as with any course, can only provide a bit of that knowledge: the course is big and covers a lot of ground, but it’s still just one course, focussed toward web development.
(Sorry wasn’t trying to avoid the first question, just on v low battery so can’t type up an example cos phone’s almost dead)