Skip to content

Instantly share code, notes, and snippets.

@CodeSigils
Last active October 17, 2016 09:23
Show Gist options
  • Save CodeSigils/a241f5ab801eb7f4fa34ec68fa86a5e1 to your computer and use it in GitHub Desktop.
Save CodeSigils/a241f5ab801eb7f4fa34ec68fa86a5e1 to your computer and use it in GitHub Desktop.
Number Drills
// =================== Square Area =================== //
// Create a function called computeArea that takes two arguments: width and height.
// It returns the area of a square whose width is width and height is height.
// So computeArea(2, 2) would return 4, and computeArea(3, 5) would return 15.
function computeArea(width, height) {
return width * height;
}
// tests
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();
// =================== Temperature Conversion =================== //
// Create two functions, one called celsToFahr that converts Celsius to Fahrenheit,
// and another called fahrToCels that converts Fahrenheit to Celsius.
// celsToFahr takes one argument, celsTemp, and fahrToCels takes one argument, fahrTemp.
function celsToFahr(celsTemp) {
var c2Fahr = celsTemp * 9 / 5 + 32;
return c2Fahr;
}
function fahrToCels(fahrTemp) {
var fahr2Cels = (fahrTemp - 32) * 5 / 9;
return fahr2Cels;
}
// 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();
// =================== Is Divisible =================== //
// Write a function called isDivisible that takes two arguments: divisee and divisor.
// This function should return true if divisee can be divided by divisor with no remainder,
// otherwise it should return false.
// So isDivisible(18, 3) should be true, while isDivisible(15, 4) should be false.
// Note that we haven't covered boolean values in depth in this lesson yet.
// All you need to know to complete this drill is that true is represented by the keyword true
// and false with the keyword false.
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment