Ingolme Posted April 23, 2014 Share Posted April 23, 2014 How to distinguish a recursive function from a non-recursive one? This: function factorial(n){ if ((n == 0) || (n == 1)) return 1; else return (n * factorial(n - 1));} Notice that factorial is the name of the function, but inside the function factorial is used. 1 Link to comment Share on other sites More sharing options...
L8V2L Posted April 23, 2014 Author Share Posted April 23, 2014 Recursive functions (functions that call themselves) have some use, but again they aren't something you are going to need regularly. I never need them in Javascript. I think you are going "down the rabbit hole" with this tutorial. You need to practice writing ordinary, practical functions and not advanced techniques. As you mention above, you are unfamiliar with simple things such as the parse functions, so it does not make much sense for you to be studying closures and OOP.You got it wrong, I'm all about the foundation, I'm going down the list with these tutorial, and going to go back over! A parses function like parsesfloat and parsesInt are two function that parses through a string to look for number. a parsesfloat function look for any number, - + sign, and decimal sign, any thing else, it'll ignore and return everything previses. Example: -132.3423d3244234, from this number it'll return -132.3423. parsesInt only return hold number, so with the same example, it will return only, 132(don't want to look back(cause I'm trying to go off of memorize), but I believe that it's) it convert -132 to 132 or I think integer number also have negative, but isn't saying -10 the same as 0.1. Any way, I'm going through tutorial to for-memorize my self.... What's is advance? Tell me that? What is advance programming? With each tutorial, I make sure it's start at a beginner level, or even stay at a beginner level, don't tell me such a thing(I don't want it to effect me, such words). If a tutorial step over the line of beginner stage, I stop reading it. Did it before. How to distinguish a recursive function from a non-recursive one? This:function factorial(n){ if ((n == 0) || (n == 1)) return 1; else return (n * factorial(n - 1));} Notice that factorial is the name of the function, but inside the function factorial is used.Thank you Ingolme!So a recursive function contain the name of it's function inside the function it self correct? Please answer anyone, and more feedback on that, cause it can't be the simple.(I'm not doubting you, just would love to read more feedback on this.) Link to comment Share on other sites More sharing options...
thescientist Posted April 23, 2014 Share Posted April 23, 2014 a recursive function is just a function that calls itself. because of that, it is important that there is an exit conditional in order to prevent an infinite loop. Link to comment Share on other sites More sharing options...
L8V2L Posted April 23, 2014 Author Share Posted April 23, 2014 Okay... I know the definition... That wasn't the question. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 23, 2014 Share Posted April 23, 2014 mean that it does not need console.log to evoke itNothing needs console.log. The console object and its methods are for debugging, they are not required for anything.So a recursive function contain the name of it's function inside the function it self correct?I'm not sure what you mean by "contains the name", a recursive function is a function that calls (runs, executes, invokes, etc) itself. I don't know how to make that any more clear. Hopefully you understand what calling a function means. It does not mean naming a function. Link to comment Share on other sites More sharing options...
L8V2L Posted April 23, 2014 Author Share Posted April 23, 2014 Nothing needs console.log. The console object and its methods are for debugging, they are not required for anything.I'm not sure what you mean by "contains the name", a recursive function is a function that calls (runs, executes, invokes, etc) itself. I don't know how to make that any more clear. Hopefully you understand what calling a function means. It does not mean naming a function.This is what I mean by contains the name:How to distinguish a recursive function from a non-recursive one?This:function factorial(n){if ((n == 0) || (n == 1))return 1;elsereturn (n * factorial(n - 1));}Notice that factorial is the name of the function, but inside the function factorial is used. Link to comment Share on other sites More sharing options...
thescientist Posted April 23, 2014 Share Posted April 23, 2014 right, and we're just trying to explain to you the correct definition. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 23, 2014 Share Posted April 23, 2014 This is what I mean by contains the name:The definition of a recursive function is not a function that "contains its name", it is a function that calls itself. It runs itself. That is the terminology. It will be less confusing for all of us if we are all using the same terminology. I've never heard a programmer say that a function contains its name or that a variable "grasps" something, that's just not the terminology that programmers use. Link to comment Share on other sites More sharing options...
L8V2L Posted April 23, 2014 Author Share Posted April 23, 2014 (edited) And I'm not talking about the definition, I'm talking about distinction of a recursive function from a regular function... You just gave me the definition, but I ask for how to tell it by looking at it, the definition, just tell it's functionality, but not how to identified it.How do you identified it: How to distinguish a recursive function from a non-recursive one?This:function factorial(n){if ((n == 0) || (n == 1))return 1;elsereturn (n * factorial(n - 1));}Notice that factorial is the name of the function, but inside the function factorial is used.Is this is how you identified it? Yes, or no then so follow up feedback please. Edited April 23, 2014 by L8V2L Link to comment Share on other sites More sharing options...
Ingolme Posted April 23, 2014 Share Posted April 23, 2014 If you understood the definition properly you would have no problem distinguishing a recursive function from a non-recursive one. You don't actually understand the definition. Here's an example of a recursive function that doesn't have the name inside it: function factorial(n) { if(n == 0 || n == 1) { return 1; } else { return n * other(n - 1); }}var other = factorial;alert( factorial(10) ); You need to read about functions again. Read about them in the W3schools tutorial: Functions I and Functions II Read carefully. 1 Link to comment Share on other sites More sharing options...
L8V2L Posted April 24, 2014 Author Share Posted April 24, 2014 (edited) If you understood the definition properly you would have no problem distinguishing a recursive function from a non-recursive one. You don't actually understand the definition. Here's an example of a recursive function that doesn't have the name inside it: function factorial(n) { if(n == 0 || n == 1) { return 1; } else { return n * other(n - 1); }}var other = factorial;alert( factorial(10) );You need to read about functions again. Read about them in the W3schools tutorial: Functions I and Functions IIRead carefully. I did as you said, and it made me went to go over the hold thing... as indicated in my signature which redirect you to my pic, I read over JavaScript tutorial two time, but due to thought roaming, I did not receive every thing I should or would have it if wasn't for my mind drifting off. THANK YOU! After I'm finishing going through developer MDN guide on JavaScript, I will return to either scheme through, or strictly parses through it for more refresh.It said you need an (function(){;})() around the code to evoke it self. Yours don't have that. Edited April 24, 2014 by L8V2L Link to comment Share on other sites More sharing options...
L8V2L Posted April 24, 2014 Author Share Posted April 24, 2014 (edited) If you understood the definition properly you would have no problem distinguishing a recursive function from a non-recursive one. You don't actually understand the definition. Here's an example of a recursive function that doesn't have the name inside it: function factorial(n) { if(n == 0 || n == 1) { return 1; } else { return n * other(n - 1); }}var other = factorial;alert( factorial(10) );You need to read about functions again. Read about them in the W3schools tutorial: Functions I and Functions IIRead carefully. function factorial(n) { if(n == 0 || n == 1) { return 1; } else { return n * other(n - 1);// That's crazy... I through no out side influence can happen in a function?!?! }}var other = factorial; // So how is this possible?!?!?alert( factorial(10) ); Edited April 24, 2014 by L8V2L Link to comment Share on other sites More sharing options...
Ingolme Posted April 24, 2014 Share Posted April 24, 2014 It works because the function has access to the global scope, it's complicated to explain scope and you still don't understand enough about functions yet. If you think you're capable, you can read about scope here: http://javascriptplayground.com/blog/2012/04/javascript-variable-scope-this/ Link to comment Share on other sites More sharing options...
L8V2L Posted April 24, 2014 Author Share Posted April 24, 2014 It works because the function has access to the global scope, it's complicated to explain scope and you still don't understand enough about functions yet. If you think you're capable, you can read about scope here: http://javascriptplayground.com/blog/2012/04/javascript-variable-scope-this/Now that is way to advance for me, I'mma go back to developer MDN and keeping over the basic of JavaScript. But thinks any way. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 24, 2014 Share Posted April 24, 2014 It said you need an (function(){;})() around the code to evoke it self. Yours don't have that.That's because it's not an immediately-invoked function. You only need to use immediately-invoking functions in a few scenarios, like when you want that code to run inside a new scope to avoid any naming conflicts, or as a shortcut when you're trying to set a value that is based on a complex set of rules. Link to comment Share on other sites More sharing options...
L8V2L Posted April 28, 2014 Author Share Posted April 28, 2014 Need help understanding the argument object. This is the argument object: arguments[0]? Give a short easy to read and understandable explanation on this please. It doesn't have to be how I describe it, just please give me your take or point to a site that can give me such easy to receive information…. Or both if you can, your take and a site. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2014 Share Posted April 29, 2014 Arguments is a collection of all of the arguments that were passed to a function, so that you don't need to list them as variables. It is available inside every function. It also has some properties like arguments.caller, which points to the function that called the current function, and arguments.callee, which points to the current function. The arguments.callee property can be used to make an anonymous function recursive.Try this and see what it shows: function test1() { for (var i = 0; i < arguments.length; i++) { console.log('argument ' + i + ' is ' + arguments[i]); }}test1('one', 'two', 'three');It can be useful for doing things like this:function addAll() { var retval = 0; for (var i = 0; i < arguments.length; i++) { retval += arguments[i]; } return retval;}console.log(addAll(1, 2));console.log(addAll(1, 1, 1, 1));console.log(addAll(5));console.log(addAll(1, 2, 3, 4, 5, 6, 7, 8, 9)); https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments 1 Link to comment Share on other sites More sharing options...
L8V2L Posted April 29, 2014 Author Share Posted April 29, 2014 Thanks justsomeguy. Link to comment Share on other sites More sharing options...
L8V2L Posted April 29, 2014 Author Share Posted April 29, 2014 (edited) //The following code creates a two-dimensional array.var a = new Array(4);for (i = 0; i < 4; i++) {a[i] = new Array(4);for (j = 0; j < 4; j++) {a[i][j] = "[" + i + "," + j + "]";}}//This example creates an array with the following rows:Row 0: [0,0] [0,1] [0,2] [0,3]Row 1: [1,0] [1,1] [1,2] [1,3]Row 2: [2,0] [2,1] [2,2] [2,3]Row 3: [3,0] [3,1] [3,2] [3,3]//to my understanding an nested array is:var myarray=[[0,1][2,3]];//Looking at the ouput on the console again, I think I understand it:// output [object Array][Array[4], Array[4], Array[4], Array[4]]// but would still like to have someone explain it.// even know I can now guess that each row is referring to an array in a array of arrays..// but still would like to have someone explain it... please. Edited April 29, 2014 by L8V2L Link to comment Share on other sites More sharing options...
justsomeguy Posted April 29, 2014 Share Posted April 29, 2014 What don't you understand about that? Link to comment Share on other sites More sharing options...
L8V2L Posted April 29, 2014 Author Share Posted April 29, 2014 What don't you understand about that?Just another point of view... or maybe quoting my own answer along side your point of view to know that I'm right. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 30, 2014 Share Posted April 30, 2014 I'm not sure what answer you're referring to. If this is what you're talking about: // even know I can now guess that each row is referring to an array in a array of arrays..Then yes, each row is an array that contains other arrays. Link to comment Share on other sites More sharing options...
L8V2L Posted April 30, 2014 Author Share Posted April 30, 2014 (edited) I'm not sure what answer you're referring to. If this is what you're talking about:Then yes, each row is an array that contains other arrays. //The following code creates a two-dimensional array.var a = new Array(4);for (i = 0; i < 4; i++) {a[i] = new Array(4);for (j = 0; j < 4; j++) {a[i][j] = "[" + i + "," + j + "]";}}//This example creates an array with the following rows:Row 0: [0,0] [0,1] [0,2] [0,3]Row 1: [1,0] [1,1] [1,2] [1,3]Row 2: [2,0] [2,1] [2,2] [2,3]Row 3: [3,0] [3,1] [3,2] [3,3]//to my understanding an nested array is:var myarray=[[0,1][2,3]];//Looking at the ouput on the console again, I think I understand it:output [object Array][Array[4], Array[4], Array[4], Array[4]]I was speaking on the output. each array is an array that is in another away as a group. array a contain four arrays rows with four in each row.//as so:a=[0,0] [0,1] [0,2] [0,3][1,0] [1,1] [1,2] [1,3][2,0] [2,1] [2,2] [2,3][3,0] [3,1] [3,2] [3,3]]sorry I wasn't clear on my explanation.I have another question: Inheritance and the prototype chainI know that's a chapter of it's on. But if you could give me an short visual explanation, like you di with value and variable reference.<!--ignore-->/*//as invar d = new Date(); // d is a Date objectvar e = d; // e is a reference to an objecte.setDate(10); // e is changed// even though only the e variable was changed, both variables show the same value because e is a reference to dconsole.log(d);console.log(e);var a = 10; // a is set to a scalar valuevar b = a; // the value of a is copied to bb++; // b is changedconsole.log(a); // a stays the same because b is not a reference to aconsole.log(; // b is differentvar function e()*/ Edited April 30, 2014 by L8V2L Link to comment Share on other sites More sharing options...
justsomeguy Posted April 30, 2014 Share Posted April 30, 2014 This output:output [object Array][Array[4], Array[4], Array[4], Array[4]]means that there is an array, which contains 4 arrays, and each of those arrays contains 4 elements. I have another question: Inheritance and the prototype chainThat's not a question, but inheritance is one of the major principles of object-oriented programming.http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)In languages that support inheritance, you can create one class that inherits everything from another class. Usually that is called extending. You might have a class that represents a vehicle in general. Then you create other classes that extend the vehicle class, such as a ground vehicle, air vehicle, water vehicle, etc. Then other classes that extend each of those, so a ground vehicle class might be extended by passenger vehicles, commercial vehicles, etc. Down to sedans, coupes, pickup trucks, motorcycles, etc, down to individual vehicles like a 2013 Mercedes C350. The class for that would inherit from every class that it extended, all the way up to the vehicle class. The vehicle class might define properties and methods that are common to all vehicles, like the current speed, direction, methods to start, stop, etc. Those properties and methods would be inherited by anything that extends the class, and each other class can also define their own properties and methods which would get inherited by any other class that extends it.Javascript is a prototype-based object-oriented language. Javascript does not have classes, it has prototypes instead. Instead of creating a new object of a certain class, instead you clone the prototype. Changing the prototype will affect every object that was created from it, all objects inherit the properties and methods of the prototype chain.http://en.wikipedia.org/wiki/Prototype-based_programming 1 Link to comment Share on other sites More sharing options...
L8V2L Posted April 30, 2014 Author Share Posted April 30, 2014 (edited) This output:output [object Array][Array[4], Array[4], Array[4], Array[4]]means that there is an array, which contains 4 arrays, and each of those arrays contains 4 elements.That's not a question, but inheritance is one of the major principles of object-oriented programming.http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)In languages that support inheritance, you can create one class that inherits everything from another class. Usually that is called extending. You might have a class that represents a vehicle in general. Then you create other classes that extend the vehicle class, such as a ground vehicle, air vehicle, water vehicle, etc. Then other classes that extend each of those, so a ground vehicle class might be extended by passenger vehicles, commercial vehicles, etc. Down to sedans, coupes, pickup trucks, motorcycles, etc, down to individual vehicles like a 2013 Mercedes C350. The class for that would inherit from every class that it extended, all the way up to the vehicle class. The vehicle class might define properties and methods that are common to all vehicles, like the current speed, direction, methods to start, stop, etc. Those properties and methods would be inherited by anything that extends the class, and each other class can also define their own properties and methods which would get inherited by any other class that extends it.Javascript is a prototype-based object-oriented language. Javascript does not have classes, it has prototypes instead. Instead of creating a new object of a certain class, instead you clone the prototype. Changing the prototype will affect every object that was created from it, all objects inherit the properties and methods of the prototype chain.http://en.wikipedia.org/wiki/Prototype-based_programmingThanks for the short explanation and the links. I read the introduction to the first one, but no more, since I'm concentrating on JavaScript. var foo = {name: "foo", zero: 0, one: 1};var bar = {one: "one", 1: 1};bar.__proto__ = foo; // this./*out put:[object Object] { [functions]: , 1: 1, __proto__: { }, name: "foo", one: "one", zero: 0 }*/var foo = {name:"foo", one: 3, two: 2};var bar = Object.create(foo); // this.bar.two ="two"; bar[2] = 2;console.log(bar);/*output:[object Object] { [functions]: , 2: 2, __proto__: { }, name: "foo", one: 3, two: "two" }*/var foo = {name:"foo", four: 4, five: 5};var bar = foo; // this.bar.four ="four"; bar[5] = 5;console.log(bar);/*output:[object Object] { [functions]: , 5: 5, __proto__: { }, five: 5, four: "four", name: "foo" }*//*Please explain the 'this.', which showcase the different of instance of an object(which to me is copying... or rather inheriting from another object(call the prototype object... or better term object prototype)). Yes, so what's the different in the instance display. and another that is not show case their but is similar in appearance is(var bar = foo()) and bar.[[prototype]] = foo. Please explain.*/ Edited April 30, 2014 by L8V2L Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now