-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.js
75 lines (61 loc) · 1.72 KB
/
ai.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
const rumus = document.getElementById("rumusEl");
let text, prompt;
function ganti(str, obj) {
for (const x in obj) {
str = str.replace(new RegExp(x, 'g'), obj[x]);
}
return str;
};
function interval(fn, t) {
fn();
return(setInterval(fn, t));
}
var i = 0
function timer() {
i++;
var time = parseFloat(i/10);
$("#timer").html(time + "s");
}
setInterval(timer, 100);
function hitung() {
fetch('https://api-mininxd.vercel.app/gemini/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: `${prompt}`
})
}).then(res => {return res.json()})
.then(data => {
console.log(data);
const result = ganti(data.text, {
'"' : "", '\n': ' <br> ', 'add':'+'
});
$('.loaderWrapper').css("display","none");
$('#donate').css("display","flex");
$('#resultEl').html('<md-block>' + result + '</md-block>');
}).catch(e => {
console.log(e);
$('.loaderWrapper').css("display","none");
$('#resultEl').html('<h2>Failed to Fetch</h2><ul> <li>Cek kembali jaringan kamu</li> <li>koneksi ke AI gagal</li> <li>coba refresh halaman</li> </ul>');
});
}
$('.modalClose').on('click', function() {
$('.loaderWrapper').css("display","block");
$('#resultEl').html('');
$('#donate').css("display","none");
$('.modal').css('display', 'none');
});
$('.openModal').on('click', function() {
if(rumus.textContent.length == 0) {
alert("Isi rumus terlebih dahulu");
} else {
i = 0;
//fix pertambahan
$('.modal').css('display', 'block');
text = "kerjakan soal matematika saya, ini rumusnya " + rumus.textContent + ", dan jelaskan penyelesaian rumus tersebut";
prompt = text;
hitung();
}
});