Jump to content

L8V2L

Recommended Posts

I want to make this:<!DOCTYPE html><html><head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script><script>jQuery(document).ready(function(){function afterText(){var txt1="<b>I </b>"; // Create element with HTMLvar txt2=$("<i></i>").text("love "); // Create with jQueryvar txt3=document.createElement("big"); // Create with DOMtxt3.innerHTML="jQuery!";$("img").after(txt1,txt2,txt3); // Insert new elements after img});});</script></head><body><img src="/images/w3jquery.gif" alt="jQuery" width="100" height="140"><br><br><button onclick="afterText()">Insert after</button></body></html>into a combination of these two:<!DOCTYPE html><html><head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script><script>jQuery(document).ready(function (){jQuery("#appendText").click(function(){var txt1="<p>Text.</p>"; // Create text with HTMLvar txt2=$("<p></p>").text("Text."); // Create text with jQueryvar txt3=document.createElement("p");txt3.innerHTML="Text."; // Create text with DOM$("body").append(txt1,txt2,txt3); // Append new elements});});</script></head><body><p>This is a paragraph.</p><button id="appendText">Append text</button></body></html>and:<!DOCTYPE html><html><head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script><script>function afterText(){var txt1="<b>I </b>"; // Create element with HTMLvar txt2=$("<i></i>").text("love "); // Create with jQueryvar txt3=document.createElement("big"); // Create with DOMtxt3.innerHTML="jQuery!";$("img").after(txt1,txt2,txt3); // Insert new elements after img}</script></head><body><img src="/images/w3jquery.gif" alt="jQuery" width="100" height="140"><br><br><button onclick="afterText()">Insert after</button></body></html>

Link to comment
Share on other sites

What specifically are you trying to accomplish? That code is basically 3 ways to do the same thing.

I found the way! justsomeguy! Since you reply to my stupid question, I'mma email you this too, to make sure you get this! After this you can delete this stupid question, that doesn't supply any directional information to what I'm trying to accomplish.<!DOCTYPE html><html><head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script><script>jQuery(document).ready(function(){jQuery("button").click(function afterText(){var txt1="<b>I </b>"; // Create element with HTMLvar txt2=$("<i></i>").text("love "); // Create with jQueryvar txt3=document.createElement("big"); // Create with DOMtxt3.innerHTML="jQuery!";$("img").after(txt1,txt2,txt3); // Insert new elements after img});});</script></head><body><img src="/images/w3jquery.gif" alt="jQuery" width="100" height="140"><br></br><button onclick="afterText()">Insert after</button></body></html>This is what I was trying to do... I guess... it's the only way it seem to satisfy me... And this wasn't even apart of the tutorial for jQuery which what this is from. Also I read from another tutorial, which I can't seem to get the hang of, is that you can use [] as . syntax notation. WHY DIDN'T THIS SITE GO OVER THAT! NO COOKIE FOR YOU!... reference from a you tuber funny silly guy... Any way MAKE THE TOTURIAL!!!!
Link to comment
Share on other sites

yo, I guest I should explain. I wanted to still keep the function appendText also while having the jQuery(document).ready(function too.

Link to comment
Share on other sites

There are a ton of things about every language that aren't covered in the tutorials. They are just beginner's tutorials, not in-depth discussions of the languages. The people who wrote the tutorials decided how much of each language they wanted to cover in a beginner's tutorial.Look at the PHP reference, for example, look at this page:http://www.w3schools.com/php/default.aspUnder the heading "PHP 5 References", they claim a "complete reference" for "all PHP functions", which is a joke. Look at the number of sections they link to (Array, Calendar, etc). Now look at the actual PHP function reference, see how many sections are there:http://www.php.net/manual/en/funcref.phpIf you're interesting in going farther with Javascript or jQuery, I would recommend getting a book. There are books about both Javascript and jQuery that have more pages than the entire W3Schools website.

Link to comment
Share on other sites

There are a ton of things about every language that aren't covered in the tutorials. They are just beginner's tutorials, not in-depth discussions of the languages. The people who wrote the tutorials decided how much of each language they wanted to cover in a beginner's tutorial.Look at the PHP reference, for example, look at this page:http://www.w3schools.com/php/default.aspUnder the heading "PHP 5 References", they claim a "complete reference" for "all PHP functions", which is a joke. Look at the number of sections they link to (Array, Calendar, etc). Now look at the actual PHP function reference, see how many sections are there:http://www.php.net/manual/en/funcref.phpIf you're interesting in going farther with Javascript or jQuery, I would recommend getting a book. There are books about both Javascript and jQuery that have more pages than the entire W3Schools website.

:(But I like this hand on experience better... Could you tell me other sites that give greater detail but are still for beginner?And books too?If not thanks anyway for replying to my post. Appreciate it!
Link to comment
Share on other sites

I don't know what else to do... Anyone have any ideals? I want to learn JavaScript, I went through this tutorial two times, and have to module on codeacademy left, but it just feel like I'm not advancing. I want to have a strong foundation of the basic, so I can understand everything else.

Link to comment
Share on other sites

you do realize it's not going to happen overnight right? A couple tutorials and some videos is just a drop in the bucket. What you need is to start challenging yourself with actual projects that force you to write code. Be they your own, or for a friend.

Link to comment
Share on other sites

/*A stack is when the computer bring with it the memory of where it stop to back track into a code, and so it throw the last code from which cause it to backtrack on top of the code it self to remember that it stop there to go back, and once the code is done being read an computed, it take the memory of where it left off the top of the block of code and resume from that point. Is that correct? So print(chicken()+" came first."); is thrown on top of the function chicken as a bookmark, and ones it's done going through the function, it take the book mark off the top of the function and consume reading from that point?*/    function chicken() {  return egg();}function egg() {  return chicken();}print(chicken() + " came first.");//And this too:function power(base, exponent){var result = 1;for(var count = 0; count < exponent; count++){result *= base;//}What's the different between putting clury braces bhind this, outputing 1024. And the one underneath?return result;//} Why do result turn 2 when curly braces includ the return result in the for stament.}show(power(2, 10)); 
Link to comment
Share on other sites

/*What's the flow here? What's the route that 24 take? When I exchange it for 2 it return null, even know at that point it see if start is greater than goal, which it isn't if goal is 2 and start is 1. Why is that?*/function findSequence(goal) {  function find(start, history) {    if (start == goal)      return history;    else if (start > goal)      return null;    else      return find(start + 5, "(" + history + " + 5)") ||             find(start * 3, "(" + history + " * 3)");  }  return find(1, "1");}print(findSequence(24));
Edited by L8V2L
Link to comment
Share on other sites

//How is this recursive(repeating over and over)?function power(base, exponent) {  if (exponent == 0)    return 1;  else    return base * power(base, exponent - 1);}console.log(power(2, 3));// → 8
Link to comment
Share on other sites

Recursive means a function that calls itself.

 

This function sums all the numbers from 1 to the number you give it:

function sum(levels) {    return levels + sum(levels - 1);}console.log( sum(5) ); // Shows 15 which is 1 + 2 + 3 + 4 + 5
Link to comment
Share on other sites

Recursive means a function that calls itself. This function sums all the numbers from 1 to the number you give it:

function sum(levels) {    return levels + sum(levels - 1);}console.log( sum(5) ); // Shows 15 which is 1 + 2 + 3 + 4 + 5

 

It's cause stock to over flow.
Link to comment
Share on other sites

Oh, my mistake. Every recursive function needs to have a condition that ends the recursion, which I forgot to add.

function sum(levels) {    if(levels == 1) {        return levels + 1;    } else {        return levels + sum(levels - 1);    }}

To understand recursion you need to try to read the code and write what it's doing on paper to see what's happening. Each time that the function is called, you need to remember that it was so that you can go back to it. The place where this remembering occurs in the computer is called the stack memory.

Link to comment
Share on other sites

function sum(levels) {    if(levels == 1) {        return levels + 1;    } else {        return levels + sum(levels - 1);    }}/*When I put in 1 it give back two, put in three it give back 7... I can't even guess on if I put in 2, cause the simple fact is I can't see the operation. I understand that:*/function sum(levels) { // that console.log(sum(1)); is the same as var levels = 1;     if(levels == 1) { // So if levels === 1        return levels + 1; // it'll add one, and output or console will print out 2.    } else { // if levels don't equal 1 or if this was a else if (levels !== 1)        return levels + sum(levels - 1); // than.. what? levels add to sum(levels - 1)?    } // if I go with that thinking than, putting two in their and it run in their two}// times than levels + sum(1) + levels + sum(1)= 4... I do hope this shows you that I am/* taking this all very serous, despite my hindrance(see signature). And that I do want to learn this. Please, what is the mind set of this, the pattern of thinking. This is appose to imitate human logical to some degree of comprehend.*/   /*The if(levels == 1) Should be if(levels === 1) triple equal sign for the fact that I can simple put in "1" a string value 1, and it will take it, than return it as 11. strangely enough it'll cause the console to return out of stack space, if I do input a string value of 1 in there... You might be thinking what that got to do with what I'm trying to teach you, will if I did not stress on the detail, I could write the same mistake in my program if I don't stricken my JavaScript foundation in the beginning*//*Do you know any material that can help me advance myself in the understanding of this? books, online tutorial, etc. Please, I been non-stop studying JavaScript. Please, I want to release my creativity in this, and make something of myself with this.*/ 
Link to comment
Share on other sites

The result is adding an extra 1 to it because I didn't think out the function well. Explaining why might be a bit complicated unless you understand the function.

 

The key to the recursion is this line:

return levels + sum(levels - 1);

sum() is the function that is currently being executed.

 

First, I'll fix the function to remove the error I had:

function sum(levels) {    if(levels <= 1) {        return levels;    } else {        return levels + sum(levels - 1);    }}console.log( sum(4) );

Now I'll do what's called a "trace". That means I pretend I'm the computer and I follow the instructions

# 8. Call sum() and pass 4 to it# # Inside sum() [level 1]# 1. levels is 4# 2. Is levels less than or equal to 1? It isn't, so go to line 5.# 5. We need to call sum() again, but give it (levels - 1), which is (4 - 1 = 3)#       Store the current value of levels in the stack: stack is [4]# #    Inside sum() [level 2]#    1. levels is 3#    2. Is levels less than or equal to 1? It isn't, so go to line 5.#    5. We need to call sum() again, but give it (levels - 1), which is (3 - 1 = 2)#       Store the current value of levels in the stack: stack is [4, 3]# #       Inside sum() [level 3]#       1. levels is 2#       2. Is levels less than or equal to 1? It isn't, so go to line 5.#       5. We need to call sum() again, but give it (levels - 1), which is (2 - 1 = 1)#          Store the current value of levels in the stack: stack is [4, 3, 2]# #          Inside sum() [level 4]#          1. levels is 1#          2. Is levels less than or equal to 1? Yes, it's 1, so go to line 3.#          3. Return levels, which is 1# #       Inside sum() [level 3]#       5. We have the value of sum()[level 4], it's 1, so add that to levels,#          which is the last element in stack [4, 3, *2*], levels = 2.#          Return (levels + 1) = (2 + 1) which is 3,#          then remove the element from the stack: stack = [4, 3]# #    Inside sum() [level 2]#    5. We have the value of sum()[level 3], it's 3, so add that to levels,#       which is the last element in stack [4, *3*], levels = 3.#       Return (levels + 3) = (3 + 3) which is 6,#       then remove the element from the stack: stack = [4]# # Inside sum() [level 1]# 5. We have the value of sum()[level 2], it's 6, so add that to levels,# which is the last element in stack [4], levels = 4.# Return (levels + 6) = (4 + 6) which is 10,# then remove the element from the stack: stack is empty## 8. Print out the return value of sum(4): 10
  • Like 1
Link to comment
Share on other sites

Please! Run your code through a console first!!! I appreciate the walk through and time to write it for me, but syntax error!!!!!!!!And I try to find out where is the error, but I can't. I though it might be do to your first return not having a ; at the end of the statement it return, but that not it... But it is an error the less, for every statement should end with a ; or I'll just be an expression. Why is their a we in there... We would indicate self awareness and plural, more than one. It should be just "have the value of" in my opinion.... Why? Go ask Neo.

Edited by L8V2L
Link to comment
Share on other sites

//Yes I know. I'm talking about this!1.function sum(levels) {2.    if(levels <= 1) {3.        return levels;4.    } else {5.        return levels + sum(levels - 1);6.    }7.}8.console.log( sum(4) );Expected ';'// This is straight from the console, copy and paste.
Link to comment
Share on other sites

As someone that uses a Gauss Series (the 1+2+3+4+5 summation) in a lot of the math I play with, I can say that theres a simple formula to calculate this Sum. However, I'm sure Ingolme knows this, and he's providing a simple example to help you learn how recursion fundamentally works. And it is a very useful example. I just wanted to give a heads up about that. Recursion does wonders when you need to write code that relies on the result of previous repetitions. It can be an elegant, clean function with only a couple of lines of code. though do beware that it can come with some extra overhead. for example if you enter in a very large number into that function you can easily hit the stack limit. Because the compiler (or rather interpreter as javascript is an interpreted language) can only remember so many function calls currently in the stack at any one time. Allow me to Answer previous questions you had in earlier posts as well. This is a massive text post so to help you digest I'll store these in multiple spoiler tags so you don't feel so overwhelmed:1) The Chicken and the Egg problem you present will simply run in an infinite function loop until again you hit the stack limit trace. but to answer the underlying question, yes the interpreter will remember the functions you called and any following functions called in the chain, or as the common metaphor "Stack". Since this also touches on what I said about Recursion limits I'll go a little more in-depth.

Lets take for example a deck of playing cards. when a function is called you, who is the interpreter in this metaphor, draw a card from the top of the deck and lays it down on top of a new pile called the "stack". When the function is done you take that card from the stack and place it back on top of the deck. If a card on the top of the "stack" calls a function whether it is the same function or not, you draw a new card from the deck and place it on the top of the stack. you can only access the top of the stack, which means you must complete everything in the current function (placing that card back into the the deck) before you can access and continue working on other functions lower in the stack. since a normal deck has 52 cards, you the interpreter can only "remember" upto 52 active functions. if the 52nd function calls another function you'll try to draw from an empty deck and then suddenly you'll probably panic because now you can remember every function that is in mostion. so you simply stop what ever you are doing, throw tanturm at your programmer saying "Stack limit exceeded" or similar lines. after you calm down you'll empty the stack and put all those cards back into the deck and wait for someone to prompt you to work on some more code.

2) the reason the 1st curly brace would give the answer 1024 (which is correct 2^10 is 1024) is that the "return" statement is outside the looping control structure.

 

 

/**for( initialization; condition; iteration){code inside the two curly braces are looped over. The curly braces help define the entire looping structure.}code placed here is considered outside the looping structure and will only be worked on after the loop is done**/

This means it multiplies result by the base and overwrites the old result with the new one. so the looping steps becomeresult = 1*2 // equals 2result = 2*2 // equals 4result = 4*2 // equals 8result = 8*2 // equals 16result = 16*2 // equals 32result = 32*2 // equals 64result = 64*2 // equals 128result = 128*2 // equals 256result = 256*2 // equals 512result = 512*2 // equals 1024 Then the looping finishes and comes across the line "return result" and thus you get the answer 1024. in the second case the loop can't even finish it's 1st pass, in comes acrossresult = 1*2 // equals 2return resultwhen it sees the return command it stops whatever it was doing and returns the value directly following it. even though it was in the middle of a loop the rest of the loop is canceled and the function just gives the first result it came to.

3) the find sequence trys to find the 1st combinations of +5s and *3 that will equal 24, with priority given the the +5. It's kind of hard to give a fully detailed explanation in text so I'll use a binary tree to help illustrate the functions path. Going left down the tree adds 5, while going right multiplies by 3.

 

                (1)                /               (6)                /               (11)                /                (16)  (33)            /       |          (21)  (48) NULL         /       |            (26)  (63) NULL            |     |                   NULL  NULL           (18)                         /                         (23)  (54)                     /       |                   (28)  (69) NULL                   |     |                           NULL  NULL      (3)                                  /                                (8)                                /                              (13) (24) -------> DONE "(((1*3)+5)*3)"                             /                            (18)  (39)                          /       |                       (23)  (54) NULL                       /       |                    (28)  (69) NULL                     |     |                    NULL  NULL

The code runs all the way down the left of the tree until it hit a null then it goes up one down right one, and all the way down the left of that sub tree. it continues this until it finds it's "24", otherwise it exhausts every possible sequence and then returns null. Once it finds it's number, it doesn't even check the rest of the tree. It found the answer that matters and simply uses short-circuiting to stop there.

 

This example applies multiple nuances of a language together, personally I don't think it's intended for a complete beginner to tackle quite yet so if it goes over your head don't sweat it.

 

 

I think all the other open questions have been dealt with so I'll let you sit and ponder of this.

  • Like 1
Link to comment
Share on other sites

If you want to see what the code is doing then add output to show that.

function sum(levels) {  console.log('sum(' + levels + '): entering sum(' + levels + ')');  if(levels <= 1) {    console.log('sum(' + levels + '): returning ' + levels);    return levels;  } else {    console.log('sum(' + levels + '): calculating the sum of ' + levels + ' and sum(' + (levels-1) + ')');    var retval = levels + sum(levels - 1);    console.log('sum(' + levels + '): returning ' + retval);    return retval;  }}console.log( sum(4) );
If you run that, then you will see lines that say it is entering or returning from the function. Every time it enters the function it adds another execution frame on the call stack. Every time it returns, it removes that frame and goes back to the previous one. If you run that then you will see that it calls the function 4 times, adding 4 execution frames to the call stack, and then it returns out of those frames in the opposite order that they were called, getting back to the bottom of the stack. That type of stack is called a FILO stack - first in, last out. The first item on the stack is the last one to be removed.
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...