# add 4 new puzzles and instructions #1

Merged
merged 1 commit into from Apr 10, 2013
Commits
Jump to file or symbol
Failed to load files and symbols.
+86 −1

#### Just for now

19 1.js
 @@ -0,0 +1,19 @@ +// problem: why does Ralph only bark once? +// fix it to make Ralph bark twice! + +var Dog = function( name ) { + this.name = name +} + +Dog.prototype.bark = function() { + console.log( this.name ) +} + +// make a new instance of a Dog +var ralph = new Dog('Ralph') + +// make Ralph bark once immediately +ralph.bark() + +// in 1 second we want Ralph to bark again +setTimeout( ralph.bark , 1000 )
14 2.js
 @@ -0,0 +1,14 @@ +// problem: why does it log out out 1000? +// fix it to log 100, 200, 300, 400, 500 etc + +// loops 10 times from 0 - 1000 in increments of 100 +for (var i = 0; i < 1000; i += 100) { + waitFor(i, function then() { + console.log(i) + }) +} + +// this will run a callback function after waiting milliseconds +function waitFor(milliseconds, callback) { + setTimeout(callback, milliseconds) +}
13 3.js
 @@ -0,0 +1,13 @@ +// problem: why is it logging out 0? it should be 12 +// since 4 * 4 is 16, 2 * 2 is 4 and 16 - 4 is 12 +// fix it to log out 12! + +function square(num) { + result = num * num + return result +} + +result = square(4) +result2 = square(2) +var subtracted = result - result2 +console.log(subtracted)
19 4.js
 @@ -0,0 +1,19 @@ +// problem: it is adding all of the numbers to the evenNumbers array +// even though it should only add even numbers +// fix it to only add even numbers! + +var evenNumbers = [] + +function addEvensOnly(num) { + if (num % 2 === 0) + console.log(num, 'is even, adding!') + evenNumbers.push(num) +} + +addEvensOnly(0) +addEvensOnly(1) +addEvensOnly(2) +addEvensOnly(3) +addEvensOnly(4) + +console.log('even numbers: ', evenNumbers)
 @@ -1,4 +1,24 @@ javascript-puzzles ================== -Improve your JavaScript skills by correcting the errors in this code +Improve your JavaScript skills by correcting the errors in this code. + +You can run this in the browser by copy and pasting each exercise into your browsers JavaScript console: + +![browser](screenshots/browser.png) + +Or on the command line using [node.js](http://nodejs.org): + +![node](screenshots/node.png) + +## contributing + +If you have an idea for a puzzle that will teach someone an important JS concept then please submit it as a pull request! + +## license + +BSD + +## credits + +some puzzles adapted from http://liamkaufman.com/blog/2012/09/09/common-javascript-errors/
Binary file not shown.
Binary file not shown.