-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlingo.js
117 lines (102 loc) · 2.84 KB
/
lingo.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// enter here the correct answer
var correctAnswer = 'html5';
// how many lines to guess?
var numberOfLines = 5;
$( document ).ready(function() {
prepareLingo();
$( "input" ).keyup(function(event) {
var targetId = event.target.id;
var inputVal = $("#"+targetId).val();
var linesAndRowsArray = targetId.split('_');
var line = linesAndRowsArray[0];
var row = linesAndRowsArray[1];
if(inputVal.length ==1) {
row++;
$("#"+line+"_"+row).focus();
isWordCompleted(line);
}
});
});
function isWordCompleted(line)
{
var isWord;
var makeWordArray = new Array();
for(var i =1; i<6; i++) {
if($("#"+line+"_"+i).val()!='') {
isWord = true;
makeWordArray.push(i);
} else {
isWord = false;
}
}
// check and validate the row
if(isWord == true) {
var inputWord ='';
makeWordArray.forEach(function(item) {
inputWord +=$("#"+line+"_"+item).val();
});
var wordGuessed = checkWord(inputWord,line);
if(wordGuessed == false) {
setNextLineActive(line);
} else {
// display, 1337 you have won :-)
}
}
return isWord;
}
function checkWord(inputWord,line)
{
var checkGoodWord = correctAnswer.split("");
var wordGuessed = false;
var isGoodWord;
for(var i =0; i<inputWord.length; i++) {
var cube = (i + 1);
if(i == 0 ) {
isGoodWord = inputWord[0];
} else {
isGoodWord += inputWord[i];
}
if(isGoodWord == correctAnswer) {
wordGuessed = true;
markWordAsGuessed(line);
} else {
if(cube >1) {
if(checkGoodWord[i] == inputWord[i]) {
$("#"+line+"_"+cube).css("background-color","green").prop('disabled',true);
} else {
$("#"+line+"_"+cube).css("background-color","red").prop('disabled',true);
}
}
}
}
return wordGuessed;
}
function setNextLineActive(line)
{
var nextLine = parseInt(line) + 1;
for(var i = 1; i<6; i++) {
$("#"+nextLine+"_"+i).prop('disabled',false);
}
if(line < numberOfLines) {
line++;
$("#"+line+"_2").focus();
}
}
function markWordAsGuessed(line)
{
for(var i = 1; i<6; i++) {
$("#"+line+"_"+i).css("background-color","green").prop('disabled',true);
}
}
function prepareLingo()
{
var getFirstWord = correctAnswer.split("");
for(var i =1; i< (numberOfLines +1); i++) {
$("#"+i+"_1").val(getFirstWord[0]).css("background-color","green").prop('disabled',true);
if(i > 1) {
for (var x= 1; x < 6; x++) {
$("#"+i+"_"+x).prop('disabled',true);
}
}
}
}