-
Notifications
You must be signed in to change notification settings - Fork 1
EventScoreAPI #11
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
base: ms-training-2
Are you sure you want to change the base?
EventScoreAPI #11
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//Define class here | ||
// Define class here | ||
|
||
export interface User { | ||
readonly id?: number | ||
|
@@ -82,3 +82,62 @@ export class UserAPI { | |
} | ||
} | ||
} | ||
|
||
// | ||
// | ||
// EVENT API | ||
|
||
export interface Event { | ||
readonly timestamp: number | ||
readonly eventType: "new message" | "view" | "screenshot" | ||
} | ||
|
||
export class EventScoreAPI { | ||
private score: number | ||
private eventArr: ReadonlyArray<Event> | ||
|
||
constructor(eventArr: ReadonlyArray<Event>) { | ||
this.eventArr = eventArr | ||
this.score = 0 | ||
} | ||
|
||
public calcHighestScoreArrSequence = (): ReadonlyArray<Event> => { | ||
let tempCount: number = 0 | ||
let highestScoreArr: ReadonlyArray<Event> = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While insignificant in this case, as highestScoreArr is going to have max length of 5, it is possible to solve this without storing the returned highestScoreArr, and instead just directly return it. |
||
|
||
if (this.eventArr.length <= 5) { | ||
return this.eventArr | ||
} else { | ||
this.eventArr.forEach((event, i) => { | ||
const { eventType } = event | ||
|
||
if (eventType === "screenshot") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, can definitely refactor the conversion of eventType to a scoring value. |
||
tempCount += 3 | ||
} else if (eventType === "view") { | ||
tempCount += 2 | ||
} else { | ||
tempCount += 1 | ||
} | ||
|
||
if (i > 4) { | ||
const eventTypeToSubtract = this.eventArr[i - 5].eventType | ||
|
||
if (eventTypeToSubtract === "screenshot") { | ||
tempCount -= 3 | ||
} else if (eventTypeToSubtract === "view") { | ||
tempCount -= 2 | ||
} else { | ||
tempCount -= 1 | ||
} | ||
} | ||
|
||
if (tempCount > this.score) { | ||
this.score = tempCount | ||
highestScoreArr = this.eventArr.slice(i - 4, i + 1) | ||
return highestScoreArr | ||
} | ||
}) | ||
} | ||
return highestScoreArr | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth having tests for edge cases: arrays of length 0 and 5.