-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
75 lines (61 loc) · 2.4 KB
/
app.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
/*
If you have ideas on improving the code, feel free to do so, by either
introducing a new way to handle functions, renaming or simply commenting
alongside variables to help understand what the code is doing.
New features are welcomed.
*/
import { contributorsInformation } from "./modules/data/contributorsInformation.mjs";
import { greetingsList } from "./modules/data/greetingsList.mjs";
const greetingsTitle = document.getElementById("title");
const travelButton = document.getElementById("travel-button");
const contributorContainer = document.getElementById("contributor-container")
let greetingsIndex = 0
// used for fetching said contributors name, github and links
let contributorIndex;
function generateRandomNumber(reference) {
return Math.floor(Math.random() * reference.length);
}
function startProgram(){
// reset parent container
contributorContainer.replaceChildren('')
contributorIndex = chooseRandomContributor(contributorsInformation);
greetingsTitle.innerHTML= loopThroughGreetings(greetingsList)
travelButton.href = getRandomLinkFromContributor(contributorsInformation)
contributorContainer.appendChild(getContributorsNameAndGithub(contributorsInformation))
}
// generates an index for a contributor in the list
function chooseRandomContributor(list){
const randomContributor = generateRandomNumber(list);
return randomContributor
}
function getRandomLinkFromContributor(list){
const contributorsLinks = list[contributorIndex].links;
const randomLink = contributorsLinks[generateRandomNumber(contributorsLinks)];
return randomLink;
}
function loopThroughGreetings(list){
const greeting = list[greetingsIndex]
greetingsIndex++
if (greetingsIndex >= list.length) greetingsIndex = 0
return greeting
}
/*
fetches contributor's details and creates a button the leads
to their github account. depends on contributorIndex
*/
function getContributorsNameAndGithub(list){
const container = document.createElement('div')
const aTag = document.createElement('a')
const pTag = document.createElement('p')
const name = list[contributorIndex].name
const github = list[contributorIndex].github
const message = 'contributor - '
container.style.display = 'flex'
aTag.append(name)
aTag.href = github
pTag.append(message)
container.appendChild(pTag)
container.appendChild(aTag)
return container
}
setInterval(startProgram, 2000)