0

I'm trying to learn basic javascript. I've created this jsfiddle: http://jsfiddle.net/Friar_Broccoli/b2gur/

function useless(callback) { return callback(); }
var text = 'Domo arigato!';
assert(useless(function(){ return text; }) === text,
"The useless function works! " + text);

which is straight out of page 37 of: http://netcraft.co.il/fedia/books/SecretsoftheJavaScriptNinja.pdf

It does NOTHING, and if I add:

document.writeln(text);

it works only if I place it immediately after the "var text = .." declaration. Not the first time I've had this type of problem, although I sometimes succeed in getting javascript functions to work properly.

So (1) Why does nothing work after the assert() call? (2) How can I make it work? (3) Is there somewhere I can find a for-morons explanation of how to organize code in a *.js file?

Thanks;

  • 6
    assert is not a JS function – koningdavid Feb 04 '14 at 19:26
  • Are you including Resig's testing library from the book, where he defines the assert() function? – Russell Zahniser Feb 04 '14 at 19:27
  • @Russel: Not in the demo fiddle, at least. He should have though… – Bergi Feb 04 '14 at 19:28
  • ^^ "No" - Friar: that function is not a native javascript thing, but he made it a new function in his code somewhere. jsfiddle has no idea what the function could be, though! – Stachu Feb 04 '14 at 19:28
  • 1
    I recommend http://eloquentjavascript.net/contents.html for learning JS, its free and very good – koningdavid Feb 04 '14 at 19:29
  • 1
    If you open your javascript console (F12 in FF/Chrome), you'll see that there's an error : `ReferenceError: assert is not defined`. ;) –  Feb 04 '14 at 19:29
  • 2
    I wouldn't recommend this book to someone just learning JS anyway. it talks more about advanced uses of functions/objects and whatnot. It is definitely something worthwhile after you learn the basics. – Jeff Shaver Feb 04 '14 at 19:29
  • 1
    You shouldn't use a book called "Secrets of a (insert skill here) Ninja" for learning anything basic. – Dexygen Feb 04 '14 at 19:29
  • There's no `assert` in Javascript. see question [Javascript - assert?](http://stackoverflow.com/questions/15313418/javascript-assert). Or you need to use other assertion frameworks like [chai](http://chaijs.com/) – shawnzhu Feb 04 '14 at 19:33

3 Answers3

0

Your fiddle has no defined "assert" method.

Xavier J
  • 4,326
  • 1
  • 14
  • 25
0

1) There is no assert() function, so the code fails and doesn't do the rest. If you put an output right after " var text = 'Domo arigato!'; ", it works only because it simply manages to get up to that point without an error.

2) You need to define your own assert function, something like :

function assert(condition,okMessage,failMessage){
    if(condition) document.writeln(okMessage);
    else document.writeln(failMessage);
}

3) There is no problem with your code organization.

ElDoRado1239
  • 3,938
  • 2
  • 16
  • 13
-3

This is a good read for you :

http://www.w3schools.com/js/js_functions.asp

functions don't start themself, you need to start them with an onload / onclick / etc event.

  • It's not straightforward because of the OP's formatting but the example WOULD call the function shown if there were a proper 'assert' method defined. – Xavier J Feb 04 '14 at 20:28