Skip to content
Open
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
76 changes: 66 additions & 10 deletions my-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,111 @@ 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,
* https://eloquentjavascript.net/02_program_structure.html#i_swb9JBtSQQ
* 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!
Expand All @@ -85,4 +142,3 @@ module.exports = {
oddFactorial: typeof oddFactorial === 'function' ? oddFactorial : null,
chessboard: typeof chessboard === 'function' ? chessboard : null,
};