Skip to content

Instantly share code, notes, and snippets.

@MartinsOnuoha
Last active August 2, 2020 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinsOnuoha/edb6b18ae762db7d8df920c278eea495 to your computer and use it in GitHub Desktop.
Save MartinsOnuoha/edb6b18ae762db7d8df920c278eea495 to your computer and use it in GitHub Desktop.
A reusable solution to the lycanthrope log task
const journal = []
function requestEventData() {
let day = prompt('what day is it?')
let activities = prompt('what activities did you do today?')
let didJacTurn = confirm('Did you turn today?')
/**
* where activities variable above, is a string in this format: 'eat, sleep, pizza, sleep'
* .split(..) breaks the string into an array where each element is the word before a comma: ['eat', ' sleep', ' pizza', ' sleep']
* .map(..) iterates through the array and mutate each element.
* .trim() on each element removes white spaces at the beginning and end of the element: ['eat', 'sleep', 'pizza', 'sleep']
* new Set(..): creates a collections of unique values and returns a Set Object (with unique elements) Set: ['eat', 'sleep', 'pizza']
* [...]: spread operator extracts the elements from the Set object and encloses them in an Array object instead.
**/
let eventData = {
day,
activities: [...new Set(activities.split(',').map(x => x.trim()))],
didJacTurn
}
return eventData
}
/**
* Recursive function to add new entry to journal
*/
function addEntryToJournal() {
let endOfWeek = confirm('has the week ended?')
if (endOfWeek) {
determineCause(journal)
return
}
let eventJournal = requestEventData()
// push the entry to the journal array
journal.push(eventJournal)
// recursively call this function again, so this process repeats (until the basecase: endOfWeek === true @line 9, is met)
addEntryToJournal()
}
/**
* Function to determine what activities
* causes jac to turn
*/
function determineCause(selectedJournal) {
/**
* @line 54
* check if there are any entries
* journal.length returns the number of elements in the array (0 if empty)
* since (0) is a falsy value, we can treat as boolean.
*/
if (selectedJournal.length) {
/*
* @line 60:
* .filter(..) creates a new array with all elements that pass the test
* the test here is [elements where the property "didJacTurn" equals true
*/
let daysTurned = selectedJournal.filter(day => day.didJacTurn === true)
let allActivitiesOndaysTurned = []
/*
* .forEach(..) iterates over all the days jac turned
* each day is an object in this format { day: "Monday", activities: ['eat', 'sleep' ...], didJacTurn: true }
* .concat(..) merges two or more arrays and returns a new array
* we assign the result to the "allActivitiesOndaysTurned" variable
*/
daysTurned.forEach(day => {
allActivitiesOndaysTurned = allActivitiesOndaysTurned.concat(day.activities)
})
// map.entries returns the key value pair of activities and their frequency
const map = arr.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
/*
* find which entry has a value equal to daysTurned.length
* those activities are similar activities done in all days jac turned
* and would most likely be the cause of jac's predicament.
*/
console.log(map)
/**
Map(6)
[[Entries]]
0: {"eat" => 1}
1: {"sleep" => 1}
2: {"pizza" => 2}
3: {"buy bread" => 1}
4: {"breakfast" => 1}
5: {"lunch" => 1}
size: 6
**/
} else {
console.log('You made no logs this week')
}
}
addEntryToJournal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment