Jump to content

html data-* with js


L8V2L

Recommended Posts

You need to trace the code. Use a pencil and paper if you need to. What are you telling the function to return?

....tracing the code:Check to see if arguments'" property is a numerical value is less than 0...False, proceed to next statement/expressionCheck to see if arguments'" property is a numerical value is equal 0...False, proceed to next statement/expressionReturn statement, evaluate expression, and return the value.... I don't know what happen after that truly. I guess, it'll repeat it self until it equal zero or negative one if I was decrementing by a higher numerical value than one. Then return the value back to the caller, but how? And what of returning one? That's what end the excution is that statement to be true.
Link to comment
Share on other sites

It's probably best to keep your posts a little simpler. I thought you were talking about why this results in a stack overflow:

var bar = function(num){        return (num + arguments.callee(num - 1));}var result = bar(2);console.log(result);//stack exceeded
Apparently you're tracing something completely different.The function you're looking at is overly complex. This does the same thing:
var bar = function(num) {       if(num < 0){    return -1;  }  else if(num === 0){    return 1;  }  else{    return (num * arguments.callee(num - 1));  }}
There's not much point in checking how many arguments were passed if you're only going to use one argument. You can check if the argument is undefined if you want to handle that situation. The switch statement is also a little silly, an if statement would be better if there are only 2 possibilities.In the else case it multiples the argument with the return value from the function being passed the argument minus 1. If you want to see what that does you might want to trace out the complete execution on a piece of paper, starting when num is 3 or 4. It looks like that is a function to calculate a factorial.If you want to know why the first function results in a stack overflow, again, just trace it out.
Link to comment
Share on other sites

If you want to know why the first function results in a stack overflow, again, just trace it out.

Is it cause that the function recursive will never end, unless a condition is meet that will stop the recursive. But as I said again, why doesn't the else if condition return statement value 1 when it is equal to zero?
Link to comment
Share on other sites

Is it cause that the function recursive will never end, unless a condition is meet that will stop the recursive.

Right, that function has no way to stop the recursion. It keeps going until the stack fills up.

But as I said again, why doesn't the else if condition return statement value 1 when it is equal to zero?

It does, that's what it says to do. Run bar(0) and watch it return 1.
Link to comment
Share on other sites

Right, that function has no way to stop the recursion. It keeps going until the stack fills up.It does, that's what it says to do. Run bar(0) and watch it return 1.

I'm speaking when the stack reach a level that doesn't hold true. I speak of... bar(2); it'll call it self two time, in that second call to it self, it no longer hold true against the condition that is tested against it; as it no longer hold turn the condition that test if it's equal to 0 will run it's block of code causing the return statement in that block of code to return 1, where is that one being return??? Where is that one ones the condition no longer hold false, where is that one? That one that is return. Where is it.
Link to comment
Share on other sites

Please still answer my question that is posted above me.---------------------------------------------------------------

//whats the different between this:var foo = function(args){    var i = 0, add = 0;    for(;arguments[i]{         return(add += arguments[i]);          i++;       }};var obj = {"0":1,"1":2,"2":3,"3":4};var bar = foo.bind(obj,obj["0"],obj["1"],obj["2"],obj["3"]);bar(); // 1//And this:var foo = function(args){    var i = 0, add = 0;    for(;arguments[i]{          (add += arguments[i]);          i++;       }   return(add);};var obj = {"0":1,"1":2,"2":3,"3":4};var bar = foo.bind(obj,obj["0"],obj["1"],obj["2"],obj["3"]);//bar(); //10/*is it cause in the first one, after obe itreation, the loop is force to exit do to the return statment, returning only the first numerical value pass to the add variable?*/
Edited by L8V2L
Link to comment
Share on other sites

where is that one being return???

Like any other return value, it gets returned to the code that called it. In the case of a recursive function, it gets returned to the previous function stack. Look at this page, scroll down to linear recursion and look at the green diagram. That is the same function you are using. Notice how it calls itself several times, and then the values get returned back up the scope chain until the last return value.http://www.codeproject.com/Articles/25470/Recursion-Primer-Using-C-Part

is it cause in the first one, after obe itreation, the loop is force to exit do to the return statment, returning only the first numerical value pass to the add variable?

Yes.
  • Like 1
Link to comment
Share on other sites

Can you clarify this:

Similarly, if your HTML document includes an element whose id is x and you also declare and assign a value to the global variable x in your code, the explicitly declared variable will hide the implicit element variable. If the variable is declared in a script that appears before the named element, its existence will prevent the element from getting a window property of its own. And if the variable is declared in a script that appears after the named element, your explicit assignment to the variable overwrites the implicit value of the property.

<!DOCTYPE html><html>    <head>    </head>    <body>        <script id="before">            //var class = 'y'; /*before the named element, its existence will prevent the element from getting a window property of its own*/            /*var x = document.querySelectorAll(".y")[0].innerHTML;*///window.onload = function(){           var clas = 'x';           var x = document.querySelectorAll(".y")[0].innerHTML;           document.getElementById("p_elem").innerHTML = x;}        </script>        <h1 id="x" class="y">            ECMA-Script4Life!        </h1>        <p id="p_elem">           What programming language are you down for?        </p>        <script id="after">            //var id = 'x';/*after the named element, your explicit assignment to the variable overwrites the implicit value of the property*//*            var x = document.querySelector("x");            Var y = document.querySelector("p_elem").innerHTML = x;            */window.onload = function(){           var class = 'x';           var x = document.querySelectorAll(".y")[0].innerHTML;           document.getElementById("p_elem").innerHTML = x;}        </script>    </body></html>
Edited by L8V2L
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...