From a24ef288b4d00cea5366e32ae0c6f477100f3967 Mon Sep 17 00:00:00 2001 From: benmcmahon100 Date: Fri, 24 Jul 2015 00:38:13 +0100 Subject: [PATCH] Ready Js Curriculum Added Regex, Random Numbers and If else Challenges --- seed/challenges/basic-javascript.json | 167 +++++++++++++++++++++++++- 1 file changed, 163 insertions(+), 4 deletions(-) diff --git a/seed/challenges/basic-javascript.json b/seed/challenges/basic-javascript.json index 7bf51368b5..9fb5129929 100644 --- a/seed/challenges/basic-javascript.json +++ b/seed/challenges/basic-javascript.json @@ -886,12 +886,20 @@ "While it's great that we can create random decimal numbers it's a lot more useful to generate a random whole number", "To achieve this we can multiply the random number by ten and use the Math.floor() to convert the decimal number to a whole number", "This technique gives us a whole number between zero and nine", + "Example:", + "Math.floor(Math.random()*10);", "Let's give this technique a go now" ], - "tests":[], + "tests":[ + "assert(typeof(myFunction()) == 'number', 'The result of myFunction should be a number');", + "assert(editor.getValue().match(/Math.random/g), 'You should be using Math.random to create a random number');", + "assert(!(''+myFunction()).match(/\\./g), 'You should have multiplied the result of Math.random but 10 to make it a number that\\'s greater then zero');", + "assert(editor.getValue().match(/Math.floor/g), 'You should use Math.floor to remove the decimal part of the number');" + ], "challengeSeed":[ "function myFunction(){", " //Make myFunction return a random number between zero and nine instead of a float", + " return(Math.random());", "}", "", "(function(){return(myFunction());})();" @@ -899,16 +907,167 @@ "challengeType": 1 }, { - "_id":"", + "_id":"bh1111c1c12feddfaeb2bdef", "name":"Random Whole Numbers In a Range", "dashedName":"waypoint-random-whole-numbers-in-a-range", "difficulty":"9.9829", "description":[ "", + "We can use a certain mathematical expression to get a random number between between twp numbers.", + "Math.floor(Math.random() * (max - min + 1)) + min", + "By using this we can control the output of the random number.", "" ], - "tests":[], - "challengeSeed":[], + "tests":[ + "assert(myFunction() >= min, 'The random number that\\'s generated by myFunction should be greater than or equal to the minimum number');", + "assert(myFunction() <= max, 'The random number that\\'s generated by myFunction should be less than or equal to the maximum number');", + "assert((function(){if(editor.getValue().match(/max/g).length >= 2 && editor.getValue().match(/min/g).length >= 3 && editor.getValue().match(/Math.floor/g) && editor.getValue().match(/Math.random/g)){return(true);}else{return(false);}})(), 'You should be using the function given in the description to calculate the random in number in a range');" + ], + "challengeSeed":[ + " var min = 0;", + " var max = 12;", + "function myFunction(){", + " //Make myFunction return a random number between zero and nine instead of a float", + " return(Math.random());", + "}", + "", + "(function(){return(myFunction());})();" + ], + "challengeType": 1 + }, + { + "_id":"bh1111c1c12feddfaeb3bdef", + "name":"If Else Statements", + "dashedName":"waypoint-if-else-statements", + "difficulty":"9.983", + "description":[ + "", + "We can use if statements in JavaScript to only execute code if a certain condition is met", + "if statements require some sort of boolean condition evaluate", + "Example:", + "if(1==2){", + " return(true);", + "}", + "else{", + " return(false);", + "}", + "Let's have a go of using if statements now by making a coin-flip game", + "Create an if else statement to return heads if the flip var is zero and to return tails if it's not" + ], + "tests":[ + "assert((function(){if(myFunction() == 'heads' || myFunction() == 'tails'){return(true);}else{return(false);}})(), 'myFunction should either return heads or tails');", + "assert(editor.getValue().match(/if/g).length >= 3, 'You should have created a new if statement');", + "assert(editor.getValue().match(/else/g).length >= 2, 'You should have created a new else statement');" + ], + "challengeSeed":[ + "function myFunction(){", + " var flip = Math.floor(Math.random() * (1 - 0 + 1)) + 0;", + " //Create and if else statement here to return heads if flip is 0 otherwise return false", + " ", + "}", + "", + "(function(){return(myFunction());})();" + ], + "challengeType": 1 + }, + { + "_id":"bh1111c1c12feddfaeb6bdef", + "name":"An Intro To RegEx", + "dashedName":"waypoint-an-intro-to-regex", + "difficulty":"9.984", + "description":[ + "", + "RegEx is a powerful tool we can use to find certain words or patterns in strings", + "RegEx can look difficult at first but there's not much to getting it working", + "If we wanted to find the number of times the word \"the\" occured in the string \"The dog chased the cat\" We could use the following RegEx:", + "\/the+\/gi", + "Let's break this down a bit", + "\"The\" is the pattern we want to match", + "\"+\" means we are looking for one or more occurrences of this pattern", + "\"g\" means that it searhces the whole string", + "\"i\" means that we are ignoring the case(upper or lower) of what we are looking for", + "Let's try finding the word and in the string \"John and Alan went to the shop and got some milk\" by replacing the .+ in the currnet RegEx with something that will find the word \"and\" and count how many times it occurs" + ], + "tests":[ + "assert(test==2, 'You\\'re RegEx should have found two occurances of the word \"and\"');", + "assert(editor.getValue().match(/\\/and\\+\\/gi/), 'You should have used this RegEx to find the word \"and\"');" + ], + "challengeSeed":[ + "var test = (function(){", + " var testString = \"John and Alan went to the shop and got some milk\";", + "", + "//Do Not Modify Above", + "", + " var expression = /.+/gi;", + "", + "//Do Not Modify Below", + "", + " return(testString.match(expression).length);", + "})();(function(){return(test);})();" + ], + "challengeType": 1 + }, + { + "_id":"bh1111c1c12feddfaeb7bdef", + "name":"Finding Numbers", + "dashedName":"waypoint-finding-numbers", + "difficulty":"9.985", + "description":[ + "", + "We can use special selectors in RegEx to select a particular type of value", + "One such selector is the digit selector \\d which is used to grab the numbers in a string", + "It is used like this:", + "/\\d+/g", + "" + ], + "tests":[ + "assert(test === 2, 'Your RegEx should have found two numbers in the testString');", + "assert(editor.getValue().match(/\\/\\\\d\\+\\//gi), 'You should be using the following expression /\\d+/gi to find the numbers in the testString');" + ], + "challengeSeed":[ + "var test = (function(){", + " var testString = \"There's 3 cats but 4 dogs.\"", + "", + "//Do Not Modify Above", + "", + " var expression = /.+/gi;", + "", + "//Do Not Modify Below", + "", + " return(testString.match(expression).length);", + "})();(function(){return(test);})();" + ], + "challengeType": 1 + }, + { + "_id":"bh1111c1c12feddfaeb8bdef", + "name":"Finding WhiteSpace", + "dashedName":"waypoint-finding-whitespace", + "difficulty":"9.987", + "description":[ + "", + "We can also use selectors like \\s to find spaces in a string", + "It is used like this:", + "/\\s+/g", + "" + ], + "tests":[ + "assert(test === 7, 'Your RegEx should have found seven spaces in the testString');", + "assert(editor.getValue().match(/\\/\\\\s\\+\\//gi), 'You should be using the following expression /\\s+/gi to find the spaces in the testString');" + ], + "challengeSeed":[ + "var test = (function(){", + " var testString = \"How many spaces are there in this sentance.\";", + "", + "//Do Not Modify Above", + "", + " var expression = /.+/gi;", + "", + "//Do Not Modify Below", + "", + " return(testString.match(expression).length);", + "})();(function(){return(test);})();" + ], "challengeType": 1 } ]