Skip to content

Add Zeller's Congruence Algorithm in Math #996

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

Merged
merged 5 commits into from
Apr 27, 2022
Merged
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
26 changes: 26 additions & 0 deletions Maths/ZellersCongruenceAlgorithm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Zeller's Congruence Algorithm finds the day of the week from the Gregorian Date. Wikipedia: https://en.wikipedia.org/wiki/Zeller%27s_congruence
export const zellersCongruenceAlgorithm = (day, month, year) => {
if (typeof day !== 'number' || typeof month !== 'number' || typeof year !== 'number') {
throw new TypeError('Arguments are not all numbers.')
}
const q = day
let m = month
let y = year
if (month < 3) {
m += 12
y -= 1
}
day =
(q + Math.floor(26 * (m + 1) / 10) + (y % 100) + Math.floor((y % 100) / 4) + Math.floor(Math.floor(y / 100) / 4) + (5 * Math.floor(y / 100))) %
7
const days = [
'Saturday',
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday'
]
return days[day]
}
17 changes: 17 additions & 0 deletions Maths/test/ZellersCongruenceAlgorithm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { zellersCongruenceAlgorithm } from '../ZellersCongruenceAlgorithm'

function testZeller (day, month, year, expected) {
test('Testing on ' + day + '/' + month + '/' + year, () => {
expect(zellersCongruenceAlgorithm(day, month, year)).toBe(expected)
})
}

test('Testing on this/should/throw', () => {
expect(() => {
zellersCongruenceAlgorithm('this', 'should', 'error')
}).toThrowError(new TypeError('Arguments are not all numbers.'))
})
testZeller(25, 1, 2013, 'Friday')
testZeller(26, 1, 2013, 'Saturday')
testZeller(16, 4, 2022, 'Saturday')
testZeller(25, 4, 2022, 'Monday')