-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (76 loc) · 2.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// The names listed below are ordered by days from Sunday
const maleAkanNames = [
"Kwame",
"Kwasi",
"Kwadwo",
"Kwabena",
"Kwaku",
"Yaw",
"Kofi"
];
const femaleAkanNames = [
"Ama",
"Akosua",
"Adwoa",
"Abenaa",
"Akua",
"Yaa",
"Afua"
];
// This function collects the data from input fields
function fetchFormData() {
let form = document.forms[0];
let day, month, year, gender;
gender = form.gender.value;
day = parseInt(form.day.value);
month = parseInt(form.month.value);
year = parseInt(form.year.value);
return [gender, day, month, year];
}
/**
* Initialize calcWeekDay function.
* The function calculates the day of the week from a specific date.
*/
function calculateWeekDay(birthDate) {
let [dayOfMonth, monthOfYear, yearOfBirth] = birthDate;
let zeroBasedCentury, yearOfCentury;
// Count Jan & Feb as months 13 and 14 of the previous year.
if (monthOfYear <= 2) {
monthOfYear += 12;
yearOfBirth -= 1;
}
// Split year to centuryCode & yearCode
zeroBasedCentury = parseInt(yearOfBirth / 100);
yearOfCentury = yearOfBirth % 100;
let dayOfWeek =
(dayOfMonth +
parseInt(
2.6 * (monthOfYear + 1) +
yearOfCentury +
parseInt(yearOfCentury / 4) +
parseInt(zeroBasedCentury / 4) +
5 * zeroBasedCentury
)) %
7;
// return dayOfWeek as a zero-based index
// dayOfWeek = (0 = Saturday, 1 = Sunday, 2 = Monday, ..., 6 = Friday)
return dayOfWeek;
}
/**
* Initialize deriveAkanName function.
* The function calls the calcWeekDay function and derives the user gender
* from the Akan Name arrays defined at the top
*/
function deriveAkanName() {
let formData = fetchFormData();
let userBirthDate, userGender, dayOfWeek;
[userGender, ...userBirthDate] = formData;
dayOfWeek = calculateWeekDay(userBirthDate);
if (userGender === "Male") {
alert("Your Akan Name is: " + maleAkanNames[dayOfWeek]);
} else {
alert("Your Akan Name is: " + femaleAkanNames[dayOfWeek]);
}
// Helps clear the input fields after retrieving the values
return false;
}