diff --git a/my-script.js b/my-script.js index 5ac970c..12d2014 100644 --- a/my-script.js +++ b/my-script.js @@ -21,46 +21,86 @@ const greeting = (name) => { /** * Write a function called `add` that returns the sum of two numbers */ - - +function add(number1, number2) { + return (number1 + number2); +} +console.log(add(2, 2)) /** * Write a function called `subtract` that returns the difference between two numbers */ - +function subtract(number1, number2) { + return (number1 - number2); +} +console.log(subtract(2, 2)) /** * Write a function called `min` that returns the smaller of two numbers */ - +function min(number1, number2) { + return (Math.min(number1, number2)); +} +console.log(min(2, 2)) /** * Write a function called `max` that returns the larger of two numbers */ - +function max(number1, number2) { + return (Math.max(number1, number2)); +} +console.log(max(2, 2)) /** * Write a function called `isEven` that takes a single value and * returns `true` if it is even and `false` if it is odd */ - +function isEven(number) { + if (number % 2 == 0) { + return (true); + } else { + return (false); + } +} +console.log(isEven(1)); /** * Write a function called `isOdd` that takes a single value and * returns `false` if it is even and `true` if it is odd */ +function isOdd(number) { + if (number % 2 == 0) { + return (false); + } else { + return (true); + } +} +console.log(isOdd(1)); /** * Write a function called `factorial` that takes a single integer and * returns the product of the integer and all the integers below it */ - - +function factorial(number) { + let result = 1; + for (let factors = 1; factors <= number; factors++) { + result = (result * factors); + } + return (result); +} +console.log(factorial(4)); /** * Write a function called `oddFactorial` that takes a single integer and * returns the product of the integer and all the integers below it, but * only if they are odd. If the starting number is even, don't include it. */ +function oddFactorial(number) { + let result = 1; + for (let factors = 1; factors <= number; factors = factors + 2) { + result = (result * factors); + } + return result; +}; +console.log(oddFactorial(4)); /** * Write a function that solves the Chessboard exercise from chapter two, @@ -68,7 +108,24 @@ const greeting = (name) => { * Instead of printing each line using `console.log()`, build the grid using * a single string and return it at the end of the function */ - + function chessboard(number){ + let size = number; + + let board = ""; + + for (let y = 0; y < size; y++) { + for (let x = 0; x < size; x++) { + if ((x + y) % 2 == 0) { + board += " "; + } else { + board += "#"; + } + } + board += "\n"; + } + return board; +} + console.log(8); /******************************************* * DO NOT CHANGE ANYTHING BELOW THIS LINE! @@ -85,4 +142,3 @@ module.exports = { oddFactorial: typeof oddFactorial === 'function' ? oddFactorial : null, chessboard: typeof chessboard === 'function' ? chessboard : null, }; -