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
78 changes: 70 additions & 8 deletions my-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,116 @@ const greeting = (name) => {
/**
* Write a function called `add` that returns the sum of two numbers
*/

const add = (x,y) => x+y;

/**
* Write a function called `subtract` that returns the difference between two numbers
*/


const subtract = (x, y) => x - y;
/**
* Write a function called `min` that returns the smaller of two numbers
*/

const min = function (x,y) {
if (x === y) {
return null;
} else if (x > y) {
return y;
} else {
return x;
}
}

/**
* Write a function called `max` that returns the larger of two numbers
*/

const max = function (x,y) {
if (x===y) {
return null;
} else if (x > y){
return x;
} else {
return y;
}
}
/**
* Write a function called `isEven` that takes a single value and
* returns `true` if it is even and `false` if it is odd
*/

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

/**
* Write a function called `isOdd` that takes a single value and
* returns `false` if it is even and `true` if it is odd
*/

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

/**
* Write a function called `factorial` that takes a single integer and
* returns the product of the integer and all the integers below it
*/
const factorial = function (n) {
let total = 1;
for (i=1; i<= n; i++){
total*=i;
} return total;
}


/**
* 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.
*/

const oddFactorial = function (n) {
let total = 1;
for (i=1; i<= n; i++){
if ( i % 2 === 1) {
total*=i;
} else if (i % 2 === 0) {
total*=1;
}
} return total;
}

/**
* 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
*/

const chessboard = function (n) {
let board='';
for (let line=1; line<= n; line++){
for (let character=1; character <= n; character++){
if(line % 2 !==0){
//odd Lines
board += character % 2 !== 0 ? ' ' : '#';
} else {
//even Lines
board += character % 2 !== 0 ? '#': ' ';
}
}
board += '\n';
}
return board;
}

/*******************************************
* DO NOT CHANGE ANYTHING BELOW THIS LINE!
Expand Down