-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (81 loc) · 3.17 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Function to get the current time and return hours/minutes for use in other functions
function getCurrentTime() {
const now = new Date();
const hours = now.getHours().toString();
const minutes = now.getMinutes().toString().padStart(2, '0');
return { hours, minutes };
}
// Function to display current time
function showTime() {
const timeElement = document.getElementById('time');
const { hours, minutes } = getCurrentTime();
timeElement.textContent = `Current Time: ${hours}:${minutes}`;
}
// Function to fetch the Bible text file and parse verses
async function loadBible() {
try {
const response = await fetch('http://localhost:5500/bibles/kjv.txt');
const bibleText = await response.text();
const verses = parseBibleText(bibleText);
return verses;
} catch (error) {
console.error('Error loading Bible text:', error);
}
}
// Function to parse the Bible text into an array of verses
function parseBibleText(bibleText) {
const lines = bibleText.split('\n');
const verses = lines.map(line => {
// Match and extract chapter and verse using regex
const match = line.match(/(\d+):(\d+)/);
if (match) {
const chapter = match[1];
const verse = match[2];
return { chapter, verse, text: line };
} else {
// Handle lines that might not contain chapter and verse information
return null;
}
}).filter(Boolean); // Remove any null entries
return verses;
}
// Function to get a random verse
function getRandomVerse(verses) {
const randomIndex = Math.floor(Math.random() * verses.length);
return verses[randomIndex];
}
// Function to get a verse based on the current hour and minute
function getVerseByTime(verses) {
const { hours, minutes } = getCurrentTime();
const verseElement = document.getElementById('verse');
// Remove leading zeroes
const cleanHours = hours.replace(/^0+/, '');
const cleanMinutes = minutes.replace(/^0+/, '');
let matchingVerse;
if (cleanMinutes === '0') {
// If the minute is :00, get a random verse
matchingVerse = getRandomVerse(verses);
} else {
// Filter verses that match the current time
const matchingVerses = verses.filter(verse => verse.chapter === cleanHours && verse.verse === cleanMinutes);
if (matchingVerses.length > 0) {
// Pick a random verse from the matching verses
const randomIndex = Math.floor(Math.random() * matchingVerses.length);
matchingVerse = matchingVerses[randomIndex];
} else {
// If no matching verse is found, get a random verse
matchingVerse = getRandomVerse(verses);
}
}
verseElement.textContent = matchingVerse ? `${matchingVerse.text}` : "No matching verse found.";
}
// Initialize the app
async function init() {
showTime();
setInterval(showTime, 60000); // Update time every minute
const verses = await loadBible();
getVerseByTime(verses);
setInterval(() => getVerseByTime(verses), 60000); // Update verse every minute
}
// Run the init function when the window loads
window.onload = init;