-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainScript.js
More file actions
91 lines (81 loc) · 4.09 KB
/
mainScript.js
File metadata and controls
91 lines (81 loc) · 4.09 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
$(function(){
//The objects that hold all the question sets
var questionSetOne = {
matchStrings: ["string1", "string2", "string3"],
skipStrings: ["skip1", "skip2", "skip3"]
};
var questionSetTwo = {
matchStrings: ["sentence4", "sentence5", "sentence6"],
skipStrings: ["skip4", "skip5", "skip6"]
};
//Global array variables
var questionArray = [questionSetOne, questionSetTwo]; //Array to get an index of all questions
var counter = -1; //Counter to loop through the array above
//Global score calculator variables
var previous_length = 0; //this variable holds the previous length of user input
var current_length = 0; //this variable holds the current length of user input
var points = 1000; //this variable holds the final score for the user after changes
//When play button is clicked, present the next question set
$('.playButton').click(function(){
nextQuestionSet();
});
//When input is changed....update score and test for correctness
$('.inputArea').on('input', function(e){
scoreCalc(); //update score
$('.scoreBoard').html(`${points}`); //push score to html
var user_regex_input = new RegExp($('.inputArea').val()); //convert string to regex
regexTest(questionArray[counter].matchStrings, questionArray[counter].skipStrings, user_regex_input);
})
//Local functions to be called when triggered
function scoreCalc() {
var regex_input = $('.inputArea').val().split(''); //split user's input into an array of letters
current_length = regex_input.length; //this variable will equal the input length
if (current_length > previous_length) { //if user has typed a character, decrease points by 10
var increment = current_length - previous_length;
for (i = 0; i < increment; i++) { //to account for copy and pasting regex
points -= 10;
}
} else if (current_length < previous_length) { //if user has removed a character, increase points by 5
var decrement = previous_length - current_length;
for (i = 0; i < decrement; i++) { //to account for highlight deleting
points += 5;
}
}
previous_length = current_length; //give previous_length a new value = current_length so tracking can start over
}
function regexTest(match_array, skip_array, input) {
var match = 0; //this variable tracks the number of match errors
var skip = 0; //this variable tracks the number of skip errors
for (var x = 0, y = match_array.length; x < y; x++) {
if (input.test(match_array[x]) == false) { //if any of the strings_to_match are false, increase match error count
match += 1;
}
}
for (var x = 0, y = skip_array.length; x < y; x++) {
if (input.test(skip_array[x]) == true) { //if any of the strings_to_skip are true, increase skip error count
skip += 1;
}
}
if (match > 0 || skip > 0) { //if any errors occurred, regex isn't correct
console.log('number of match errors: ' + match);
console.log('number of skip errors: ' + skip);
console.log('Regex does not meet requirements');
} else { //no errors means regex is correct
console.log('Regex meets all requirements');
$('.message').html('You win!')
}
}
function nextQuestionSet() {
counter += 1;
$('.matchArea').empty();
//append all the match strings to the textarea
for (i = 0; i < questionArray[counter].matchStrings.length; i++) {
$(`<li>${questionArray[counter].matchStrings[i]}</li>`).appendTo('.matchArea');
}
//append all the skip strings to the textarea
for (i = 0; i < questionArray[counter].skipStrings.length; i++) {
$(`<li>${questionArray[counter].skipStrings[i]}</li>`).appendTo('.matchArea');
}
}
})
//regex input history