Skip to content
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

Feedback #1

Open
wants to merge 10 commits into
base: feedback
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
104 changes: 104 additions & 0 deletions modules/coin.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/** Coin flip functions
* This module will emulate a coin flip given various conditions as parameters as defined below
*/

/** Simple coin flip
*
* Write a function that accepts no parameters but returns either heads or tails at random.
*
* @param {*}
* @returns {string}
*
* example: coinFlip()
* returns: heads
*
*/

export function coinFlip() {
if (Math.random() < 0.5) {
return "tails";
}
return "heads";
}

/** Multiple coin flips
*
* Write a function that accepts one parameter (number of flips) and returns an array of
* resulting "heads" or "tails".
*
* @param {number} flips
* @returns {string[]} results
*
* example: coinFlips(10)
* returns:
* [
'heads', 'heads',
'heads', 'tails',
'heads', 'tails',
'tails', 'heads',
'tails', 'heads'
]
*/

export function coinFlips(flips) {
var result = [flips];
for (var i = 0; i < flips; i++) {
result[i] = coinFlip();
}
return result;
}

/** Count multiple flips
*
* Write a function that accepts an array consisting of "heads" or "tails"
* (e.g. the results of your `coinFlips()` function) and counts each, returning
* an object containing the number of each.
*
* example: conutFlips(['heads', 'heads','heads', 'tails','heads', 'tails','tails', 'heads','tails', 'heads'])
* { tails: 5, heads: 5 }
*
* @param {string[]} array
* @returns {{ heads: number, tails: number }}
*/

export function countFlips(array) {
var headCount = 0;
var tailCount = 0;
for (var i = 0; i < array.length; i++) {
if (array[i] == "heads") {
headCount += 1;
}
if (array[i] == "tails") {
tailCount += 1;
}
}
return {
tails: tailCount,
heads: headCount,
}
}

/** Flip a coin!
*
* Write a function that accepts one input parameter: a string either "heads" or "tails", flips a coin, and then records "win" or "lose".
*
* @param {string} call
* @returns {object} with keys that are the input param (heads or tails), a flip (heads or tails), and the result (win or lose). See below example.
*
* example: flipACoin('tails')
* returns: { call: 'tails', flip: 'heads', result: 'lose' }
*/

export function flipACoin(call) {
var flip = coinFlip();
var result;
if (call == flip) result = "win";
else result = "lose";
return {call: call, flip: flip, result: result};
}


/** Export
*
* Export all of your named functions
*/
Loading