-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
142 lines (119 loc) · 4.71 KB
/
script.js
File metadata and controls
142 lines (119 loc) · 4.71 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const printName = (feedBack) => {
// ce que je vais mettre dans mon html, je crée une fonction pour la réutiliser dans la fonction feedBackName, ça évite de se répéter
//to avoid repeat this for each condition, I will just return it in feedBackName for each condition
$('#answer').css("display", "block")
const retour = $('#answer').text("Wonderful! your babyname is " + feedBack);
}
const feedBackName = (babyNames) => {
if (babyNames.length === 0) { //if it is not in the API result, then it is original
//si il y a aucune valeur correspondante dans les resultats API
return printName("original")
}
const isOlderThan2000 = babyNames.filter(function(item) {
//to get the 4 first string
//récupérer les 4 premier élément de year
const babyYear = item.year.substring(0, 4);
//transformer en chiffre
//transform string to integer
const yearNumber = parseInt(babyYear);
// ici il faut qu'il soit supérieur à 2000
//condition > 2000, if this name is used only before the year 2000 it means is traditional
return yearNumber < 2000
//si dans mes resultats API, il y a des nom correspondant à la condition ci dessus alors l'index devrait être supérieur à zéro
//if there is a results so if its length > 0 mean true
}).length > 0
//same function but it's for > 2000 so popular
const isYoungerThan2000 = babyNames.filter(function(item) {
const babyYear = item.year.substring(0, 4);
const yearNumber = parseInt(babyYear); //même raisonnement
return yearNumber > 2000
}).length > 0
//ici on établit les conditions
//here we establish the conditions
if (isOlderThan2000 && isYoungerThan2000) {
return printName('timeless')
} else if (isOlderThan2000) {
return printName('traditional')
} else if (isYoungerThan2000) {
return printName('Popular')
}
}
//first request API to get the name
function getBabyName(name) { // API request function
$.ajax({
url: `https://data.novascotia.ca/resource/emf8-vmuy.json?`,
type: "GET",
data: {
"$limit": 10000,
"$$app_token": "whW6hkBV9XeQn2RtJhqKb9pzA",
"first_name": name.toUpperCase(),
}
}).then(function(name) { //when I got my results
console.log(name)
feedBackName(name); //then run feedBack function
});
}
//second request to get the sex, I had issue to do with only one request function so I try this and it works
function getGender(sex) { // API request function
$.ajax({
url: `https://data.novascotia.ca/resource/emf8-vmuy.json?`,
type: "GET",
data: {
"$limit": 10000,
"$$app_token": "whW6hkBV9XeQn2RtJhqKb9pzA",
"sex": sex
}
}).then(function(sex) { //when I got my results
nameByGender(sex);
//then run feedBack function
});
}
function ideaButtonEvent() {
// code for the event listerner
$('#ideaButton').on('click', function(event) {
event.preventDefault();
console.log('hi')
// when the gender is checked
const ideaName = $('input[type=radio]:checked').val();
// make your ajax call (passing the ideaName to it)
getGender(ideaName); //
});
}
//this function is to get name randomly according to the gender choice
const nameByGender = (genderName) => {
const numberName = genderName.length;
console.log(numberName)
for (i = 0; i < numberName; i++) {
const randNumberName = genderName[Math.floor(Math.random() * numberName)];
const sexName = randNumberName.first_name;
console.log(sexName)
return getNameBySex(sexName)
}
}
//make it appear on the page
const getNameBySex = (backnames) => {
const retour = $("#randyName").append(function() {
$(this).text(backnames);
$('#listnames').css("display", "block")
$('#answer').css("display", "none")
});
}
// this function is for the second form, the one which check what kind of name is your babyname
function checkButtonEvent() {
// code for the event listerner
$('#check-button').on('click', function(event) {
event.preventDefault();
// get the information - baby name
const requestedName = $('#myname').val();
// make your ajax call (passing the name to it)
getBabyName(requestedName); //
$('#listnames').css("display", "none")
});
}
$(function() {
init()
})
function init() {
checkButtonEvent();
ideaButtonEvent();
}