Skip to content

Commit 5641b6f

Browse files
authored
Add Zeller's Congruence Algorithm in Math (TheAlgorithms#996)
1 parent 7881cb5 commit 5641b6f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Maths/ZellersCongruenceAlgorithm.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Zeller's Congruence Algorithm finds the day of the week from the Gregorian Date. Wikipedia: https://en.wikipedia.org/wiki/Zeller%27s_congruence
2+
export const zellersCongruenceAlgorithm = (day, month, year) => {
3+
if (typeof day !== 'number' || typeof month !== 'number' || typeof year !== 'number') {
4+
throw new TypeError('Arguments are not all numbers.')
5+
}
6+
const q = day
7+
let m = month
8+
let y = year
9+
if (month < 3) {
10+
m += 12
11+
y -= 1
12+
}
13+
day =
14+
(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))) %
15+
7
16+
const days = [
17+
'Saturday',
18+
'Sunday',
19+
'Monday',
20+
'Tuesday',
21+
'Wednesday',
22+
'Thursday',
23+
'Friday'
24+
]
25+
return days[day]
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { zellersCongruenceAlgorithm } from '../ZellersCongruenceAlgorithm'
2+
3+
function testZeller (day, month, year, expected) {
4+
test('Testing on ' + day + '/' + month + '/' + year, () => {
5+
expect(zellersCongruenceAlgorithm(day, month, year)).toBe(expected)
6+
})
7+
}
8+
9+
test('Testing on this/should/throw', () => {
10+
expect(() => {
11+
zellersCongruenceAlgorithm('this', 'should', 'error')
12+
}).toThrowError(new TypeError('Arguments are not all numbers.'))
13+
})
14+
testZeller(25, 1, 2013, 'Friday')
15+
testZeller(26, 1, 2013, 'Saturday')
16+
testZeller(16, 4, 2022, 'Saturday')
17+
testZeller(25, 4, 2022, 'Monday')

0 commit comments

Comments
 (0)