« first day (3391 days earlier)      last day (1540 days later) » 
03:00 - 16:0016:00 - 22:00

V.7
3:51 AM
Hey there ;)
So, could anyone suggest why AngularJS does not run a function on ng-init attribute, but {{test}} shows a value from scope?
I mean, for example, <div ng-init="inittest()"> does not work, but {{test}} shows value with scope.test = 1 set
scope.inittest = function() { alert (1); } exists
Controller is assigned
 
4:17 AM
the new cookie button from vice
 
4:55 AM
It should be.... "Please Sell My Info" "Do Not Sell My Info"
@V.7 it would be pretty helpful if you put this on stackblitz.com or something or show it here
 
 
2 hours later…
6:43 AM
@forresthopkinsa Getting third party plugins "using an url"? Count me out, that's asking for trouble down the drain somewhere. It's already a leap of faith using npm (and there are example of malicious update to existing packages). But if for each compilation/run I potentially, implicitly download packages I really can never be sure what is running when.
 
yep I'm also not on that train
@JBis hot
 
 
3 hours later…
9:52 AM
hi , i'm trying to use Javascript autocomplete.js
i found this part of the code that i don't understand
["focus", "blur"].forEach(function (eventType) {
  const resultsList = document.querySelector("#autoComplete_list");

  document.querySelector("#autoComplete").addEventListener(eventType, function () {
    // Hide results list & show other elemennts
    if (eventType === "blur") {
      action("dim");
      resultsList.style.display = "none";
    } else if (eventType === "focus") {
      // Show results list & hide other elemennts
      action("light");
      resultsList.style.display = "block";
whenever i run the code i get :
indexAutoComplete.js:135 Uncaught ReferenceError: action is not defined
action('dim'); and action("light");

and its true there is no reference for any of them inside the code, not sure where it came from but it works smoothly in the demo page without any reference to them either.

tarekraafat.github.io/autoComplete.js/demo
nvm
i've just realized i never really attach the css file to my html on my work computer , i only did this on my personal pc . sorry
 
I have a doubt in react js
this is working but I done the same in a codepen with my login but its wasn't giving the same result
what could be the issue
it shows
react-dom.development.js:27742 Uncaught Error: Target container is not a DOM element. at Object.render
 
10:16 AM
@CommonMan missing a HTML element
check the HTML tab
 
so I followed the same
what HTML element i supposed to add
<div id=app></div>
the above code ?
 
@CommonMan how does it not have any HTML ??
 
@KarelG
 
what is this then?
 
I couldnt' see any if I click the down arrow button
oh wow
the moment i clicked on inspect to see oncsole
console
i seen this
<div id="root"></div>
by the way your image not found @KarelG
yeeeei! i got it... thank you KarelG
 
10:22 AM
it is an image of your codepen with a red circle around that html snippet and a huge question mark
 
it didn't got rendered... anyway thank you @KarelG
 
11:16 AM
hi , have anyone used autocomplete.js before ?
 
11:32 AM
hi every body, do you know what wrong with this line of code (javascript)
[1,31,423].map((function(e){return{name:e,evt:new Event(e)}}))
my "belowd" browser ie11 don't like it
"beloved"*
 
Hi@all @KostyukRostyslav@diamond I have developed patternlibrary and i hvae written a component with pure HTML and Javascript. Now i am trying to write a test cases for the component. I havent got any proper link or blod to help me to proceed with it. Can any one help me on how to it pls?
 
@KostyukRostyslav new Event(e) is your problem in IE
IE does not support it AFAIK. You can use either MDN or caniuse website to check it
- https://caniuse.com/#feat=mdn-api_event_event
- https://developer.mozilla.org/nl/docs/Web/API/Event/Event#Browser_compatibility
 
@KarelG yeah, to be more precise swipe events are the problem. Thnk you very much for your replay, I realy apriciate it
 
@Sravani please don't ping some users randomly.
 
do there some polifil to extend ie11 events list?
 
11:42 AM
@KarelG ok
 
There is a polyfill I think. It uses the old document.createEvent('Event') stuff
check polyfill.io
 
@Sravani so you developed something like react that lets you write patterns and then extend them, am I right?
if it's so you must give ass some more info
@KarelG thx
I !love ie11 <3
ie in general
 
@Kostyuk Yes , We have developed something like Carbon design system . People use my components like buttons similary how we use angular materian button or other components
 
most of us hates IE 😛
 
@Kastyuk We have developed components using Pure HTml and Js to be able to compatible with other frameworks also
Now I need to write the jasmine test cases for it
 
11:53 AM
@Sravani lol I need to compleat learing jasmine and I am afraid I am not very familiar with Carbon either. I use vue &vuex. If your components have similar structure to vue.js' components. I will do something like this.
 
@KostyukRostyslav hehe Fine
 
<color-transformer :colorRGB="255 255 255">
and you component must transform the rgb collor in hex I will make result checker that checks hexColor attribute of your componet
 
ok
 
12:21 PM
I'm writing a recursive function that returns the sum of numbers in an array. Why is undefined being returned when I call the function?
var sum = 0;

var numbers = [1, 2, 3, 4];

function sumNumbers(array, index) {
  if (index > array.length - 1) {
    return sum;
  }

  sum += array[index];

  // console.log("Each sum variable: " + sum);


  sumNumbers(array, index + 1);
}

console.log("sum of numbers in array: " + sumNumbers(numbers, 0));
 
@MyWrathAcademia because not all paths return something..
also, this is an awful example of recursion
if your teacher told you to do this as homework, it's not a good example of how recursion is used
 
@Neil So only the bottom stack returns something?
@Neil I made up the example. Basically I'm learning how to use recursion by replacing loops with recursive functions
 
no, you're only returning something if you write "return something;"
 
I did write return sum
 
ok, well take this example
sumNumbers([1, 2, 3, 4], 0);
this calls sumNumbers([1, 2, 3, 4], 1), then sumNumbers([1, 2, 3, 4], 2) then sumNumbers([1, 2, 3, 4], 3) then sumNumbers([1, 2, 3, 4], 4)..
at that point, it returns sum, which is 0, it's true
though the call to sumNumbers([1, 2, 3, 4], 4) doesn't save anything from that call
so it just shrugs and ends the function
if you wanted to do something with the call, you'd need to save it to a variable or return it
you're only calling
 
12:29 PM
I don't understand. When sumNumbers([1, 2, 3, 4], 4) is called, sum is 10 so how is it 0?
What do you mean that the call to sumNumbers([1, 2, 3, 4], 4) doesn't save anything from that call so it just shrugs and ends the function? Can you clarify?
 
you have to walk through each call as it gets run
also you're right, sum would be 10 after the call to sumNumbers([1, 2, 3, 4], 4)
 
what neil meant is
 
but you're not using that 10
 
*drawing*
 
Have you guys ever used Matter.js?
 
12:33 PM
Why isn't 10 being used?
 
The result of this call is:
sum += array[0];
sum += array[1];
sum += array[2];
sum += array[3];
return 10;
return undefined;
return undefined;
return undefined;
return undefined;
return undefined;
 
Returns don't implicitly persist through multiple function calls
 
if you reach the end of a function and return nothing, then you are doing "return undefined;"
 
returning multiple values in the same statement? thats a thing?
 
so your recursive call doesn't return anything to your other recursive calls which don't return anything to your main function hence the undefined
 
12:35 PM
you can return an array, that counts as multiple values, right?
 
you simply need to return your recursive case
@TaylorS neil was just showing the returns made be recursion not real code
 
you should figure out how to make this work without using global variables like sum
 
Right, I see so its like I suspected:
11 mins ago, by MyWrathAcademia
@Neil So only the bottom stack returns something?
 
right, except not. the bottom stack returns nothing..
 
@Neil Should the return of the call not be:
The result of this call is:
sum += array[0];
sum += array[1];
sum += array[2];
sum += array[3];
return 10;
return undefined;
return undefined;
return undefined;
Isn't the bottom stack where 10 is returned?
 
12:39 PM
no. You're in your deepest call when you return 10
 
Since sumNumbers([1, 2, 3, 4], 4) creates a stack?
Isn't that deepest call the bottom stack?
 
generally you talk about pushing something onto a stack.. that would make the deepest call the thing on top of the stack..
I appreciate the importance of making a pedantic point here, but shouldn't you be trying to learn how to fix this instead?
 
Man Im so bored, I decided to try out a new library, like matter or three but I got stuck in the first minute
might go back to Unity development for a while to get bored of it again and come back to JS
 
You can do js in unity
 
@Neil @JBis thanks for the explanation. Since I'm using a global variable I can just access the global variable from outside the function to get its value.
@Neil @JBis How would you write this recursiive function differently?
 
12:45 PM
well for starters, you wouldn't do this recursively
 
@JBis WEll thanks look what ya did... ya gave me ideas, and now ima go waste more hours of my life doing something lol
 
@Neil You mean using a loop instead?
 
@MyWrathAcademia Was this assigned by your teacher?
 
[1, 2, 3, 4].reduce((a, b) => a + b, 0)
this returns the sum of the array
of course reduce uses a loop, so it isn't faster
 
@JBis No it wasn't I'm just practising writing recursive functions in order to understand it better
 
12:48 PM
but it's a lot less verbose and imho, a little more elegant
but if you had to write this recursively:
 
hi , I'm trying to make country input field with states and maybe even towns.
I've a json object with all countries in the world but i'm looking for a library or a json object for all countries ..

is there is a library or an api that i can use for such task ?
 
Neil is correct. Bad example of recursive function but I guess its ok for an intro to recursion.
 
function sum(arr) {
    if(arr.length == 0) return 0;
    return arr[0] + sum(arr.splice(1));
}
 
@Neil I am not at the stage of understanding lambda functions so prefer not using it
 
12:48 PM
@MyWrathAcademia see image.
 
@Neil ew, stop one lining ifs
 
oops. that 10 should be 6 D:
 
function sum(arr) {
    if(arr.length == 0)
        return 0;
    return arr[0] + sum(arr.splice(1));
}
 
splicing an array each cycle?
 
Marginally better, i like brackets
 
12:49 PM
boo
 
@KarelG using recursion to sum an array? boo..
 
I'm the reduce guy :P
 
[1, 2, 3, 4].reduce(function (a, b) {
return a + b;
}, 0);
there's a lambda-less version
 
nvm
 
@KarelG +1 that looks like it took a lot of work
 
12:53 PM
i am waiting on your recursive sum :D
 
who?
 
function sumArray(array) {
    if (array.length === 0) {
        return 0;
    }

    function sumInt(int) {
        if (int === 0) {
            return 0;
        }
        return 1 + sumInt(int - 1);
    }

    return sumInt(array[0]) + sumArray(array.splice(1));
}
double recursion
XD
 
heh, inner function. I would do that too
I was thinking at
 
I don't get why people prefer const sumInt = (int) => { ... } syntax inside functions tho.
 
function sum(array) {
  if (!Array.isArray(array) || !array.length) {
    return 0;
  }
  const sumInner = function(arr, index) {
    return index === 0
      ? arr[index]
      : arr[index] + sumInner(arr, index-1);
  }
  return sumInner(array, array.length-1);
}
 
1:01 PM
@KarelG I understand your image.
 
@KarelG beautiful
 
Let's all slow down with all the fancy JavaScripy code.
 
even got some type checking in there
 
@JBis is even using closures now
 
@MyWrathAcademia We are just joking, don't use our examples they are horrible.
 
1:03 PM
we usually use .reduce to get our sum
 
You guys are gonna scare me away
:0
 
@KarelG Not even joking here. We had this exact assignment in my java class last year and the teacher made a second "helper" function to do this exact thing.
 
I have two questions:
(1) @Neil mentioned that I can write that recursive function the same way but without using a global variable. How do I this?
(2) If returning a value in a recursive function does not presist up through the function stack then how do I return something in a recursive function?
 
if you're thinking of a recursive function as a stack
what gets pushed onto the stack are the parameters you pass to the call..
what gets pushed off the stack is what gets returned
so in this way, if you keep track of the current sum, you don't need a global variable
 
@JBis uhm, the Java API has functionalities for that
 
1:08 PM
what is the sum of an array of numbers if not the first element plus the sum of the rest?
 
(but you know that. I think hope)
 
@MyWrathAcademia You need to keep returning so the value with cascades through the stack back to your original function. Basically you need to return recrusiveFunction(args)
 
the sum of the rest will handle its sum, and when it gets back to you, you just tack on the first element in the array and you're done
 
@KarelG He said using the Java API was "cheating"
 
he probably said that in that context (tutoring you)
but really, don't do that in business.
if there is an API method, use it
(there are exceptions on it ofc)
 
1:10 PM
i got into a fight with him. Learned the valuable lesson of don't get into fights with the people who determine your grade
 
if you gain that insight, that's all that counts ultimately
fighting with the teacher is both pointless and fruitless
 
@JBis I don't understand how return recursiveFunction(args) works? (1) wouldn't that recursive call just continue being called? (2) If return something doesn't persist up through the stack what makes return recursiveFunction(args) different?
 
1. It would, except for the fact that you have an if statement to stop it.
if (index > array.length - 1) {
    return sum;
  }
under certain conditions it won't call the recursive function again
instead it returns a values. That value will return and return and return until it gets to the outer most call.
 
we've written you several examples of how to write a recursive function for finding the sum of an array of numbers
the true test (and I'll leave it to you) is to find the sum of a tree of numbers: [1, 2, [3, 4, 5], 6, 7, [8]]
 
2. Calling return inside one function doesn't return the value in the function that called it UNLESS you explicitly tell it to. If you say return recursiveFunction(args) you are saying "return the return value of recursiveFunction(args)". if "recursiveFunction(args)" calls "recursiveFunction(args)" again it will translate to "return the return value of the return value of recursiveFunction(args)". A bit confusing but should make sense.
 
1:24 PM
@Neil I think I understand what you mean by this:
15 mins ago, by Neil
what is the sum of an array of numbers if not the first element plus the sum of the rest?
But if I do the following then in each recursive call sumOfNumbers is reset to the first element and if I put it in a condition statement then sumOfNumbers doesn't exist when the index is not 0:
var sum = 0;

var numbers = [1, 2, 3, 4];

function sumNumbers(array, index) {
  if (index == 0) {
  let sumOfNumbers = array[0];
  }

  if (index > array.length - 1) {
    return sum;

  }

  //sum += array[index];
  sumOfNumbers += array[index];

 //console.log("Each sum variable: " + sum);
  console.log("Each sumOfNumbers variable: " + sumOfNumbers);


  sumNumbers(array, index + 1);
}

console.log("sum of numbers in array: " + sumNumbers(numbers, 0));
console.log("summation variable is: " + sum);
 
@MyWrathAcademia hmm, think of it this way
you have your array and your index. Assuming your index isn't at the end, so you're somewhere in the middle
your task is to return the sum of all elements at your index and beyond
without having to use global variables, you can just return the array[index] + sumNumbers(array, index + 1)
the caller of this function would receive array[index] + sumNumbers(array, index + 1), which if you want to expand it, is really just array[index] + array[index + 1] + sumNumbers(array, index + 2)
etc. until your index is too large, in which case you return 0 (otherwise it would end in error or you'd never stop)
like this, you're still not returning the result of the call
you need to do return array[index] + sumNumbers(array, index + 1)
 
1:46 PM
Have any of you actually used recursion in your work?
ive never experienced a need
 
honestly, no
it comes up for code golf challenges sometimes
but once you realize that you can use a literal stack instead of recursion, recursion is just inefficient
it's great for answering interview questions though :)
 
@JBis I want to check that my understanding of the explanation you gave in your previous comment is correct. Can you confirm whether the following is right or wrong?
@JBis Calling (i.e. executing) return inside one function doesn't return the value in the function that called (i.e. executed) return unless you explicitly tell return to do so. If you say return recursiveFunction(args) you are saying "return the value that was returned by the recursive call recursiveFunctionFunction(args)" (i.e. as you say return the return value of the recursive call recursiveFunction(args)).
@JBis Okay now I think I know how to return the return value of a recursive call. Since there should be only one recursive call I assume that the recursive call should be moved to the conditional statement so that the conditional statement should now be: But now how will the recursive call be executed for indexes < array.length?
 
@MyWrathAcademia sounds good. Do note the value of args should change on the second call.
@MyWrathAcademia I don't understand this.
 
@JBis learning how to code. They talk to developers as part of their jobs so better understanding how coding works is helpful for them.
 
@Neil Yeah. Recursive overhead will always be much larger than iterative.
 
1:57 PM
though that can be optimized by the compiler in some languages
 
@SaraChipps Ah makes sense
@Neil Hmm, how?
 
@JBis QuickSort uses it. And personally .... once or two times I thinl
 
@JBis same way we do it, really
uses a stack and a loop instead of recursive calls
though there are limits to what it can optimize, for instance if you call another method that isn't the recursive method, then it can no longer optimize
 
oh lol so it doesn't do recursion then
 
if it can optimize, no, just loops
 
2:02 PM
tbh recursion is more used in functional programming. If you comprehend it then you have an advantage when writing code in eg Haskell
 
well I think it's definitely important for opening your brain to new paradigms
 
It maybe worth learning eventually but I look at it in terms of priorities. Its taught in most introductory programming courses. Are there more important and useful topics that can be covered instead of recursion? I think so.
Same way I view binary, hexadecimal, and decimal conversions
 
fyi, method calls are recursive
 
@JBis Sorry I rushed my previous comment. I meant:
Okay now I think I know how to return the return value of a recursive call. Since there should be only one recursive call I assume that the recursive call should be moved to the conditional statement so that the conditional statement should now be:
if (index > array.length - 1) {
    //return sum;
    return sumNumbers(array, index + 1);

  }
 
eh no
 
2:09 PM
But now how will the recursive call be executed for indexes < array.length?
 
so close
 
your condition for making a recursive call is "make sure your index is out of range"
but you're right to check before making the recursive call
just make sure there's more work to be done before making a recursive call
 
@Neil @JBis I'm glad you guys have never used recursion in your work. I understand loops intuitively but recursion is a bit confusing so far. It's a lot easier to misunderstand what happens in a recursive algorithm than it is in the same algorithm written using a loop
 
heres the basic format for recursion
function recursiveFunction(args){
	if(finalCase){
		return finalValue;
	}
	return firstVal + recursiveFunction(argsWithoutFirstVal);
}
 
^
that's a decent template
most recursive functions tend to fit nicely with this format
 
2:14 PM
see if you can adapt that template for your use
 
However:
57 mins ago, by Neil
the true test (and I'll leave it to you) is to find the sum of a tree of numbers: [1, 2, [3, 4, 5], 6, 7, [8]]
do this, and I can safely say that you understand recursion
 
who ever is passing that input should be fired
 
Haha @Neil. I'll do that using a loop. I need to understand the simpler example of recursion first
 
function recursiveFunction(args){
	if(args === neilsCrappyInput){
		fireNeil();
	}
	if(finalCase){
		return finalValue;
	}
	return firstVal + recursiveFunction(argsWithoutFirstVal);
}
 
@JBis the + is not part of "basic" format
 
2:19 PM
no, but there's no way to represent that..
 
fair, i guess "some operation" would suffice
 
that's just part of the logic of the recursion that changes
 
Any of yall know how I can format an Input to function like a textarea? multiline?
 
return <some logic combining firstVal and the result of recursive call>;
 
searching multiline input always brings up "textarea"
 
2:21 PM
@TaylorS how about you do the reverse? use textarea with lines="1"
then expand as needed
 
Thats not even close to what I need
dude why
 
.... assuming I know what you need
if you need a input to behave like a textarea, the logical question is, why not use textarea?
 
@TaylorS why?
^^
sounds like an xy to me
 
@JBis I am familiar with that template. I just don't know how to return the value of a function recursively. @JBis what did you mean by:
13 mins ago, by JBis
so close
 
Look at your example and look at the template. Where is the recursive function called?
inside or outside the if?
 
user8729657
2:30 PM
@JBis, How do people monetize selling your information when you visit blogs or other websites?
 
@JBis @Neil @KarelG update:
 
@OvieTrix That's a question with a 12 page essay introduction to the answer
 
So I am getting the sum of numbers of the array using recursion as follows:
 
@MyWrathAcademia If I pass an array of length 0, what gets returned using this condition?
 
@OvieTrix If you'd like an answer, ask a more specific question please.
 
2:33 PM
var sum = 0;

var numbers = [1, 2, 3, 10];

function sumNumbers(array, index) {

  if (index > array.length - 1) {
    return sum; // return finalValue
  }

  sum += array[index];

  console.log("Each sum variable: " + sum);

  return sumNumbers(array, index + 1); // return the return value of each recursive function call
}

console.log("sum of numbers in array: " + sumNumbers(numbers, 0));
console.log("summation variable is: " + sum);
 
Are you asking from a marketing standpoint or from a technical developer standpoint?
@MyWrathAcademia awesome, very close
now we just need to remove the sum
 
@JBis Very close? But it works?
 
I'll give you a hint, you are missing something here:
> return sumNumbers(array, index + 1);
In the template it looks like this:
> return firstVal + recursiveFunction(argsWithoutFirstVal);
So you are missing the firstVal + part
 
@MyWrathAcademia there are many ways to write the same thing in code. You should strive for the ways which emphasize best practice
using a global variable isn't best practice
return sumNumbers(array, index + 1);
This is saying that the result in the call will be the sum
but that would imply that nothing was carried over from your current call
of course that isn't true, you're just using a global variable to hold that information
the result of each call to your recursive call should contribute something new in some form or another
function repeat(str, n) {
    if(n == 0)
        return "";
    return str + repeat(str, n - 1);
}
here's another example. What does this recursive call do?
what is being contributed at each call?
 
2:50 PM
@OvieTrix Fun fact: I know two people who work on the evil side of data monetization. One tries to sell the actual data, and the other figures out how to use that data to better the company. Pretty interesting if you ask me.
 
is that not just regular data science?
😛
 
I think anyone who says data monetization is something different is selling you something
 
@JBis I intentionally didn't use return firstVal + recursiveFunction(argsWithoutFirstVal); because I don't understand how it works. My understanding of how return sum and return sumNumbers(array, index + 1); work (using the function call sumNumbers([1,2,3,4], 0) is:
return sum returns 10 to the bottom (i.e. third) stack then in the current stack return sumNumbers(array, index + 1) returns the return value that was returned by each recursive function call, so in the third stack that statement returns 10, in the second stack it returns 10, in the first stack returns 10. Can you explain the logic behind removing return sum and using return firstVal + recursiveFunction(argsWithoutFirstVal); so that I see how it makes sense?
 
what if you had to calculate the Fibonacci sequence recursively?
 
that's how it gets done o.o
 
3:01 PM
mathematically, it is defined as the previous value summed with the previous previous value
 
calculating it has been a prime example of applying recursion
 
@KarelG the difference is internal vs external data sources
 
I would quote that
> "internal" and "external" yeah
 
@KarelG despite being an awful use of recursion :P
if it's not memoized at least
 
but i'm off. bb
 
3:02 PM
@KarelG huh?
cya o/
@MyWrathAcademia so lets discuss that
 
@KarelG glad you confirmed my understanding is correct
@Neil I've have to read up on the fibonacci sequence
 
Before you go onto fibonacci lets get sum done
 
it's a typical problem solved using recursion
and yes, understand how to get the sum without using global variables before moving on
 
@JBis yes I'm ready to understand how to get the sum without using global variables
 
I'm giving you the solution because you are basically there. We will then try to understand the final change I made.
 
3:16 PM
Sounds good!
 
var numbers = [1, 2, 3, 4];

function sumNumbers(array, index) {

    if (index > array.length - 1) {
        return 0; // return finalValue
    }

    return array[index] + sumNumbers(array, index + 1);
}

console.log("sum of numbers in array: " + sumNumbers(numbers, 0));
 
which is essentially my solution, except there is no need to pass the index, just pass the array you want summed
though admittedly array.slice(1) would have been better than array.splice(1)
 
@MyWrathAcademia Lets breakdown what is occuring
After the first time we run it, fill in the values of the following line:
 return array[index] + sumNumbers(array, index + 1);
what are array[index], array, and index + 1?
 
user8729657
@JBis, I don't understand what kind of information are they selling?
 
Depends on the business
Someone like Google can sell, for starters, your search history pertaining to the product a company is in
 
user8729657
3:22 PM
:O
 
What's the keyword you use in a variable declaration to make it unmodifiable?
 
Lets say I sell shirts. And of the people searching for search 85% of them want blue shirts. I will produce more blue shirts so more people will buy my shirts.
 
@JBis array[index] is 1, array is [1, 2, 3, 4] and index + 1 is 1
 
awesome so we have
 
@Michael It's const
 
3:23 PM
return 1 + sumNumbers([1,2,3,4], 1)
 
@MyWrathAcademia Thanks
 
@Michael no problem.
 
@MyWrathAcademia now do the same thing (find the values of that line) with our new function: sumNumbers([1,2,3,4], 1)
what are array[index], array, and index + 1 for the new function call?
 
@JBis array[index] is 2, array is [1, 2, 3, 4] and index + 1 is 2
 
awesome so now we have
return 1 + (2 + sumNumbers([1,2,3,4], 2))
does that make sense?
 
3:28 PM
Where is the 1 coming from?
 
from the previous call
return 1 + sumNumbers([1, 2, 3, 4], 1)
since we now know that the result of that call is 2 + sumNumbers([1, 2, 3, 4], 2), we can think of the previous call as return 1 + 2 + sumNumbers([1, 2, 3, 4], 2)
 
sumNumbers([1, 2, 3, 4], 1) returns (2 + sumNumbers([1,2,3,4], 2)) so we can just replace it
 
Just to confirm we are not returning at this point, just calling, right?
 
right
it's like saying the weight of a brick is 1 plus half a brick
 
Technically we have not returned anything to the original function yet, no. But this is to help you understand what is happening.
 
3:32 PM
return 1 + sumNumbers([1, 2, 3, 4], 1);
    return 2 + sumNumbers([1, 2, 3, 4], 2);
        return 3 + sumNumbers([1, 2, 3, 4], 3);
            return 4 + sumNumbers([1, 2, 3, 4], 4);
                return 0
therefore if sumNumbers([1, 2, 3, 4], 4) is 0, then the result of the previous call is 4 + 0
and therefore the result of the call before that is 3 + 4 + 0
etc. until you get the initial call of 1 + 2 + 3 + 4 + 0
if you wanted to, the final condition could be index > array.length - 2, and rather than return 0 you return array[index] instead
so you'd then end up with:
return 1 + sumNumbers([1, 2, 3, 4], 1);
    return 2 + sumNumbers([1, 2, 3, 4], 2);
        return 3 + sumNumbers([1, 2, 3, 4], 3);
            return 4;
the result is the same
 
@Neil I'd argue this method is more efficient as multiple arrays don't have to be created and destroyed. Simply add a default value for index as 0 and it works the same.
 
@JBis .slice isn't supposed to create a new array though
which is fine for number arrays
 
Slice creates a new a array
|| mdn slice
 
> array into a new array object
 
3:40 PM
oh
well I suppose that's a good point
then splice would be better
it's modifying the original array
buh, I don't know
 
#micro_optimizations
 
3:57 PM
@JBis @Neil I think I understand. Here I go: 1st stack return 1 + sumNumbers(array, 1);, 2nd stack return 2 + sumNumbers(array, 2), 3rd stack return 3 + sumNumbers(array, 3), 4th (i.e. bottom) stack return 4 + sumNumbers(array, 4)` then at this point if (index > array.length - 1) {return 0; // return finalValue} executes and returns 0 to the 4th stack where array[index] is 4
so 4 + 0 is returned to the 3rd stack where array[index] is 3 so now 3 + (4 + 0) is returned to the 2nd stack where array[index] is 2 so we have 2 + (3 + (4 + 0)) which is returned to the 1st function stack where array[index] is 1 so 1 + (2 + (3 + (4 + 0))) is returned by the 1st function stack to the initial function call outside of the function (or more accurately the sum of 1 + 2 + 3 + 4 + 0 which is 10 is returned to the function call sumNumbers(numbers, 0)).
N.B: values in the parentheses are the values returned from an inner (i.e. lower) stack. And the value of the parameter array is always [1, 2, 3, 4], I just left it out in order to improve clarity.
Typing it all out took some time
 
03:00 - 16:0016:00 - 22:00

« first day (3391 days earlier)      last day (1540 days later) »