This is the P2PU Archive. If you want the current site, go to!

Javascript: 101

My recent threads

You haven't posted any discussions yet.

Recently updated threads

Week 1 Questions and Assignment Retrospective

Go back to: General discussion

Please read these blog posts for good answers to the questions posted in week 1.


I am not posting any specific posts for the assignments, becasue it is very easy to verify whether the code is working or not, just by looking at the output. If anyone has doubts or questions about the code, coding style, etc, for week 1, then please discuss them here.

Jason Nyquist's picture
Jason Nyquist
Wed, 2011-02-02 03:38

Wow, nice blog posts!

Now a question. For exercise 2.5 I wrote

var x = prompt("Hey mr.smartyPants, What is 2+2?", "");
if (x == 4)
   alert("You said " + x + "?!  WoW!  Amazing Even!!");
else if(x == 3 || x == 5)
   alert("Baaaka!  <- means stupid, stupid!");

Note that I left x == 3,4,5 as numbers not strings (no “” around them) to leave open the possibility for a user to enter 3+1 or sqrt(sqrt(256)) and still be amazing. But prompt data output seems to be of type string, so an amazing person can't be.

Can anyone tell me how to make the output of the prompt a number/function/object/boolean…?


Alex Mrvaljevich's picture
Alex Mrvaljevich
Wed, 2011-02-02 04:06


A bit earlier in the chapter there is an example in the Eloquent JS book that uses "Number" to convert the input from the prompt into one, you can do it like so:

answer = Number(prompt("2 + 2 is...?", "you can do it"));



Parag Shah's picture
Parag Shah
Wed, 2011-02-02 06:24

Hi Alex,

I tried that. In the prompt I entered '5-1', and I got a NaN. I think Javascript can only convert a number from String into a Number. If it is an expression, then I (I believe) Javascript will return a NaN.

Parag Shah's picture
Parag Shah
Wed, 2011-02-02 06:27

Hi Jason,

The output of the prompt will always be a String. However if the String represents a number, then we can convert it into a Number using the Number(), parseInt() functions.

However if the output of prompt is "5-1", then Javascript will not be able to convert it into a Number. We will either have to manually parse that expression, or perhaps try to use a library which will help us do it.

Jason Nyquist's picture
Jason Nyquist
Wed, 2011-02-02 08:46

Yup, had tried the Number() and parseInt() methods out, but like you said, they fail. To parse it manually and/or to use a library seems like a bit of a step for now. Thanks though - good first week!

Amene Katanda's picture
Amene Katanda
Wed, 2011-02-02 09:53

I guess you can force it to be a number with parseInt() function. You get something like:
x = parseInt(x);

Nathanael  Potoski's picture
Nathanael Potoski
Thu, 2011-02-03 16:44

Is anyone else having trouble with the Javascript bookmarklets? I tried typing up one of the code examples from chapter 2 of Eloquent Javascript and I got an error report for the first line. I checked it a few times and there are no typos or spacing errors. Solutions?
var result = 1;
var counter = 0;
while (counter < 10)
result = result * 2;
counter = counter + 1;

Parag Shah's picture
Parag Shah
Thu, 2011-02-03 18:58

Hi Nathanael,

I can run the above example in Eloquent Javascript's console. Are you trying to run it as a Javascript bookmarklet? It will not work as a Javascript bookmarklet due to the 'show()' function call, which is available only in Eloquent Javascript.

Nathanael  Potoski's picture
Nathanael Potoski
Thu, 2011-02-03 21:50

Oh, ok. That works. Thank you very much. =)Still trying to catch up here.

Eric Chapman's picture
Eric Chapman
Sat, 2011-02-05 10:29

I'm doing track 2, using the book, and I was handling it quite well until I got to exercise 3.2. That one baffles the hell out of me, as do the concepts covered by it.

I get as far as this:
function greaterThan(num1) {
function test(num2){
return num2 > num1;

That much is described by the book, literally, but I do not understand how and where the value for 'num2' comes from unless I specify it explicitly myself.. but doesn't that completely obliterate the reason for nesting one function inside another?

I looked at the solution and I just don't get it. It looks like I coded the first part right, but I don't understand what's going on with the last couple lines. Any help, even just a pointer to a fellow student's blog who described it very well, would be appreciated.

dysert's picture
Sat, 2011-02-05 13:38

You forgot the keyword "return" before the definition of function 'test'. It should look like this:

function greaterThan(num1) {
return function test(num2) {
return num2 > num1;

So when you invoke the 'greaterThan' function, it doesn't return a value per se but instead returns a pointer to the function 'test'. (Kind of like a delegate in C#)

So invoking the 'greaterThan' function like this:
var gt5 = greatThan(5);

makes the 'gt5' variable become a call to your 'test' function, which you can then invoke via:
print(gt5(4)); // returns false
print(gt5(6)); // returns true


Eric Chapman's picture
Eric Chapman
Sat, 2011-02-05 18:22

I havn't programmed in C#.. the only modern programming language I have a handle on is PHP, and I've never done recursion or nested functions there, either.

I think I might be getting it now, though.

Okay, so, when you call:

var gt5 = greaterThan(5);

it's as if you're creating a function that would be written, by itself, like this:

function gt5(num2){
    return num2 > 5;

and that's why you call it like:


And that's where that num2 value comes from.. by returning test function into that gt4 variable, we created a variable that accepts an argument (because the variable holds a function)?

Does that sound right?

If so then now I understand how it works.. I just need to figure out when I should be using it. :)

dysert's picture
Sat, 2011-02-05 19:05

Your explanation sounds right to me. Now, as to when you'd use such a thing I don't know. Maybe when you need to pass the handle of a function around -- like to an event handler. I too am curious to see when such a construct might be necessary.

Nathanael  Potoski's picture
Nathanael Potoski
Sun, 2011-02-06 04:30

Here's my blog. I'm not sure if I have everything in there cause all of the information is spread out all over the place for this course. So if anyone knows I need to add more, let me know so I can update it. thanks. =)