Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a forum for this book? #2

Open
SOSANA opened this issue Feb 25, 2015 · 10 comments
Open

Is there a forum for this book? #2

SOSANA opened this issue Feb 25, 2015 · 10 comments
Labels

Comments

@SOSANA
Copy link

SOSANA commented Feb 25, 2015

Curious if there is a forum for this book and if that is the proper avenue to ask questions.

Thanks

@bethrobson
Copy link
Owner

Hi Zach,
When you write

name = name + "img/.jpg";

if name is "zero", then you're going to get "zeroimg/.jpg" because the
concatenation is putting together the two strings in the order given.

So to get "img/zero.jpg" you'd need to write:

name = "img/" + name + ".jpg";

Hope that makes sense.

Elisabeth

On Sun, Mar 1, 2015 at 5:32 PM, Zach Sosana notifications@github.com
wrote:

okay here is something I'd like to ask. In the events chapter I put my
img's in a folder but when I try to access the img's so far I put
img/zero.jpg to get access to the .jpg but when I am trying to do this for
the imageMultple.html it will not work. here is my code

<!doctype html>

<title> Image Guess </title> <style> body { margin: 20px; } img { margin: 20px; } </style> <script> window.onload = init;
function init() {
  // now we're getting elements from the page using a tag name, img. This finds every image
  // in the page and returns them all. We store the resulting images in the images variable
  var images = document.getElementsByTagName("img");
  for (var i = 0; i < images.length; i++) {
    // Then we iterate over the images, and assign the showAnswer cick handler to each image
    // in turn. Now the onclick property of each image is set to the showAnswer handler
    images[i].onclick = showAnswer;
  }
 };

function showAnswer(eventObj) {
    var image = eventObj.target;
    var name = image.id;
    name = name + "img/.jpg";
    image.src = name;
}
</script>


Reply to this email directly or view it on GitHub
#2 (comment)
.

@SOSANA
Copy link
Author

SOSANA commented Mar 2, 2015

Oh no accidently deleted last commit. I didn't see this prior but figured it out! Thanks for responding and the explanation. It makes total sense but what was funny is I tried first putting "img/" after name when I reversed them I was grinning from ear to ear :) like the example you shown above.

@SOSANA
Copy link
Author

SOSANA commented Mar 17, 2015

Hi Beth, I was working with the sort method, in the sharpen your pencil excercise it mentions a possible solution that could minimize that amount of code lines. I commented the solution out below.

function compareSold(colaA, colaB) {
// also could use this for compairing numbers
// return colaA - ColaB;
if (colaA.sold > colaB.sold) {
return 1;
} else if (colaA.sold === colaB.sold) {
return 0;
} else {
return -1;
}
}

But I could not get this to work for sorting alphabetically. What is a common solution would you recommend to sorting alphabetically? or would the code above be an okay practice? Obviously the less lines of code the better right?

Another question I had was that the book mentions that the sort method is destrucitive. It changes the array, rather than return a new array. Would the disadvantage be that we are mutating the array rather than returning a new altered array? Is it considered a disadvantage? And if we wanted to return a new one is there another method we would use? like toSource()?

@bethrobson
Copy link
Owner

Hi Zach,
I think you want to write:

colaA.sold - colaB.sold

because you want to return a number. colaA and colaB are objects, so
subtracting them makes no sense.

To sort alphabetically you have to use colaA.name and colaB.name.
JavaScript should do the right thing when comparing the names, although to
be a perfect alphabetic sort, at times you have to write your own! But for
this use case, using > and < on the names will be fine. (Same with color).

There's no disadvantage to sort being destructive as long as you know your
original array is not going to remain the same. If you need to keep the
original array, plus have the sorted one, then the best thing to do is copy
the array. But remember you can't just type

var arrayCopy = myArray;

because arrays are objects, so they are reference variables! If you do the
above, then arrayCopy and myArray will point to the same object.

So you actually need to write a loop and copy each value from myArray into
arrayCopy one at a time. Hope that makes sense.

The only reason you'd need to do this is if you need both the original
ordering as well as the sorted ordering for some reason.

Beth

On Mon, Mar 16, 2015 at 7:32 PM, Zach Sosana notifications@github.com
wrote:

Hi Beth, I was working with the sort method, in the sharpen your pencil
excercise it mentions a possible solution that could minimize that amount
of code lines. I commented the solution out below.

function compareSold(colaA, colaB) {
// also could use this for compairing numbers
// return colaA - ColaB;
if (colaA.sold > colaB.sold) {
return 1;
} else if (colaA.sold === colaB.sold) {
return 0;
} else {
return -1;
}
}

But I could not get this to work for sorting alphabetically. What is a
common solution would you recommend to sorting alphabetically? or would the
code above be an okay practice? Obviously the less lines of code the better
right?

Another question I had was that the book mentions that the sort method is
destrucitive. It changes the array, rather than return a new array. Would
the disadvantage be that we are mutating the array rather than returning a
new altered array? Is it considered a disadvantage? And if we wanted to
return a new one is there another method we would use? like toSource()?


Reply to this email directly or view it on GitHub
#2 (comment)
.

