-
Notifications
You must be signed in to change notification settings - Fork 0
/
easyanswer.js
88 lines (79 loc) · 2.45 KB
/
easyanswer.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
let answers = ["YES", "NO", "Ask me later."];
let button = $("input.button");
let chosenAnswer = answers[Math.floor(Math.random() * answers.length)];
//if there has been Ask me Later answers create a 30 sec timer for each question
// function setTimer(){
// }
//if the question asked is part of the ask later list erase it from entries
function checkQuestionRepetition(){
for (i=0; i<$("li").length; i++) {
if($( "li" ).eq(i).html() == $('#question').val()){
($("li").eq(i)).remove();
return;
}
}
}
//add to ask later list
function listLater(){
$("#later-list").append("<li>"+ $('#question').val()+ "</li>");
return;
}
//make former answer disappear when clicking the question input
function clearScreen(){
if($("#answer").length>0){
$("#answer").fadeOut(2000);
$("#attention").fadeOut(2000);
// remove bluring effect
$(".title").removeClass("blur-all");
}
//if already a entry im question make question disappear
if($('#question').val().length>0){
$('input[type=text]').select();
}
}
//randomise answer
function chooseAnswer(){
let chosenAnswer = answers[Math.floor(Math.random() * answers.length)];
//display the answer !need to be in choose answer to have the same chosenAnswer
function displayAnswer(){
$(".title").addClass("blur-all");
$("#answer").html(chosenAnswer);
$("#answer").fadeIn("slow");
if (chosenAnswer=="Ask me later."){
listLater();
return;
}
return;
}
displayAnswer()
return;
}
//check if this is a proper question
function checkQuestion(){
if ($("#question").val().includes("?")){
chooseAnswer();
return;
}
else {
// will appear big on screen with a fadeout
$("#attention").html("Ask me something with a '?'");
$("#attention").fadeIn("slow");
return;
}
}
//gives answer
function answer(){
checkQuestionRepetition();
checkQuestion();
};
$(document).ready(function(){
$("#question").keypress(function(event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
// Cancel the default action, of enter key reloading page
event.preventDefault();
// Trigger the button element with a click
$("#submit-btn").click();
}
});
})