diff --git a/jskoans.htm b/jskoans.htm index 5113fe4e..41ad34f9 100644 --- a/jskoans.htm +++ b/jskoans.htm @@ -5,11 +5,13 @@ + + diff --git a/support/underscore-min.js b/support/underscore-min.js new file mode 100644 index 00000000..3434d6c5 --- /dev/null +++ b/support/underscore-min.js @@ -0,0 +1,6 @@ +// Underscore.js 1.6.0 +// http://underscorejs.org +// (c) 2009-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors +// Underscore may be freely distributed under the MIT license. +(function(){var n=this,t=n._,r={},e=Array.prototype,u=Object.prototype,i=Function.prototype,a=e.push,o=e.slice,c=e.concat,l=u.toString,f=u.hasOwnProperty,s=e.forEach,p=e.map,h=e.reduce,v=e.reduceRight,g=e.filter,d=e.every,m=e.some,y=e.indexOf,b=e.lastIndexOf,x=Array.isArray,w=Object.keys,_=i.bind,j=function(n){return n instanceof j?n:this instanceof j?void(this._wrapped=n):new j(n)};"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=j),exports._=j):n._=j,j.VERSION="1.6.0";var A=j.each=j.forEach=function(n,t,e){if(null==n)return n;if(s&&n.forEach===s)n.forEach(t,e);else if(n.length===+n.length){for(var u=0,i=n.length;i>u;u++)if(t.call(e,n[u],u,n)===r)return}else for(var a=j.keys(n),u=0,i=a.length;i>u;u++)if(t.call(e,n[a[u]],a[u],n)===r)return;return n};j.map=j.collect=function(n,t,r){var e=[];return null==n?e:p&&n.map===p?n.map(t,r):(A(n,function(n,u,i){e.push(t.call(r,n,u,i))}),e)};var O="Reduce of empty array with no initial value";j.reduce=j.foldl=j.inject=function(n,t,r,e){var u=arguments.length>2;if(null==n&&(n=[]),h&&n.reduce===h)return e&&(t=j.bind(t,e)),u?n.reduce(t,r):n.reduce(t);if(A(n,function(n,i,a){u?r=t.call(e,r,n,i,a):(r=n,u=!0)}),!u)throw new TypeError(O);return r},j.reduceRight=j.foldr=function(n,t,r,e){var u=arguments.length>2;if(null==n&&(n=[]),v&&n.reduceRight===v)return e&&(t=j.bind(t,e)),u?n.reduceRight(t,r):n.reduceRight(t);var i=n.length;if(i!==+i){var a=j.keys(n);i=a.length}if(A(n,function(o,c,l){c=a?a[--i]:--i,u?r=t.call(e,r,n[c],c,l):(r=n[c],u=!0)}),!u)throw new TypeError(O);return r},j.find=j.detect=function(n,t,r){var e;return k(n,function(n,u,i){return t.call(r,n,u,i)?(e=n,!0):void 0}),e},j.filter=j.select=function(n,t,r){var e=[];return null==n?e:g&&n.filter===g?n.filter(t,r):(A(n,function(n,u,i){t.call(r,n,u,i)&&e.push(n)}),e)},j.reject=function(n,t,r){return j.filter(n,function(n,e,u){return!t.call(r,n,e,u)},r)},j.every=j.all=function(n,t,e){t||(t=j.identity);var u=!0;return null==n?u:d&&n.every===d?n.every(t,e):(A(n,function(n,i,a){return(u=u&&t.call(e,n,i,a))?void 0:r}),!!u)};var k=j.some=j.any=function(n,t,e){t||(t=j.identity);var u=!1;return null==n?u:m&&n.some===m?n.some(t,e):(A(n,function(n,i,a){return u||(u=t.call(e,n,i,a))?r:void 0}),!!u)};j.contains=j.include=function(n,t){return null==n?!1:y&&n.indexOf===y?n.indexOf(t)!=-1:k(n,function(n){return n===t})},j.invoke=function(n,t){var r=o.call(arguments,2),e=j.isFunction(t);return j.map(n,function(n){return(e?t:n[t]).apply(n,r)})},j.pluck=function(n,t){return j.map(n,j.property(t))},j.where=function(n,t){return j.filter(n,j.matches(t))},j.findWhere=function(n,t){return j.find(n,j.matches(t))},j.max=function(n,t,r){if(!t&&j.isArray(n)&&n[0]===+n[0]&&n.length<65535)return Math.max.apply(Math,n);var e=-1/0,u=-1/0;return A(n,function(n,i,a){var o=t?t.call(r,n,i,a):n;o>u&&(e=n,u=o)}),e},j.min=function(n,t,r){if(!t&&j.isArray(n)&&n[0]===+n[0]&&n.length<65535)return Math.min.apply(Math,n);var e=1/0,u=1/0;return A(n,function(n,i,a){var o=t?t.call(r,n,i,a):n;u>o&&(e=n,u=o)}),e},j.shuffle=function(n){var t,r=0,e=[];return A(n,function(n){t=j.random(r++),e[r-1]=e[t],e[t]=n}),e},j.sample=function(n,t,r){return null==t||r?(n.length!==+n.length&&(n=j.values(n)),n[j.random(n.length-1)]):j.shuffle(n).slice(0,Math.max(0,t))};var E=function(n){return null==n?j.identity:j.isFunction(n)?n:j.property(n)};j.sortBy=function(n,t,r){return t=E(t),j.pluck(j.map(n,function(n,e,u){return{value:n,index:e,criteria:t.call(r,n,e,u)}}).sort(function(n,t){var r=n.criteria,e=t.criteria;if(r!==e){if(r>e||r===void 0)return 1;if(e>r||e===void 0)return-1}return n.index-t.index}),"value")};var F=function(n){return function(t,r,e){var u={};return r=E(r),A(t,function(i,a){var o=r.call(e,i,a,t);n(u,o,i)}),u}};j.groupBy=F(function(n,t,r){j.has(n,t)?n[t].push(r):n[t]=[r]}),j.indexBy=F(function(n,t,r){n[t]=r}),j.countBy=F(function(n,t){j.has(n,t)?n[t]++:n[t]=1}),j.sortedIndex=function(n,t,r,e){r=E(r);for(var u=r.call(e,t),i=0,a=n.length;a>i;){var o=i+a>>>1;r.call(e,n[o])t?[]:o.call(n,0,t)},j.initial=function(n,t,r){return o.call(n,0,n.length-(null==t||r?1:t))},j.last=function(n,t,r){return null==n?void 0:null==t||r?n[n.length-1]:o.call(n,Math.max(n.length-t,0))},j.rest=j.tail=j.drop=function(n,t,r){return o.call(n,null==t||r?1:t)},j.compact=function(n){return j.filter(n,j.identity)};var M=function(n,t,r){return t&&j.every(n,j.isArray)?c.apply(r,n):(A(n,function(n){j.isArray(n)||j.isArguments(n)?t?a.apply(r,n):M(n,t,r):r.push(n)}),r)};j.flatten=function(n,t){return M(n,t,[])},j.without=function(n){return j.difference(n,o.call(arguments,1))},j.partition=function(n,t){var r=[],e=[];return A(n,function(n){(t(n)?r:e).push(n)}),[r,e]},j.uniq=j.unique=function(n,t,r,e){j.isFunction(t)&&(e=r,r=t,t=!1);var u=r?j.map(n,r,e):n,i=[],a=[];return A(u,function(r,e){(t?e&&a[a.length-1]===r:j.contains(a,r))||(a.push(r),i.push(n[e]))}),i},j.union=function(){return j.uniq(j.flatten(arguments,!0))},j.intersection=function(n){var t=o.call(arguments,1);return j.filter(j.uniq(n),function(n){return j.every(t,function(t){return j.contains(t,n)})})},j.difference=function(n){var t=c.apply(e,o.call(arguments,1));return j.filter(n,function(n){return!j.contains(t,n)})},j.zip=function(){for(var n=j.max(j.pluck(arguments,"length").concat(0)),t=new Array(n),r=0;n>r;r++)t[r]=j.pluck(arguments,""+r);return t},j.object=function(n,t){if(null==n)return{};for(var r={},e=0,u=n.length;u>e;e++)t?r[n[e]]=t[e]:r[n[e][0]]=n[e][1];return r},j.indexOf=function(n,t,r){if(null==n)return-1;var e=0,u=n.length;if(r){if("number"!=typeof r)return e=j.sortedIndex(n,t),n[e]===t?e:-1;e=0>r?Math.max(0,u+r):r}if(y&&n.indexOf===y)return n.indexOf(t,r);for(;u>e;e++)if(n[e]===t)return e;return-1},j.lastIndexOf=function(n,t,r){if(null==n)return-1;var e=null!=r;if(b&&n.lastIndexOf===b)return e?n.lastIndexOf(t,r):n.lastIndexOf(t);for(var u=e?r:n.length;u--;)if(n[u]===t)return u;return-1},j.range=function(n,t,r){arguments.length<=1&&(t=n||0,n=0),r=arguments[2]||1;for(var e=Math.max(Math.ceil((t-n)/r),0),u=0,i=new Array(e);e>u;)i[u++]=n,n+=r;return i};var R=function(){};j.bind=function(n,t){var r,e;if(_&&n.bind===_)return _.apply(n,o.call(arguments,1));if(!j.isFunction(n))throw new TypeError;return r=o.call(arguments,2),e=function(){if(!(this instanceof e))return n.apply(t,r.concat(o.call(arguments)));R.prototype=n.prototype;var u=new R;R.prototype=null;var i=n.apply(u,r.concat(o.call(arguments)));return Object(i)===i?i:u}},j.partial=function(n){var t=o.call(arguments,1);return function(){for(var r=0,e=t.slice(),u=0,i=e.length;i>u;u++)e[u]===j&&(e[u]=arguments[r++]);for(;r=f?(clearTimeout(a),a=null,o=l,i=n.apply(e,u),e=u=null):a||r.trailing===!1||(a=setTimeout(c,f)),i}},j.debounce=function(n,t,r){var e,u,i,a,o,c=function(){var l=j.now()-a;t>l?e=setTimeout(c,t-l):(e=null,r||(o=n.apply(i,u),i=u=null))};return function(){i=this,u=arguments,a=j.now();var l=r&&!e;return e||(e=setTimeout(c,t)),l&&(o=n.apply(i,u),i=u=null),o}},j.once=function(n){var t,r=!1;return function(){return r?t:(r=!0,t=n.apply(this,arguments),n=null,t)}},j.wrap=function(n,t){return j.partial(t,n)},j.compose=function(){var n=arguments;return function(){for(var t=arguments,r=n.length-1;r>=0;r--)t=[n[r].apply(this,t)];return t[0]}},j.after=function(n,t){return function(){return--n<1?t.apply(this,arguments):void 0}},j.keys=function(n){if(!j.isObject(n))return[];if(w)return w(n);var t=[];for(var r in n)j.has(n,r)&&t.push(r);return t},j.values=function(n){for(var t=j.keys(n),r=t.length,e=new Array(r),u=0;r>u;u++)e[u]=n[t[u]];return e},j.pairs=function(n){for(var t=j.keys(n),r=t.length,e=new Array(r),u=0;r>u;u++)e[u]=[t[u],n[t[u]]];return e},j.invert=function(n){for(var t={},r=j.keys(n),e=0,u=r.length;u>e;e++)t[n[r[e]]]=r[e];return t},j.functions=j.methods=function(n){var t=[];for(var r in n)j.isFunction(n[r])&&t.push(r);return t.sort()},j.extend=function(n){return A(o.call(arguments,1),function(t){if(t)for(var r in t)n[r]=t[r]}),n},j.pick=function(n){var t={},r=c.apply(e,o.call(arguments,1));return A(r,function(r){r in n&&(t[r]=n[r])}),t},j.omit=function(n){var t={},r=c.apply(e,o.call(arguments,1));for(var u in n)j.contains(r,u)||(t[u]=n[u]);return t},j.defaults=function(n){return A(o.call(arguments,1),function(t){if(t)for(var r in t)n[r]===void 0&&(n[r]=t[r])}),n},j.clone=function(n){return j.isObject(n)?j.isArray(n)?n.slice():j.extend({},n):n},j.tap=function(n,t){return t(n),n};var S=function(n,t,r,e){if(n===t)return 0!==n||1/n==1/t;if(null==n||null==t)return n===t;n instanceof j&&(n=n._wrapped),t instanceof j&&(t=t._wrapped);var u=l.call(n);if(u!=l.call(t))return!1;switch(u){case"[object String]":return n==String(t);case"[object Number]":return n!=+n?t!=+t:0==n?1/n==1/t:n==+t;case"[object Date]":case"[object Boolean]":return+n==+t;case"[object RegExp]":return n.source==t.source&&n.global==t.global&&n.multiline==t.multiline&&n.ignoreCase==t.ignoreCase}if("object"!=typeof n||"object"!=typeof t)return!1;for(var i=r.length;i--;)if(r[i]==n)return e[i]==t;var a=n.constructor,o=t.constructor;if(a!==o&&!(j.isFunction(a)&&a instanceof a&&j.isFunction(o)&&o instanceof o)&&"constructor"in n&&"constructor"in t)return!1;r.push(n),e.push(t);var c=0,f=!0;if("[object Array]"==u){if(c=n.length,f=c==t.length)for(;c--&&(f=S(n[c],t[c],r,e)););}else{for(var s in n)if(j.has(n,s)&&(c++,!(f=j.has(t,s)&&S(n[s],t[s],r,e))))break;if(f){for(s in t)if(j.has(t,s)&&!c--)break;f=!c}}return r.pop(),e.pop(),f};j.isEqual=function(n,t){return S(n,t,[],[])},j.isEmpty=function(n){if(null==n)return!0;if(j.isArray(n)||j.isString(n))return 0===n.length;for(var t in n)if(j.has(n,t))return!1;return!0},j.isElement=function(n){return!(!n||1!==n.nodeType)},j.isArray=x||function(n){return"[object Array]"==l.call(n)},j.isObject=function(n){return n===Object(n)},A(["Arguments","Function","String","Number","Date","RegExp"],function(n){j["is"+n]=function(t){return l.call(t)=="[object "+n+"]"}}),j.isArguments(arguments)||(j.isArguments=function(n){return!(!n||!j.has(n,"callee"))}),"function"!=typeof/./&&(j.isFunction=function(n){return"function"==typeof n}),j.isFinite=function(n){return isFinite(n)&&!isNaN(parseFloat(n))},j.isNaN=function(n){return j.isNumber(n)&&n!=+n},j.isBoolean=function(n){return n===!0||n===!1||"[object Boolean]"==l.call(n)},j.isNull=function(n){return null===n},j.isUndefined=function(n){return n===void 0},j.has=function(n,t){return f.call(n,t)},j.noConflict=function(){return n._=t,this},j.identity=function(n){return n},j.constant=function(n){return function(){return n}},j.property=function(n){return function(t){return t[n]}},j.matches=function(n){return function(t){if(t===n)return!0;for(var r in n)if(n[r]!==t[r])return!1;return!0}},j.times=function(n,t,r){for(var e=Array(Math.max(0,n)),u=0;n>u;u++)e[u]=t.call(r,u);return e},j.random=function(n,t){return null==t&&(t=n,n=0),n+Math.floor(Math.random()*(t-n+1))},j.now=Date.now||function(){return(new Date).getTime()};var T={escape:{"&":"&","<":"<",">":">",'"':""","'":"'"}};T.unescape=j.invert(T.escape);var I={escape:new RegExp("["+j.keys(T.escape).join("")+"]","g"),unescape:new RegExp("("+j.keys(T.unescape).join("|")+")","g")};j.each(["escape","unescape"],function(n){j[n]=function(t){return null==t?"":(""+t).replace(I[n],function(t){return T[n][t]})}}),j.result=function(n,t){if(null==n)return void 0;var r=n[t];return j.isFunction(r)?r.call(n):r},j.mixin=function(n){A(j.functions(n),function(t){var r=j[t]=n[t];j.prototype[t]=function(){var n=[this._wrapped];return a.apply(n,arguments),z.call(this,r.apply(j,n))}})};var N=0;j.uniqueId=function(n){var t=++N+"";return n?n+t:t},j.templateSettings={evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,escape:/<%-([\s\S]+?)%>/g};var q=/(.)^/,B={"'":"'","\\":"\\","\r":"r","\n":"n"," ":"t","\u2028":"u2028","\u2029":"u2029"},D=/\\|'|\r|\n|\t|\u2028|\u2029/g;j.template=function(n,t,r){var e;r=j.defaults({},r,j.templateSettings);var u=new RegExp([(r.escape||q).source,(r.interpolate||q).source,(r.evaluate||q).source].join("|")+"|$","g"),i=0,a="__p+='";n.replace(u,function(t,r,e,u,o){return a+=n.slice(i,o).replace(D,function(n){return"\\"+B[n]}),r&&(a+="'+\n((__t=("+r+"))==null?'':_.escape(__t))+\n'"),e&&(a+="'+\n((__t=("+e+"))==null?'':__t)+\n'"),u&&(a+="';\n"+u+"\n__p+='"),i=o+t.length,t}),a+="';\n",r.variable||(a="with(obj||{}){\n"+a+"}\n"),a="var __t,__p='',__j=Array.prototype.join,"+"print=function(){__p+=__j.call(arguments,'');};\n"+a+"return __p;\n";try{e=new Function(r.variable||"obj","_",a)}catch(o){throw o.source=a,o}if(t)return e(t,j);var c=function(n){return e.call(this,n,j)};return c.source="function("+(r.variable||"obj")+"){\n"+a+"}",c},j.chain=function(n){return j(n).chain()};var z=function(n){return this._chain?j(n).chain():n};j.mixin(j),A(["pop","push","reverse","shift","sort","splice","unshift"],function(n){var t=e[n];j.prototype[n]=function(){var r=this._wrapped;return t.apply(r,arguments),"shift"!=n&&"splice"!=n||0!==r.length||delete r[0],z.call(this,r)}}),A(["concat","join","slice"],function(n){var t=e[n];j.prototype[n]=function(){return z.call(this,t.apply(this._wrapped,arguments))}}),j.extend(j.prototype,{chain:function(){return this._chain=!0,this},value:function(){return this._wrapped}}),"function"==typeof define&&define.amd&&define("underscore",[],function(){return j})}).call(this); +//# sourceMappingURL=underscore-min.map \ No newline at end of file diff --git a/topics/about_arrays.js b/topics/about_arrays.js index 3d4cd41f..fa4cb1e0 100644 --- a/topics/about_arrays.js +++ b/topics/about_arrays.js @@ -2,25 +2,27 @@ module("About Arrays (topics/about_arrays.js)"); test("array literal syntax and indexing", function() { var favouriteThings = ["cellar door", 42, true]; // note that array elements do not have to be of the same type - equal(__, favouriteThings[0], 'what is in the first position of the array?'); - equal(__, favouriteThings[1], 'what is in the second position of the array?'); - equal(__, favouriteThings[2], 'what is in the third position of the array?'); + equal("cellar door", favouriteThings[0], 'what is in the first position of the array?'); + equal(42, favouriteThings[1], 'what is in the second position of the array?'); + equal(true, favouriteThings[2], 'what is in the third position of the array?'); }); test("array type", function() { - equal(__, typeof([]), 'what is the type of an array?'); + //TODO: remember this + equal(typeof([]), 'object', 'what is the type of an array?'); }); test("length", function() { var collection = ['a','b','c']; - equal(__, collection.length, 'what is the length of the collection array?'); + equal(3, collection.length, 'what is the length of the collection array?'); }); test("splice", function() { var daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; - var workingWeek = daysOfWeek.splice(__, __); - ok(workingWeek.equalTo([__]), 'what is the value of workingWeek?'); - ok(daysOfWeek.equalTo([__]), 'what is the value of daysOfWeek?'); + var workingWeek = daysOfWeek.splice(0, 5); + ok(workingWeek.equalTo(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']), 'what is the value of workingWeek?'); + //TODO: remember this + ok(daysOfWeek.equalTo(['Saturday', 'Sunday']), 'what is the value of daysOfWeek?'); }); test("stack methods", function() { @@ -28,16 +30,18 @@ test("stack methods", function() { stack.push("first"); stack.push("second"); - equal(__, stack.pop(), 'what will be the first value popped off the stack?'); - equal(__, stack.pop(), 'what will be the second value popped off the stack?'); + equal("second", stack.pop(), 'what will be the first value popped off the stack?'); + equal("first", stack.pop(), 'what will be the second value popped off the stack?'); }); test("queue methods", function() { var queue = []; queue.push("first"); queue.push("second"); + //TODO: remember this queue.unshift("third"); - equal(__, queue.shift(), 'what will be shifted out first?'); - equal(__, queue.shift(), 'what will be shifted out second?'); + //TODO: remember this + equal("third", queue.shift(), 'what will be shifted out first?'); + equal("first", queue.shift(), 'what will be shifted out second?'); }); diff --git a/topics/about_asserts.js b/topics/about_asserts.js index baf7fc75..a2146770 100644 --- a/topics/about_asserts.js +++ b/topics/about_asserts.js @@ -2,13 +2,13 @@ module("About Asserts (topics/about_asserts.js)"); test("ok", function() { - ok(__ === true, 'what will satisfy the ok assertion?'); + ok(true === true, 'what will satisfy the ok assertion?'); }); test("not ok", function() { - ok(__ === false, 'what is a false value?'); + ok(false === false, 'what is a false value?'); }); test("equal", function() { - equal(__, 1 + 1, 'what will satisfy the equal assertion?'); + equal(2, 1 + 1, 'what will satisfy the equal assertion?'); }); diff --git a/topics/about_assignment.js b/topics/about_assignment.js index 4532861e..cba47c77 100644 --- a/topics/about_assignment.js +++ b/topics/about_assignment.js @@ -2,11 +2,11 @@ module("About Assignment (topics/about_assignment.js)"); test("local variables", function() { - var temp = __; + var temp = 1; equal(temp, 1, "Assign a value to the variable temp"); }); test("global variables", function() { temp = 1; // Not using var is an example. Always use var in practise. - equal(window.__, temp, 'global variables are assigned to the window object'); + equal(window.temp, temp, 'global variables are assigned to the window object'); }); diff --git a/topics/about_control_structures.js b/topics/about_control_structures.js index 767d266c..5f107038 100644 --- a/topics/about_control_structures.js +++ b/topics/about_control_structures.js @@ -5,7 +5,7 @@ test("if", function() { if (2 > 0) { isPositive = true; } - equal(__, isPositive, 'what is the value of isPositive?'); + equal(true, isPositive, 'what is the value of isPositive?'); }); test("for", function() { @@ -13,7 +13,7 @@ test("for", function() { for (var i = 1; i <= 3; i++) { counter = counter + i; } - equal(__, counter, 'what is the value of counter?'); + equal(16, counter, 'what is the value of counter?'); }); test("for in", function() { @@ -23,19 +23,42 @@ test("for in", function() { age: 102 }; var result = ""; + var memberValues = []; // for in enumerates the property names of an object for (var property_name in person) { result = result + property_name; + memberValues.push(person[property_name]); }; - equal(__, result, 'what is the value of result?'); + equal("nameage", result, 'what is the value of result?'); + equal("Amory Blaine",memberValues[0], 'what are the members values?'); + equal(102,memberValues[1], 'what are the members values?'); + +// TODO: understand why these keep failing +// var provaArrary = [1, 2, 3, 4, 5]; + +// var indexSum = 0; +// var elementSum = ''; +// for (var ind in provaArrary) { +// indexSum = indexSum + ind; +// //elementSum += provaArray[ind]; +// }; +// equal(4,indexSum, 'what is the sum of the index'); +// equal('12345',elementSum, 'what is the sum of the elements'); + +// indexSum = 0; +// for (ind in memberValues) { +// indexSum += ind; +// } +// equal('001',indexSum, 'what is the sum of the index'); + }); test("ternary operator", function() { var fruit = true ? "apple" : "orange"; - equal(__, fruit, 'what is the value of fruit?'); + equal('apple', fruit, 'what is the value of fruit?'); fruit = false ? "apple" : "orange"; - equal(__, fruit, 'now what is the value of fruit?'); + equal('orange', fruit, 'now what is the value of fruit?'); }); test("switch", function() { @@ -48,7 +71,7 @@ test("switch", function() { result = 2; break; } - equal(__, result, 'what is the value of result?'); + equal(2, result, 'what is the value of result?'); }); test("switch default case", function() { @@ -64,10 +87,10 @@ test("switch default case", function() { result = "Merry"; break; } - equal(__, result, 'what is the value of result?'); + equal("Merry", result, 'what is the value of result?'); }); test("null coalescing", function() { var result = null || "a value"; - equal(__, result, 'what is the value of result?'); + equal("a value", result, 'what is the value of result?'); }); diff --git a/topics/about_data_types.js b/topics/about_data_types.js new file mode 100644 index 00000000..32cd063d --- /dev/null +++ b/topics/about_data_types.js @@ -0,0 +1,105 @@ +module("About Data Types (topics/about_data_types.js)"); + +test("using typeof to identify data types", function() { + var aString = ''; + var aNumber = 1; + var anotherNumber = 1.0; + var anHex = 0xff; + var aBool = true; + var aFunction = function(){}; + var anObject = {a:'a'}; + var anotherObject ={b:'b'}; + + var anArray = ["1","2"]; + var anUndefVar; + var aNull = null; + + equal(typeof(aString), 'string', 'what is the type of a string?'); + equal(typeof(aNumber), 'number', 'what is the type of an int?'); + equal(typeof(anotherNumber), 'number', 'what is the type of a float?'); + equal(typeof(anHex), 'number', 'what is the type of a float?'); + equal(typeof(aBool), 'boolean', 'what is the type of a boolean?'); + equal(typeof(aFunction), 'function', 'what is the type of a function?'); + equal(typeof(anObject), 'object', 'what is the type of an object?'); + equal(typeof(anObject), typeof(anotherObject), 'how to differentiate between objects?'); + + equal(typeof(anArray), 'object', 'what is the type of an array?'); + equal(typeof(anUndefVar), 'undefined', 'what is the type of an unassigned var?'); + equal(typeof(aNull), 'object', 'what is the type of a null?'); +}); + +test("using instanceof to identify objects", function() { + var aString = ''; + var aNumber = 1; + var anotherNumber = 1.0; + var anHex = 0xff; + var aBool = true; + var aFunction = function(){}; + var anObject = {a:'a'}; + + function anotherObject(){b:'b'}; + + var anArray = ["1","2"]; + var anUndefVar; + var aNull = null; + + equal(aString instanceof String, false, 'what is the type of a string?'); + equal(aNumber instanceof Number, false, 'what is the type of an int?'); + equal(anotherNumber instanceof Number, false, 'what is the type of a float?'); + equal(anHex instanceof Number, false, 'what is the type of a float?'); + equal(aBool instanceof Boolean, false, 'what is the type of a boolean?'); + equal(aFunction instanceof Function, true, 'what is the type of a function?'); + equal(anObject instanceof Object, true, 'what is the type of an object?'); + equal(anObject instanceof Object, true, 'what is the type of an object?'); + equal((new anotherObject()) instanceof Object, true, 'how to differentiate between objects?'); + equal((new anotherObject()) instanceof anotherObject, true, 'how to differentiate between objects?'); + + equal(anArray instanceof Object, true, 'what is the type of an array?'); + equal(anArray instanceof Array, true, 'what is the type of an array?'); + equal(anUndefVar instanceof Object, false, 'what is the type of an unassigned var?'); + equal(aNull instanceof Object, false, 'what is the type of a null?'); +}); + +test("using constructor name to identify objects", function() { + + function anotherObject(){this.b='b'}; + var anObject = new anotherObject(); + var aSecondObject = {a:'a'}; + var aThirdObject = {}; + + // Constructor property points to the objects constructor function. Own or inherited + // Own + equal(typeof(anObject.constructor), 'function', 'what is the name of an object constructor?'); + equal(anObject.constructor.name, 'anotherObject', 'what is the name of an object constructor?'); + equal(anObject.constructor, anotherObject, 'what is the object constructor?'); + equal(anObject.constructor, anotherObject.prototype.constructor, 'what is the name of an object constructor?'); + equal(anotherObject, anotherObject.prototype.constructor, 'what is the name of an object constructor?'); + + // Inherited + equal(typeof(aSecondObject.constructor), 'function', 'what is the name of an object constructor?'); + equal(aSecondObject.constructor.name, 'Object', 'what is the name of an object constructor?'); + equal(aSecondObject.constructor, Object, 'what is the object constructor?'); + // equal(aSecondObject.constructor, aSecondObject.prototype.constructor, 'what is the name of an object constructor?'); + equal(aSecondObject.constructor, aThirdObject.constructor, 'all objects of same type share constructor?'); + + + +}); + +test("using prototype name to identify objects", function() { + var aFunction = function(){ return 'hola';} + function anotherObject(){b:'b'}; + var anObject = new anotherObject(); + var aSecondObject = {a:'a'}; + + // Every function has a prototype property that points to the ... + equal(typeof(aFunction.prototype), "object", 'what is the prototype of a function?'); + // equal(aFunction.prototype, new Object(), 'what is the prototype of a function?'); + + equal(typeof(anotherObject.prototype), "object", 'what is the prototype of an object constructor?'); + + // Prototype is a property of the function not the object + equal(aSecondObject.prototype, null, 'what is the prototype of an object constructor?'); + equal('prototype' in aSecondObject, false, 'objects have no prototypes') +}); + diff --git a/topics/about_equality.js b/topics/about_equality.js index fe3e3d21..17eb462c 100644 --- a/topics/about_equality.js +++ b/topics/about_equality.js @@ -2,22 +2,22 @@ module("About Equality (topics/about_equality.js)"); test("numeric equality", function() { - equal(3 + __, 7, ""); + equal(3 + 4, 7, ""); }); test("string equality", function() { - equal("3" + __, "37", "concatenate the strings"); + equal("3" + "7", "37", "concatenate the strings"); }); test("equality without type coercion", function() { - ok(3 === __, 'what is exactly equal to 3?'); + ok(3 === 3, 'what is exactly equal to 3?'); }); test("equality with type coercion", function() { - ok(3 == "__", 'what string is equal to 3, with type coercion?'); + ok(3 == "3", 'what string is equal to 3, with type coercion?'); }); test("string literals", function() { - equal(__, "frankenstein", "quote types are interchangable, but must match."); - equal(__, 'frankenstein', "quote types can use both single and double quotes."); + equal('frankenstein', "frankenstein", "quote types are interchangable, but must match."); + equal("frankenstein", 'frankenstein', "quote types can use both single and double quotes."); }); diff --git a/topics/about_functions_and_closure.js b/topics/about_functions_and_closure.js index 7eb20777..9648135c 100644 --- a/topics/about_functions_and_closure.js +++ b/topics/about_functions_and_closure.js @@ -7,29 +7,30 @@ test("defining functions directly", function() { result = "b"; }; changeResult(); - equal(__, result, 'what is the value of result?'); + equal("b", result, 'what is the value of result?'); }); test("assigning functions to variables", function() { var triple = function(input) { return input * 3; }; - equal(__, triple(4), 'what is triple 4?'); + equal(12, triple(4), 'what is triple 4?'); }); test("self invoking functions", function() { var publicValue = "shared"; + // TODO: Super important to understand this // self invoking functions are used to provide scoping and to alias variables (function(pv) { var secretValue = "password"; - equal(__, pv, 'what is the value of pv?'); - equal("__", typeof(secretValue), "is secretValue available in this context?"); - equal("__", typeof(publicValue), "is publicValue available in this context?"); + equal("shared", pv, 'what is the value of pv?'); + equal("string", typeof(secretValue), "is secretValue available in this context?"); + equal("string", typeof(publicValue), "is publicValue available in this context?"); })(publicValue); - equal("__", typeof(secretValue), "is secretValue available in this context?"); - equal("__", typeof(publicValue), "is publicValue available in this context?"); + equal("undefined", typeof(secretValue), "is secretValue available in this context?"); + equal("string", typeof(publicValue), "is publicValue available in this context?"); }); test("arguments array", function() { @@ -37,9 +38,9 @@ test("arguments array", function() { var total = 0; for(var i = 0; i < arguments.length; i++) { // complete the implementation of this method so that it returns the sum of its arguments - // __ + total += arguments[i]; } - // __ + return total; }; equal(15, add(1,2,3,4,5), "add 1,2,3,4,5"); @@ -51,13 +52,14 @@ test("using call to invoke function",function(){ return this + message; }; + // TODO: important too //another way to invoke a function is to use the call function which allows //you to set the callers "this" context. Call can take any number of arguments: //the first one is always the context that this should be set to in the called //function, and the arguments to be sent to the function,multiple arguments are separated by commas. var result = invokee.call("I am this!", "Where did it come from?"); - equal(__, result, "what will the value of invokee's this be?"); + equal("I am this!Where did it come from?", result, "what will the value of invokee's this be?"); }); test("using apply to invoke function",function(){ @@ -70,6 +72,6 @@ test("using apply to invoke function",function(){ //function and and array of arguments to be passed into the called function. var result = invokee.apply("I am this!", ["I am arg1","I am arg2"]); - equal(__, result, "what will the value of invokee's this be?"); + equal("I am this!I am arg1I am arg2", result, "what will the value of invokee's this be?"); }); diff --git a/topics/about_numbers.js b/topics/about_numbers.js index 1319acd8..eae0ec3c 100644 --- a/topics/about_numbers.js +++ b/topics/about_numbers.js @@ -4,13 +4,14 @@ module("About Numbers (topics/about_numbers.js)"); test("types", function() { var typeOfIntegers = typeof(6); var typeOfFloats = typeof(3.14159); - equal(__, typeOfIntegers === typeOfFloats, 'are ints and floats the same type?'); - equal(__, typeOfIntegers, 'what is the javascript numeric type?'); - equal(__, 1.0, 'what is a integer number equivalent to 1.0?'); + equal(true, typeOfIntegers === typeOfFloats, 'are ints and floats the same type?'); + equal('number', typeOfIntegers, 'what is the javascript numeric type?'); + equal(1, 1.0, 'what is a integer number equivalent to 1.0?'); }); test("NaN", function() { var resultOfFailedOperations = 7/'apple'; - equal(__, isNaN(resultOfFailedOperations), 'what will satisfy the equals assertion?'); - equal(__, resultOfFailedOperations == NaN, 'is NaN == NaN?'); + equal(true, isNaN(resultOfFailedOperations), 'what will satisfy the equals assertion?'); + //TODO: research this + equal(false, resultOfFailedOperations == NaN, 'is NaN == NaN?'); }); diff --git a/topics/about_objects.js b/topics/about_objects.js index 24c03533..55edb58a 100644 --- a/topics/about_objects.js +++ b/topics/about_objects.js @@ -3,13 +3,13 @@ module("About Objects (topics/about_objects.js)"); test("object type", function() { var empty_object = {}; - equal(__, typeof(empty_object), 'what is the type of an object?'); + equal('object', typeof(empty_object), 'what is the type of an object?'); }); test("object literal notation", function() { var person = { - __:__, - __:__ + name:"Amory Blaine", + age:102 }; equal("Amory Blaine", person.name, "what is the person's name?"); equal(102, person.age, "what is the person's age?"); @@ -17,16 +17,16 @@ test("object literal notation", function() { test("dynamically adding properties", function() { var person = {}; - person.__ = "Amory Blaine"; - person.__ = 102; + person.name = "Amory Blaine"; + person.age = 102; equal("Amory Blaine", person.name, "what is the person's name?"); equal(102, person.age, "what is the person's age?"); }); test("adding properties from strings", function() { var person = {}; - person["__"] = "Amory Blaine"; - person["__"] = 102; + person["name"] = "Amory Blaine"; + person["age"] = 102; equal("Amory Blaine", person.name, "what is the person's name?"); equal(102, person.age, "what is the person's age?"); }); @@ -36,7 +36,7 @@ test("adding functions", function() { name: "Amory Blaine", age: 102, toString: function() { - return __; // HINT: use the 'this' keyword to refer to the person object. + return 'I ' + this.name + ' am ' + this.age + ' years old.'; // HINT: use the 'this' keyword to refer to the person object. } }; equal("I Amory Blaine am 102 years old.", person.toString(), "what should the toString function be?"); diff --git a/topics/about_operators.js b/topics/about_operators.js index 9859900b..9d5ce763 100644 --- a/topics/about_operators.js +++ b/topics/about_operators.js @@ -7,7 +7,7 @@ test("addition", function() { for (var i = 0; i <= 5; i++) { result = result + i; } - equal(__, result, "What is the value of result?"); + equal(15, result, "What is the value of result?"); }); test("assignment addition", function() { @@ -16,7 +16,7 @@ test("assignment addition", function() { //the code below is just like saying result = result + i; but is more concise result += i; } - equal(__, result, "What is the value of result?"); + equal(15, result, "What is the value of result?"); }); test("subtraction", function() { @@ -24,7 +24,7 @@ test("subtraction", function() { for (var i = 0; i <= 2; i++) { result = result - i; } - equal(__, result, "What is the value of result?"); + equal(2, result, "What is the value of result?"); }); test("assignment subtraction", function() { @@ -32,7 +32,7 @@ test("assignment subtraction", function() { for (var i = 0; i <= 2; i++) { result -= i; } - equal(__, result, "What is the value of result?"); + equal(2, result, "What is the value of result?"); }); //Assignment operators are available for multiplication and division as well @@ -43,5 +43,5 @@ test("modulus", function() { var x = 5; //again this is exactly the same as result = result % x result %= x; - equal(__, result, "What is the value of result?"); + equal(0, result, "What is the value of result?"); }); diff --git a/topics/about_prototypal_inheritance.js b/topics/about_prototypal_inheritance.js index 811c040e..d4d8ae7c 100644 --- a/topics/about_prototypal_inheritance.js +++ b/topics/about_prototypal_inheritance.js @@ -16,7 +16,7 @@ Mammal.prototype = { test("defining a 'class'", function() { var eric = new Mammal("Eric"); - equal(__, eric.sayHi(), 'what will Eric say?'); + equal("Hello, my name is Eric", eric.sayHi(), 'what will Eric say?'); }); // add another function to the Mammal 'type' that uses the sayHi function @@ -26,7 +26,7 @@ Mammal.prototype.favouriteSaying = function() { test("more functions", function() { var bobby = new Mammal("Bobby"); - equal(__, bobby.favouriteSaying(), "what is Bobby's favourite saying?"); + equal("Bobby's favourite saying is Hello, my name is Bobby", bobby.favouriteSaying(), "what is Bobby's favourite saying?"); }); test("calling functions added to a prototype after an object was created", function() { @@ -36,9 +36,10 @@ test("calling functions added to a prototype after an object was created", funct }; // the following statement asks the paul object to call a function that was added // to the Mammal prototype after paul was constructed. - equal(__, paul.numberOfLettersInName(), "how long is Paul's name?"); + equal(4, paul.numberOfLettersInName(), "how long is Paul's name?"); }); +// TODO: remember this // helper function for inheritance. // From https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_Revisited function extend(child, supertype){ @@ -47,6 +48,7 @@ function extend(child, supertype){ // "Subclass" Mammal function Bat(name, wingspan) { + // TODO: understand this new way to call constructor Mammal.call(this, name); this.wingspan = wingspan; } @@ -56,6 +58,6 @@ extend(Bat, Mammal); test("Inheritance", function() { var lenny = new Bat("Lenny", "1.5m"); - equal(__, lenny.sayHi(), "what does Lenny say?"); - equal(__, lenny.wingspan, "what is Lenny's wingspan?"); + equal("Hello, my name is Lenny", lenny.sayHi(), "what does Lenny say?"); + equal("1.5m", lenny.wingspan, "what is Lenny's wingspan?"); }); diff --git a/topics/about_prototype_chain.js b/topics/about_prototype_chain.js index 46e3b4df..723a60ac 100644 --- a/topics/about_prototype_chain.js +++ b/topics/about_prototype_chain.js @@ -8,6 +8,8 @@ var father = { c: 4 }; +// TODO: study Object.create creates a new object with the specified prototype +// object and properties. Object.create(proto [, propertiesObject ]) var child = Object.create(father); child.a = 1; child.b = 2; @@ -27,29 +29,32 @@ child.b = 2; * */ test("Is there an 'a' and 'b' own property on child?", function () { - equal(__, child.hasOwnProperty('a'), 'child.hasOwnProperty(\'a\')?'); - equal(__, child.hasOwnProperty('b'), 'child.hasOwnProperty(\'b\')?'); + equal(true, child.hasOwnProperty('a'), 'child.hasOwnProperty(\'a\')?'); + equal(true, child.hasOwnProperty('b'), 'child.hasOwnProperty(\'b\')?'); }); test("Is there an 'a' and 'b' property on child?", function () { - equal(__, child.a, 'what is \'a\' value?'); - equal(__, child.b, 'what is \'b\' value?'); + equal(1, child.a, 'what is \'a\' value?'); + equal(2, child.b, 'what is \'b\' value?'); }); test("If 'b' was removed, whats b value?", function () { delete child.b; - equal(__, child.b, 'what is \'b\' value now?'); + // TODO: remember this + equal('3', child.b, 'what is \'b\' value now?'); }); test("Is there a 'c' own property on child?", function () { - equal(__, child.hasOwnProperty('c'), 'child.hasOwnProperty(\'c\')?'); + equal(false, child.hasOwnProperty('c'), 'child.hasOwnProperty(\'c\')?'); }); // Is there a 'c' own property on child? No, check its prototype // Is there a 'c' own property on child.[[Prototype]]? Yes, its value is... test("Is there a 'c' property on child?", function () { - equal(__, child.c, 'what is the value of child.c?'); + //TODO: why prototype not accessible? +// equal(true, child.prototype.hasOwnProperty('c'), 'child.prototype.hasOwnProperty(\'c\')?'); + equal(4, child.c, 'what is the value of child.c?'); }); @@ -57,7 +62,9 @@ test("Is there a 'c' property on child?", function () { // Is there a 'd' own property on child.[[Prototype]]? No, check it prototype // child.[[Prototype]].[[Prototype]] is null, stop searching, no property found, return... test("Is there an 'd' property on child?", function () { - equal(__, child.d, 'what is the value of child.d?'); + equal(false, child.hasOwnProperty('d'), 'child.hasOwnProperty(\'c\')?'); +// equal(false, child.prototype.hasOwnProperty('d'), 'child.prototype.hasOwnProperty(\'c\')?'); + equal(undefined, child.d, 'what is the value of child.d?'); }); diff --git a/topics/about_reflection.js b/topics/about_reflection.js index 51e7c8ff..28aa3038 100644 --- a/topics/about_reflection.js +++ b/topics/about_reflection.js @@ -1,5 +1,7 @@ module("About Reflection (topics/about_reflection.js)"); +//TODO: study all these + var A = function() { this.aprop = "A"; }; @@ -11,10 +13,10 @@ var B = function() { B.prototype = new A(); test("typeof", function() { - equal(__, typeof({}), 'what is the type of an empty object?'); - equal(__, typeof('apple'), 'what is the type of a string?'); - equal(__, typeof(-5), 'what is the type of -5?'); - equal(__, typeof(false), 'what is the type of false?'); + equal('object', typeof({}), 'what is the type of an empty object?'); + equal('string', typeof('apple'), 'what is the type of a string?'); + equal('number', typeof(-5), 'what is the type of -5?'); + equal('boolean', typeof(false), 'what is the type of false?'); }); test("property enumeration", function() { @@ -25,8 +27,8 @@ test("property enumeration", function() { keys.push(propertyName); values.push(person[propertyName]); } - ok(keys.equalTo(['__','__','__']), 'what are the property names of the object?'); - ok(values.equalTo(['__',__,__]), 'what are the property values of the object?'); + ok(keys.equalTo(['name','age','unemployed']), 'what are the property names of the object?'); + ok(values.equalTo(['Amory Blaine',102,true]), 'what are the property values of the object?'); }); test("hasOwnProperty", function() { @@ -37,8 +39,9 @@ test("hasOwnProperty", function() { for (propertyName in b) { keys.push(propertyName); } - equal(__, keys.length, 'how many elements are in the keys array?'); - deepEqual([__, __], keys, 'what are the properties of the array?'); + equal(2, keys.length, 'how many elements are in the keys array?'); + // TODO: remember this order: first own, then inherited + deepEqual(['bprop', 'aprop'], keys, 'what are the properties of the array?'); // hasOwnProperty returns true if the parameter is a property directly on the object, // but not if it is a property accessible via the prototype chain. @@ -48,21 +51,23 @@ test("hasOwnProperty", function() { ownKeys.push(propertyName); } } - equal(__, ownKeys.length, 'how many elements are in the ownKeys array?'); - deepEqual([__], ownKeys, 'what are the own properties of the array?'); + equal(1, ownKeys.length, 'how many elements are in the ownKeys array?'); + deepEqual(['bprop'], ownKeys, 'what are the own properties of the array?'); }); test("constructor property", function () { var a = new A(); var b = new B(); - equal(__, typeof(a.constructor), "what is the type of a's constructor?"); - equal(__, a.constructor.name, "what is the name of a's constructor?"); - equal(__, b.constructor.name, "what is the name of b's constructor?"); + equal('function', typeof(a.constructor), "what is the type of a's constructor?"); + // TODO: Super important !!! Study this: if no explicit constructor ... + equal('', a.constructor.name, "what is the name of a's constructor?"); + equal('', b.constructor.name, "what is the name of b's constructor?"); }); test("eval", function() { // eval executes a string var result = ""; eval("result = 'apple' + ' ' + 'pie'"); - equal(__, result, 'what is the value of result?'); + // TODO: remember this + equal('apple pie', result, 'what is the value of result?'); }); diff --git a/topics/about_regular_expressions.js b/topics/about_regular_expressions.js index b49bc723..dc9d7f05 100644 --- a/topics/about_regular_expressions.js +++ b/topics/about_regular_expressions.js @@ -4,28 +4,28 @@ module("About Regular Expressions (topics/about_regular_expressions.js)"); test("exec", function() { var numberFinder = /(\d).*(\d)/; var results = numberFinder.exec("what if 6 turned out to be 9?"); - ok(results.equalTo([__, __, __]), 'what is the value of results?'); + ok(results.equalTo(["6 turned out to be 9", "6", "9"]), 'what is the value of results?'); }); test("test", function() { var containsSelect = /select/.test(" select * from users "); - equal(__, containsSelect, 'does the string provided contain "select"?'); + equal(true, containsSelect, 'does the string provided contain "select"?'); }); test("match", function() { var matches = "what if 6 turned out to be 9?".match(/(\d)/g); - ok(matches.equalTo([__, __]), 'what is the value of matches?'); + ok(matches.equalTo(["6", "9"]), 'what is the value of matches?'); }); test("replace", function() { var pie = "apple pie".replace("apple", "strawberry"); - equal(__, pie, 'what is the value of pie?'); + equal("strawberry pie", pie, 'what is the value of pie?'); pie = "what if 6 turned out to be 9?".replace(/\d/g, function(number) { // the second parameter can be a string or a function var map = {'6': 'six','9': 'nine'}; return map[number]; }); - equal(__, pie, 'what is the value of pie?'); + equal("what if six turned out to be nine?", pie, 'what is the value of pie?'); }); // THE END diff --git a/topics/about_scope.js b/topics/about_scope.js index efa802a0..b7f9ccdc 100644 --- a/topics/about_scope.js +++ b/topics/about_scope.js @@ -3,7 +3,7 @@ module("About Scope (topics/about_scope.js)"); thisIsAGlobalVariable = 77; test("global variables", function() { - equal(__, thisIsAGlobalVariable, 'is thisIsAGlobalVariable defined in this scope?'); + equal(77, thisIsAGlobalVariable, 'is thisIsAGlobalVariable defined in this scope?'); }); test("variables declared inside of a function", function() { @@ -12,10 +12,10 @@ test("variables declared inside of a function", function() { // this is a self-invoking function. Notice that it calls itself at the end (). (function() { var innerVariable = "inner"; - equal(__, outerVariable, 'is outerVariable defined in this scope?'); - equal(__, innerVariable, 'is innerVariable defined in this scope?'); + equal("outer", outerVariable, 'is outerVariable defined in this scope?'); + equal("inner", innerVariable, 'is innerVariable defined in this scope?'); })(); - equal(__, outerVariable, 'is outerVariable defined in this scope?'); - equal(__, typeof(innerVariable), 'is innerVariable defined in this scope?'); + equal("outer", outerVariable, 'is outerVariable defined in this scope?'); + equal("undefined", typeof(innerVariable), 'is innerVariable defined in this scope?'); }); diff --git a/topics/about_strings.js b/topics/about_strings.js index 18f9c68a..723f3c14 100644 --- a/topics/about_strings.js +++ b/topics/about_strings.js @@ -4,31 +4,33 @@ module("About Strings (topics/about_strings.js)"); test("delimiters", function() { var singleQuotedString = 'apple'; var doubleQuotedString = "apple"; - equal(__, singleQuotedString === doubleQuotedString, 'are the two strings equal?'); + //TODO: Remember this + equal(true, singleQuotedString === doubleQuotedString, 'are the two strings equal?'); }); test("concatenation", function() { var fruit = "apple"; var dish = "pie"; - equal(__, fruit + " " + dish, 'what is the value of fruit + " " + dish?'); + equal('apple pie', fruit + " " + dish, 'what is the value of fruit + " " + dish?'); }); test("character Type", function() { var characterType = typeof("Amory".charAt(1)); // typeof will be explained in about reflection - equal(__, characterType, 'Javascript has no character type'); + //TODO: remember this + equal('string', characterType, 'Javascript has no character type'); }); test("escape character", function() { var stringWithAnEscapedCharacter = "\u0041pple"; - equal(__, stringWithAnEscapedCharacter, 'what is the value of stringWithAnEscapedCharacter?'); + equal('Apple', stringWithAnEscapedCharacter, 'what is the value of stringWithAnEscapedCharacter?'); }); test("string.length", function() { var fruit = "apple"; - equal(__, fruit.length, 'what is the value of fruit.length?'); + equal(5, fruit.length, 'what is the value of fruit.length?'); }); test("slice", function() { var fruit = "apple pie"; - equal(__, fruit.slice(0,5), 'what is the value of fruit.slice(0,5)?'); + equal('apple', fruit.slice(0,5), 'what is the value of fruit.slice(0,5)?'); }); diff --git a/topics/about_this.js b/topics/about_this.js index 91b3135c..a538e303 100644 --- a/topics/about_this.js +++ b/topics/about_this.js @@ -4,7 +4,7 @@ test("'this' inside a method", function () { var person = { name: 'bob', intro: function () { - return "Hello, my name is " + this.__; + return "Hello, my name is " + this.name; } } equal(person.intro(), "Hello, my name is bob", "If an object has a method can you access properties inside it?"); @@ -22,7 +22,7 @@ test("'this' on unattached function", function () { // if the function is not called as an object property 'this' is the global context // (window in a browser). This is an example. Please do not do this in practise. - window.__ = 'Peter'; + window.globalName = 'Peter'; equal(alias(), "Hello, my name is Peter", "What does 'this' referer to when it is not part of an object?"); }); @@ -35,10 +35,16 @@ test("'this' set explicitly", function () { } // calling a function with 'call' lets us assign 'this' explicitly - var message = person.intro.call({__: "Frank"}); + var message = person.intro.call({name: "Frank"}); equal(message, "Hello, my name is Frank", "What does 'this' referer to when you use the 'call()' method?"); }); // extra credit: underscore.js has a 'bind' function http://documentcloud.github.com/underscore/#bind // read the source and see how it is implemented +test("'this' set by binding", function () { + var func = function(greeting){ return greeting + ': ' + this.name }; + func = _.bind(func, {name : 'moe'}, 'hi'); + equal('hi: moe',func(),'What does this refer to when function binded'); +}); + diff --git a/topics/about_truthyness.js b/topics/about_truthyness.js index 9b524c14..1b48421d 100644 --- a/topics/about_truthyness.js +++ b/topics/about_truthyness.js @@ -3,20 +3,20 @@ module("About Truthyness (topics/about_truthyness.js)"); test("truthyness of positive numbers", function() { var oneIsTruthy = 1 ? true : false; - equal(__, oneIsTruthy, 'is one truthy?'); + equal(true, oneIsTruthy, 'is one truthy?'); }); test("truthyness of negative numbers", function() { var negativeOneIsTruthy = -1 ? true : false; - equal(__, negativeOneIsTruthy, 'is -1 truthy?'); + equal(true, negativeOneIsTruthy, 'is -1 truthy?'); }); test("truthyness of zero", function() { var zeroIsTruthy = 0 ? true : false; - equal(__, zeroIsTruthy, 'is 0 truthy?'); + equal(false, zeroIsTruthy, 'is 0 truthy?'); }); test("truthyness of null", function() { var nullIsTruthy = null ? true : false; - equal(__, nullIsTruthy, 'is null truthy?'); + equal(false, nullIsTruthy, 'is null truthy?'); });