@SOSANA
Copy link
Author

SOSANA commented Mar 17, 2015

Thank you so much for taking the time to respond and answer. This makes sense now.

@SOSANA
Copy link
Author

SOSANA commented Mar 19, 2015

back again :)

I am trying to make sure I have a firm grasp on how nested functions affects scopes.

In the flyAndQuack example we have two functions with nested funcitons.

The fly function is a function expression which will will be evaluated during the second pass when the broswer is executing the code at run time. While the function quack is a declaration function which is parsed first before being evaluated at run time. Inside the quack function there is a function expression which if I understand this will not be evaluated within the scope when the browser goes through the first pass. In the second pass, is when the browser will excute the function expression nested within the quack.

Hoping this is the right place to ask questions :)

@bethrobson
Copy link
Owner

Hi Zach,
Yes this is fine - it's not really an issue with the code itself, but "issue" extends to questions too, and answers can possibly help others, so nice to make them public.

Regarding the nested functions (that is, wingFlapper and quacker):

  1. just like at the top level, wingFlapper is defined before the code in fly is executed. So you can think of the code in fly being done in two passes, just like at the top level: you call fly(4), and first wingFlapper is defined, and then the rest of the code starts executing. (Technically, the browser's parser may do this earlier, during the initial pass when the code is loaded, but from the execution flow standpoint, that doesn't matter too much). Key point to remember: wingFlapper is local to fly, so you cannot refer to wingFlapper outside of fly. However, wingFlapper is defined everywhere in fly, so you could call wingFlapper above where you define it if you want - try moving the for loop above the function declaration to see this work.

  2. Just like at the top level, quacker is not defined until the code in quack executes (the second pass, as you said). So, if you moved the for loop above where you defined quacker you'll get an error. Try it and see! Just like with wingFlapper, quacker is local to quack, so you can't refer to quacker outside of quack.

Think of function expressions as just like regular variables, and function declarations as a bit special because they are defined everywhere in their scope.

Hope that helps.

@SOSANA
Copy link
Author

SOSANA commented Mar 19, 2015

Hi Beth,

is it possible we could create a tag for questions? That way future students could post under this tag for future questions or search through them.

If I understand this, I thought that since fly was a expression everythiing nested inside would be ignored during the first pass but if I read your comments correctly the browser parser may do this during the first pass but not executed till the second pass do to fly being a expression function.

What I was trying to make sure is my understanding of what will be excuted during the first pass and what will be executed during the second pass for nested functions. I found it interesting that the quack function will have two passes, the first pass which sees its a declaration, but than notices the expression and ignores it causing it to have two passes. This has to have a disadvanage of some sort in the future? the fact it takes two passes for execution.

I found some interesting conclusions during my QA. One was changing the for loop above as you mentioned but what really clicked was when I changed quacker from an expression to a declaration inside of the function quack. I noticed that it was executed before the fly function was called. This lead me to believe even though the quack is a delaration, that if you have a nested expression it will be ignored but if it was a declartion inside of it, it would be executed during the first pass and also would not have to have two passes.

I could of sworn there was a reason for not doing declaration over expression mentioned earlier in the book, but need to double check my notes from earlier chapters. I also really enjoyed your concrete example tracing through invoking functions. Light bulb went off and was like now this makes total sense!

@bethrobson
Copy link
Owner

Keep in mind there is a distinction between parsing the function - that is,
seeing that it's there and defining the function value - and executing the
function, which is when the code inside it is run.

Either a declaration or expression is fine as long as you understand where
each function type is defined. At the global level, a function declaration
is defined everywhere, so you can use it above where you define it. At the
global level a function expression is defined only when you get to that
line of code, so you can only use it after the function expression is
defined. Same with nested functions, only the scope of the definitions is
limited to the function containing the nested functions.

On Thu, Mar 19, 2015 at 11:49 AM, Zach Sosana notifications@github.com
wrote:

Hi Beth,

is it possible we could create a tag for questions? That way future
students could post under this tag for future questions or search through
them.

What I was trying to make sure is my understanding of what will be excuted
during the first pass and what will be executed during the second pass for
nested functions. I found it interesting that the quack function will have
two passes, the first pass which sees its a declaration, but than notices
the expression and ignores it causing it to have two passes. This has to
have a disadvanage of some sort in the future?

I found some interesting conclusions during my QA. One was changing the
for loop above as you mentioned but what really clicked was when I changed
quacker from an expression to a declaration inside of the function quack. I
noticed that it was executed before the fly function was called. This lead
me to believe even though the quack is a delaration, that if you have a
nested expression it will be ignored but if it was a declartion inside of
it, it would be executed during the first pass and also would not have to
have two passes.

I could of sworn there was a reason for not doing declaration over
expression mentioned earlier in the book, but need to double check my notes
from earlier chapters. I also really enjoyed your concrete example tracing
through invoking functions. Light bulb went off and was like now this makes
total sense!


Reply to this email directly or view it on GitHub
#2 (comment)
.

@SOSANA
Copy link
Author

SOSANA commented Mar 24, 2015

Hi Beth,

Don't have access to update this thread to be labeled under question. Can you tag this under the questions label?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants