Skip to content

Instantly share code, notes, and snippets.

@ryanbsherrill
Created February 22, 2017 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanbsherrill/fd446f79e2786bca378895e5c9b93bd6 to your computer and use it in GitHub Desktop.
Save ryanbsherrill/fd446f79e2786bca378895e5c9b93bd6 to your computer and use it in GitHub Desktop.
number drills
// Area of a Rectangle
function computeArea(width, height) {
return width * height;
}
function testComputeArea() {
var width = 3;
var height = 4;
var expected = 12;
if (computeArea(width, height) === expected) {
console.log('SUCCESS: `computeArea` is working');
}
else {
console.log('FAILURE: `computeArea` is not working');
}
}
testComputeArea();
// Is Divisible
function isDivisible(divisee, divisor) {
if (divisee % divisor === 0) {
return true;
}
else {
return false;
}
}
// Tests
function testIsDivisible() {
if (isDivisible(10, 2) && !isDivisible(11, 2)) {
console.log('SUCCESS: `isDivisible` is working');
}
else {
console.log('FAILURE: `isDivisible` is not working');
}
}
testIsDivisible();
// Temperature Conversion
// C --> F
function celsToFahr(celsTemp) {
return Math.floor(celsTemp * (9/5) + 32);
}
// F --> C
function fahrToCels(fahrTemp) {
return Math.floor((fahrTemp - 32) * (5/9));
}
// Tests
function testConversion(fn, input, expected) {
if (fn(input) === expected) {
console.log('SUCCESS: `' + fn.name + '` is working');
return true;
}
else {
console.log('FAILURE: `' + fn.name + '` is not working');
return false;
}
}
function testConverters() {
var cel2FahrInput = 100;
var cel2FahrExpect = 212;
var fahr2CelInput = 32;
var fahr2CelExpect = 0;
if (testConversion(celsToFahr, cel2FahrInput, cel2FahrExpect) &&
testConversion(fahrToCels, fahr2CelInput, fahr2CelExpect)) {
console.log('SUCCESS: All tests passing');
}
else {
console.log('FAILURE: Some tests are failing');
}
}
testConverters();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment