Jump to content

This is incorrect or... Oh I don't know just take a look at it please.


L8V2L

Recommended Posts

It comes back undefined, in the IE console.

 

You didn't answer my question about return stamen.

Edited by L8V2L
Link to comment
Share on other sites

Yes I know... I did already test it out before I posted the reply. Reread it, I post my founding in it. What I was referring to at the end was the return statement.

Link to comment
Share on other sites

I did not see a correct answer. The correct answer is...

function kryptonite(){var test = 1;// sets a local variable test = 1document.getElementById('out1').innerHTML = 'test 1'; // sets the innerHTML of element id="out1" to "test 1"if(test==='1'){return test}// tests failsdocument.getElementById('out1').innerHTML += 'test 2'; // sets the innerHTML of element id="out1" to "test 1test 2"if(test===1){return test} // returns a value of 1 and exits.//everything else is ignored.}
Link to comment
Share on other sites

/*What's the point of the document get??? I put it in the console and it return nothing. It seem like this wasn't a vialed lesson.  Unless you wanted me to rewrite it so I'll show me the answer in try it.for example:*/function kryptonite(){var test = 1;document.getElementById('out1').innerHTML = 'test 1'; if(test==='1'){document.getElementById("out1").innerHTML=test;}if(test===1){document.getElementById("out1").innerHTML=test;} }/*That's the only I could have seen the result instead of mixing it up with non related matter as you did... Your like the book I'm reading, keep jumping back and forth from beginner to intermediate.*/   
Edited by L8V2L
Link to comment
Share on other sites

The educational approach needs to be more simplistic.

 

You're working with a Javascript console? You can use console.log() to print answers which simplifies the understanding process. It's clear why W3Schools examples use document.write(), trying to use DOM manipulation while teaching Javascript is likely to confuse the user.

 

Indenting the code helps understand things. Every opening curly brace "{" has a matching closing curly brace "}" in the code. All the content between the braces is called a "block" of code. Most control structures can use curly braces and some of them require them.

 

The return statement of a function gives data back to the piece of code that called it, like in the following example:

function five() {    return 5;}console.log( five() ); // Displays "5" in the javascript console

When a function is called, it may sometimes have arguments, as in the following example:

function add(a, B) {    return (a + B);}console.log( add(2, 3) ); // Displays "5" in the Javascript console because 2 + 3 is 5

The for() loop is explained in the W3Schools tutorial.

The *=, +=, ++ and other operators are in the operators section of the tutorial.

Knowing how the for loop works, the following code should be simple to understand:

function power(base, exponent) {    var result = 1;    for(var count = 0; count < exponent; count++) {        result *= base;    } // This curly brace closes the for() loop    return result;} // This curly brace closes the function() definition
  • Like 1
Link to comment
Share on other sites

And the return statement ends the execution of the function even if there might be statements which follow the return statement -- that is the point I'm trying to get across to him.

Link to comment
Share on other sites

 

I'm currently reading this: http://eloquentjavascript.net/2nd_edition/preview/04_data.htmlIt's a good read as it break down the building blocks of JavaScript up to chapter 3 chapter 4 is like the author switch his audience to those who have experience with coding, but not JavaScript.I went to complete it, but not move on if I don't understand one part of it.I'm thinking of go here to continues to studies: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literalsFor it break it done too... even if a little wordy... and more to those who have some knowledge of programming...I want to have a strong foundation of a JavaScript... If you know o f any good book that explains all of JavaScript fundamental in a short read... 15 chapter or so or less, please share.Thanks for the replies and help Ingolme and davej.

 

Link to comment
Share on other sites

If you look at this code and it takes you longer than a couple seconds to know what value the function returns, then you don't understand yet:

function test1() {  var retval = 1;  return 'test1';  for (var i = 1; i < 100; i++) {    retval *= i;  }  return retval;}
Link to comment
Share on other sites

function test1() {  var retval = 1;  return 'test1';  for (var i = 1; i < 100; i++) {    retval *= i;  }  return retval;}// By the look of it, i will increment by 1 until it's 99// retval is the value 1, and will be time by i value// after i under go increment by 1 so... in this sequence retval *=i, it'll go// 1 *= 2, 2*=3, 6*=4, 12*=5, 60*=6, 360*=7, ... and that's all I'm doing by mind// I'm guessing you left out the parameter on purpose, if not, than the code don't // work for I try inputting it (after I figure out what it does of course) in to// the console, as console.log(test1(1)); and it throw or print to the console // test1, put in 2 and it still throw back test1... So I guess I don't know what // it's doing unless you ready did left out a parameter... it also print back undefined//preceding test 1 (above test1)
Link to comment
Share on other sites

You have a misunderstanding of the return statement.

 

Once return is called, the function stops running. Anything after return is like it doesn't exist.

function a() {    return 1;    console.log(5); // This line never gets executed because return leaves the function}
  • Like 1
Link to comment
Share on other sites

That's right, the for loop I added was just to see if you understood what the return statement does. The only thing that function does is define a local variable called retval, which is set to 1, and then it returns the string "test1" and ends. That's all it does, the loop never even runs. If you remove the first return statement, then it would calculate 100 factorial (1 * 2 * 3 * 4 * ... * 99 * 100) and return that. There's no parameter, everything the function uses is defined inside the function.

Link to comment
Share on other sites

Oh... so returns act as ; for the hold function, stopping it... like a break statement.

//What's up with this:var sum = 0;var obj = {prop1: 5, prop2: 13, prop3: 8};for each (var item in obj) {  sum += item;}print(sum); // prints "26", which is 5+13+8 // Console keep saying it's missing a ')'
function test1() {  var retval = 1;  for (var i = 1; i < 100; i++) {    retval *= i;  }  return retval;}console.log(test1(1));//return this 9.33262154439441e+155 no matter what number integer I put in. 
Edited by L8V2L
Link to comment
Share on other sites

That function doesn't take any input parameters.

// played with it, made retval be a parameter.function test1(retval) {  var retval;   for (var i = 1; i < 0; i++) {    retval *= i;  }  return retval;}console.log(test1(9));/*P.S. As I was forward deleting in this post, with the del key, it start creating errors in the console.*/
Link to comment
Share on other sites

Oh... so returns act as ; for the hold function, stopping it... like a break statement.

That's one thing it does, it also returns a value from the function to the caller (hence the name). Since a function can only return one thing, once a value is returned the function ends.
function test1(retval) {  var retval;   for (var i = 1; i < 0; i++) {    retval *= i;  }  return retval;}
That function doesn't do anything, that loop won't run. That function is the same as this, which is pointless:
function test1(retval) {  return retval;}
There's no use in a function that does nothing except return the same thing you pass to it.I did not write that function to illustrate parameters, I wrote it to illustrate the return statement.
Link to comment
Share on other sites

I know, ... it just as you all said, the only way to learn this is to play with it, which I'm I see that now(I like to say starting to see since I'm a novice still). You can't teach no one how to code, just the rules, and practices that been displayed over time as away of using the value to communicate to the console.So the return statement acts as a break, ( ; ), and console.log(in the aspect of it print back to the call, the it's value)...SO return cease the function environment after it return a value which the function either alter or just past to the return through the parameter.So return statement return a value to the caller, and cause the interpreter to continue on to the following code.

Edited by L8V2L
Link to comment
Share on other sites

A better understanding of the throw, try, catch, and finally block plus Error constructor, with the 'name' and 'message' property.

Link to comment
Share on other sites

Try-catch etc. -- this isn't as important in Javascript as it is in most other languages because you can generally avoid most errors. For example the parse functions do not need to be wrapped in a try-catch statement the way they would be in some other languages.

Link to comment
Share on other sites

A function can be recursive; that is, it can call itself. For example, here is a function that computes factorials recursively:

function factorial(n){  if ((n == 0) || (n == 1))    return 1;  else    return (n * factorial(n - 1));}//computing the factorials of one through five as follows:var a, b, c, d, e;a = factorial(1); // a gets the value 1b = factorial(2); // b gets the value 2c = factorial(3); // c gets the value 6d = factorial(4); // d gets the value 24e = factorial(5); // e gets the value 120
What make this a recursive function?And how do one distinct such a function from one that is not?And how would one go about to rewrite such a function?My thought on this(and I'm only typing this so you don't think, I'm just coming to you when I don't understand without trying to understand) is that it can call it self(go figure) mean that it does not need console.log to evoke it.(but please explain as if I had not written this, another perceptive can be insightful). Edited by L8V2L
Link to comment
Share on other sites

Try-catch etc. -- this isn't as important in Javascript as it is in most other languages because you can generally avoid most errors. For example the parse functions do not need to be wrapped in a try-catch statement the way they would be in some other languages.

If I only need what a prase function is... feel free not to tell me if you don't want to, for I am sure I will come to pass it in my studies... But it would be nice to have some prior knowledge of it, so when I do come to it, I'll have more of an easier time to gain the concept of it, and it's use.
Link to comment
Share on other sites

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.

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...