Skip to content

finished function exercises #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,31 @@
* @return {string} the number as a string
*/

function numberToString(number){
return number.toString();
}


/**
* Adds one to a given number.
* @param {number} n
* @return {number}
*/

function increase(number){
return number + 1;
};


/**
* Subtracts one from a given number.
* @param {number} n
* @return {number}
*/

function decrease(number){
return number - 1;
};

/**
* Adds two numbers.
Expand All @@ -26,6 +37,10 @@
* @return {number} the sum
*/

function add(x, y){
return x + y;
}


/**
* Subtracts the second number from the first.
Expand All @@ -34,6 +49,10 @@
* @return {number} the difference
*/

function subtract(x, y){
return x - y;
}


/**
* Multiplies two numbers.
Expand All @@ -42,6 +61,9 @@
* @return {number} the product
*/

function multiply(x, y){
return x*y;
}

/**
* Divides the first number by the second.
Expand All @@ -50,13 +72,21 @@
* @return {number} the quotient
*/

function divide(x,y){
return x/y;
}


/**
* Multiplies a number by itself.
* @param {number} x, number to be squared
* @return {number} squared
*/

function square(number){
return number*number;
}


/**
* Performs a mathematical operation on two numbers.
Expand All @@ -67,6 +97,25 @@
* @return {number} the result
*/

function calculate(string, x, y){
if(string === "add"){
var add = x + y;
console.log(x + " + " + y + " = " + add);
return add;
} else if(string === "subtract"){
var sub = x - y;
console.log(x + " - " + y + " = " + sub);
return sub;
} else if(string === "multiply"){
var mult = x*y;
console.log(x + " * " + y + " = " + mult);
return mult;
} else if(string === "divide"){
var div = x/y;
console.log(x + " / " + y + " = " + div);
return div;
}
}

/**
* Returns true if `a` is greater than `b`.
Expand All @@ -75,6 +124,13 @@
* @return {boolean} `a` is larger than `b`
*/

function isGreaterThan(x,y){
if(x>y){
return true;
} else{
return false;
}
}

/**
* Returns true if `a` is less than `b`.
Expand All @@ -83,6 +139,13 @@
* @return {boolean} `a` is smaller than `b`
*/

function isLessThan(x,y){
if(x<y){
return true;
} else{
return false;
}
}

/**
* Returns true if `a` and `b` are equal.
Expand All @@ -91,6 +154,13 @@
* @return {boolean} the numbers are equal
*/

function areEqual(x,y){
if(x===y){
return true;
} else{
return false;
}
}

/**
* Returns the smallest value of two numbers.
Expand All @@ -99,6 +169,13 @@
* @return {number} the smallest number
*/

function minimum(x,y){
if(x>y){
return y;
} else{
return x;
}
}

/**
* Returns the largest value of two numbers.
Expand All @@ -107,20 +184,41 @@
* @return {number} the largest number
*/

function maximum(x,y){
if(x>y){
return x;
} else{
return y;
}
}

/**
* Returns true if `n` is even.
* @param {number} n
* @return {boolean} the number is even
*/

function isEven(n){
if(n%2 === 0){
return true;
} else{
return false;
}
}

/**
* Returns true if `n` is odd.
* @param {number} n
* @return {boolean} the number is odd
*/

function isOdd(n){
if(n%2 !== 0){
return true;
} else{
return false;
}
}

/**
* Returns a letter grade.
Expand All @@ -134,6 +232,21 @@
* @return {string} the score represented as a letter grade
*/

function letterGrade(score, total){
var x = score/total;
if(x >= 0.90){
return "A";
} else if(x >= 0.80){
return "B";
} else if(x >= 0.70){
return "C";
} else if(x >= 0.60){
return "D";
} else if(x >= 0 && x <=0.59){
return "F";
}
}


/**
* Checks if a `restaurant` object has a `reviews` property.
Expand All @@ -143,6 +256,15 @@
* @return {object} restaurant
*/

function incrementReviews(restaurant){
if(typeof restaurant.reviews != "undefined"){
restaurant.reviews++;
} else{
restaurant.reviews = 1;
}
return restaurant;
}


/**
* Joins two strings with a space.
Expand All @@ -151,6 +273,10 @@
* @return {string} joined the words joined with a space
*/

function combine(word1, word2){
return word1 + " " + word2;
}


/**
* Returns a circle object with the properties `circumference` and `area`.
Expand All @@ -160,3 +286,10 @@
* @return {object} circle
*/

function createCircle(radius){
var circle = {};
circle.circumference = 2*radius*Math.PI;
circle.area = Math.PI*radius*radius;
return circle;
}

Loading