Understanding the Undefined Value when returned from a Function
Functions can include return statements but they don’t have to. By doing so, the code runs but returns as undefined.
As you can see here the function addSum has no return statement underneath so the global sum (outside the function’s parameters) will be changed but the returned value of the function is undefined.
Assignment with a Returned Value
Using the assignment operator (=), we can store values. Therefore with this same thinking we can take a returned value and assign it to a variable.
Here we have the variable processed to have the predefined value of 0 (line 2), and then we create the function processArg with a return statement which takes the argument num and adds 3, then divides that total by 5 (line 4 and 5). As we can see on line 10, the function processArg is then called with it’s argument assigned a value of 7, and the returned value is assigned to the variable processed.
In Data Science (phoah we’re totally learning that! how exciting) a queue is an abstruct structure where items are kept in order. I don’t know about you but that’s right up my alley, thanks OCD. New items can be added to the back of the queue and old items are removed from the front of the queue. So, a queue works based on a first-in first-out (FIFO) principle.
The insertion operation is called enqueue, and the removal operation is called dequeue. The enqueue operation inserts an element at the end of the queue, whereas the dequeue operation removes an element from the front of a queue.
Think of people standing in a line, the first is served and leaves, and another person stands at the back.
Remember: the push() method adds elements to the end of an array and the shift() method removes the first element of the array and returns it as well.
I needed a bit of help with this but I think I have this resolved. Above is the excerise I had to solve in FreeCodeCamp. We’re going to ignore the lines from 16–19 for now.
On line 1, we define the function nextInLine with an array (arr) and a number (item) as arguments. You can see on line 3 and 4 that we’re taking the variable testArr and using the push() function to add an item onto the end of the array (the array can be seen in line 14), and the shift() function to remove the first value of the array. Remember the first value of an array is in position “0". Then on line 7 we are asking to return the array removed and the number added.
Aaaand let’s keep it short today, I’ll see you all later and Merry Christmas!