Created
May 23, 2017 03:42
-
-
Save mmeigooni/b2f1a3f6e8ba5e225c5fa26e38aec327 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Super basic assertion | |
// console.log(square(2) === 4); | |
// Now, let's try writing an assertion that gives us a little more information about what's going on. | |
/* Write a function, assertEqual, that takes in 3 arguments: | |
- An invocation of the function you're testing | |
- The expected output of the function you're testing | |
- a test name for the test (string) | |
Output: | |
- If the invocation is equivalent to the output, console.log 'Test passed' | |
- If it isn't, output some error message (make sure to include the test name in the error message) | |
Added requirement for the function, square. | |
- If a string is passed in, return an error message | |
*/ | |
var square = function(num) { | |
if (typeof num === 'string') { | |
return 'Not a number! You passed in a string.'; | |
} | |
return num * num; | |
} | |
function assertEqual(actual, expected, testName) { | |
if (actual === expected) { | |
return 'Test Passed'; | |
} else { | |
return testName + ' we expected: ' + expected + ' but we got ' + actual; | |
} | |
} | |
// console.log(assertEqual(square(3), 9, 'test square')); | |
/* Write another function, assertEqual2, where it takes in two arguments: | |
- An expected behavior (should evaluate to a boolean) | |
- A description of the correct behavior | |
It should output: | |
- 'Test Passed' if the expected behavior is true | |
- The description of correct behavior if the expected behavior is false | |
*/ | |
function assertEqual2(expectedBehavior, descriptionOfBehavior) { | |
if (expectedBehavior) { | |
console.log('Test Passed'); | |
} else { | |
console.log('Failed: ' + descriptionOfBehavior) | |
} | |
} | |
assertEqual2(square(3) === 9, 'Passed in 3, expected square to return 9.'); | |
assertEqual2(square(-3) === 9, 'Passed in -3, expected square to return 9.') | |
assertEqual2(square(1/3) === 1/9, 'Passed in 1/3, expected square to return 1/9.'); | |
assertEqual2(square('hello') === 'Not a number! You passed in a string.', 'Passed in hello, expected square to return a long string'); | |
/* | |
What are some other I/O (inputs/outputs) you would want to test in a square function? | |
- Negative | |
- Fractions | |
- Decimals | |
- String | |
- Undefined | |
- Zero | |
- Really big numbers | |
- Floating point numbers | |
*/ | |
// Write a function called `average` that takes an array of numbers as a parameter and returns the average of those numbers. | |
// input: an Array of numbers, e.g. [1, 2, 3] | |
// output: a Number, e.g. 2 | |
function average(numbers) { | |
var total = 0; | |
for (var i = 0; i < numbers.length; i++) { | |
total += numbers[i]; | |
} | |
return total / numbers.length; | |
} | |
assertEqual2(average([1, 2, 3]) === 2, 'The array of numbers, [1, 2, 3], should have returned the average value of 2'); | |
console.log(JSON.stringify([1, 2, 3]) === JSON.stringify([1, 2, 3])); | |
/* Write a function called assertArraysEqual that: | |
- Takes in two arguments, each one is an array | |
- Returns true if the arrays are equal and false if they are not | |
- Do not use JSON.stringify to do this | |
- Do not use .toString to do this | |
- Do not use String() to do this | |
*/ | |
function assertArraysEqual(array1, array2) { | |
// iterate through each array compare each value of the first array to the second | |
var areEqual = true; | |
if (array1.length !== array2.length) { | |
return !areEqual; | |
} | |
for (var i = 0; i < array1.length; i++) { | |
if (array1[i] !== array2[i]) { | |
return !areEqual; | |
} | |
} | |
return areEqual; | |
} | |
function areArraysEqual(array1, array2) { | |
var areEqual = true; | |
for (var i = 0; i < array1.length; i++) { | |
if (array1[i] !== array2[i]) { | |
areEqual = false; | |
} | |
} | |
var areEqualLength = false; | |
if (array1.length === array2.length) { | |
areEqualLength = true; | |
} | |
return areEqual && areEqualLength; | |
} | |
console.log(assertArraysEqual([2, 3, 5], [1, 6, 5])); | |
// Using TDD, write a function that takes in an array of classmates' names and returns a new array with only names that are 6 characters or longer | |
var names = ['George', 'Ting', 'Gordon', 'William', 'Dennis']; | |
function longNames(names) { | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment