MASTER MIND MASTER MIND mastermind 1 Number Game there are 4 need to guess ,numbers from 0 to 9. show result by colour green, orange, red. Green means correct . Orange means this number is correct but in wrong place . Red means this is incorrect at all. Numbers: submit show results result: The correct answer is: you need restart this guess game number of green number of yellow

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
<!DOCTYPE html>
<html lang="en">
<head>
<title>MASTER MIND</title>
<link rel="stylesheet" type="text/css" href="1.css" />
</head>
<script type="text/javascript">
function MakeDot(colour) {
var string = '<span class="dot" style="background-color:';
switch (colour) {
case 'red':
string += '#ff0000';

 

break;

 

case 'green':
string += '#00cc00';

 

break;

 

case 'orange':
string += '#ff9900';

 

break;

 

default:
string += '#bbb';

 

break;
}

 

string += ';"></span>';

 

return string;
}
</script>
<body>
<div class="nav" id="nav">
<h2>MASTER MIND</h2>
<li><a id="game" href="index.html" target="_self">mastermind 1</a></li>
</div>
<div class="content" id="content">
<h1>Number Game</h1>
<div id="info">
there are 4 need to guess ,numbers from 0 to 9.<br />
show result by colour green, orange, red. <br />
<script>
document.write(MakeDot('green'));
</script>
Green means correct .<br />
<script>
document.write(MakeDot('orange'));
</script>
Orange means this number is correct but in wrong place .<br />
<script>
document.write(MakeDot('red'));
</script>
Red means this is incorrect at all.<br /><br />
</div>
<form id="form">
Numbers:
<input
type="number"
min="1000"
max="9999"
maxlength="1"
size="1"
id="number1"
autofocus
required
/>
<button id="evalBtn">submit</button>
</form>
<article class="d-flex">
<span class="ml-20 w-10"
>show <br />
results</span
>
<div class="results">
<h2 class="mt-0">result:</h2>
<span id="results"></span><br />
<span id="correctAnsSpan" class=""
>The correct answer is: <span id="txtcorrectans"></span> you need
restart this guess game
</span>
</div>
<span class="w-10"
>number of green
<br /><span id="txtcorrect"></span>
</span>
<span class="w-10"
>number of yellow
<br /><span id="txtincorrect"></span>
</span>
</article>
</div>
<script type="text/javascript">
// added event listener for evalBtn
let totalcorrect = 0;
let totalincorrect = 0;
document
.getElementById('evalBtn')
.addEventListener('click', function (e) {
var form = document.getElementById('form');

 

if (!form.checkValidity()) {
return true;
}
e.preventDefault();
submit();
document.getElementById('number1').value = '';
});
// add enter key as trigger:
var input = [document.getElementById('number1')];

 

// on pressing of enter when focused in input box,

 

// the button action will be initiated

 

for (let i = 0; i < input.length; i++) {
input[i].addEventListener('keyup', function (event) {
event.preventDefault();

 

let inputsSplitted = (input[0].value + '').split('');

 

if (inputsSplitted.length != 4) {
document.getElementById('evalBtn').disabled = true;
} else {
document.getElementById('evalBtn').disabled = false;
}
});
}
// generate guessing numbers:
var goal = [];

 

var length = 4;

 

goal[0] = Math.floor(Math.random() * 10);

 

for (let i = 1; i < length; ) {
var val = Math.floor(Math.random() * 10);

 

var flag = true;
// making sure that new number generated must be different from
for (let j = 0; j < i; j++) {
if (goal[j] == val) {
flag = false;

 

break;
}
}

 

if (flag == false) {
} else {
goal[i] = val;

 

i++;
}
}
console.log(goal, '%%%%%%%%%%%%%%%%%%');
// * Setting up limit for guess *
const guessLimit = 10;
var roundCounter = 0;
function submit() {
// check inputs for validity:
if (CheckInputs() == false) {
return;
}
// if guess limit reached

 

roundCounter++;
// prompting the user to tell how many guesses are left
alert(
`You have guessed ${roundCounter} times, you can guess ${
guessLimit - roundCounter
} times more `,
);
console.log(guessLimit, roundCounter);
if (guessLimit - roundCounter == 0) {
document.getElementById('correctAnsSpan').style.display = 'block';
document.getElementById('txtcorrectans').innerHTML = `${goal}`;
}

 

// save guessed numbers:
var guess = [];
// splitting the input so that if input was 1234 then it will split like [1,2,3,4]
let inputsSplitted = (input[0].value + '').split('');
for (let i = 0; i < inputsSplitted.length; i++) {
guess[i] = parseInt(inputsSplitted[i]);
}
// compare guessed numbers:
var guessCopy = guess.slice();
var goalCopy = goal.slice();
var evaluation = [];
let greencorrect = 0;
for (let i = 0; i < guessCopy.length; i++) {
if (
guessCopy[i] == goalCopy[i] &&
goalCopy[i] !== 'X' &&
guessCopy[i] !== 'X'
) {
evaluation[i] = 'green';
goalCopy[i] = 'X';
guessCopy[i] = 'X';
greencorrect += 1;
}
}
document.getElementById('txtcorrect').innerHTML += `${greencorrect}` + '<br>';
//count number of orange:
let orangecorrect = 0;
for (let i = 0; i < guessCopy.length; i++) {
if (goalCopy.indexOf(guessCopy[i]) != -1 && guessCopy[i] !== 'X') {
goalCopy[goalCopy.indexOf(guessCopy[i])] = 'X';
evaluation[i] = 'orange';
orangecorrect += 1;
} else if (
goalCopy.indexOf(guessCopy[i]) === -1 &&
guessCopy[i] !== 'X'
) {
//goalCopy[i] = "X";
evaluation[i] = 'red';
}
}
document.getElementById('txtcorrect').innerHTML += `${greencorrect}` + '<br>';
var correctCount = 0;
console.log(evaluation);
let bol = evaluation.includes('red');//line 227
--------------------------------
for this html ,i want to separate  it into one html file and 1 js fiel ,but i cant do this work ,can anyone help me ?i dont know why my separate is wrong .
 
document.getElementById('evalBtn').disabled
}
}
function CheckInputs() {
var all_filled
var num_of_chars_ok
var only_nums
let inputsSplitted
for (i = 0; i < inputsSplitted.length; i++) {
if (inputsSplitted[i]
all_filled
break;
268
true;
269
270
271
272
true;
=
273
= true
274
true;
275
(input [0]. value + '").split();
=
276
") {
277
==
278
false;
279
}
if (inputsSplitted.length != 4) {
num_of_chars_ok
280
281
282
false;
283
break;
}
if (isNaN(inputsSplitted[i])) {
only_nums
break;
284
285
286
false;
287
}
}
if (all_filled
alert('You ha ve to input 4 numbers');
288
289
290
false) {
==
291
292
return false;
}
if (num_of_chars_ok = false) {
alert('Only four number per input field allowed');
return false;
293
294
295
296
}
if (only_nums
alert('You cannot use letters etc. ');
297
298
false) {
==
299
300
301
return false;
302
}
303
304
return true;
305
}
S/script>
</body>
306
307
308
</html>
Transcribed Image Text:document.getElementById('evalBtn').disabled } } function CheckInputs() { var all_filled var num_of_chars_ok var only_nums let inputsSplitted for (i = 0; i < inputsSplitted.length; i++) { if (inputsSplitted[i] all_filled break; 268 true; 269 270 271 272 true; = 273 = true 274 true; 275 (input [0]. value + '").split(); = 276 ") { 277 == 278 false; 279 } if (inputsSplitted.length != 4) { num_of_chars_ok 280 281 282 false; 283 break; } if (isNaN(inputsSplitted[i])) { only_nums break; 284 285 286 false; 287 } } if (all_filled alert('You ha ve to input 4 numbers'); 288 289 290 false) { == 291 292 return false; } if (num_of_chars_ok = false) { alert('Only four number per input field allowed'); return false; 293 294 295 296 } if (only_nums alert('You cannot use letters etc. '); 297 298 false) { == 299 300 301 return false; 302 } 303 304 return true; 305 } S/script> </body> 306 307 308 </html>
228
console.log(bol);
let orangeCount
if (bol
229
230
false) {
==
231
'1 number in wrong place'
orangeCount
}
for (let i
if (evaluation [i]
correctCount++;
}
evaluation [i]
}
232
= 0; i < evaluation.length; i++) {
'green ') {
233
234
==
235
236
MakeDot (evaluation [i]);
237
238
// output guesses and evaluation:
document.getElementById('results').innerHTML +=
239
240
241
guess +
'ånbsp;ånbsp;&nbsp;&nbsp;&nbsp;&nbsp; +
evaluation +
242
243
</br>';
// set focus back to first number input:
document.getElementById('number 1').focus();
console.log(roundCounter);
if (roundCounter
document.getElementById('evalBtn').disabled =
document.getElementById('number 1').disabled
// disable the submit button
alert(
*You have guessed ${roundCounter} times. You have reached the guess
limit,
244
245
246
247
248
guessLimit) {
===
249
true;
250
true;
251
252
253
) ;
document.getElementById('txtcorrectans').innerHTML
254
255
goal;
256
return;
}
/clear the input if all numbers were guessed correctly:
if (correctCount
document.getElementById('number 1').value
document.getElementById('number 2'). value
257
258
= 4) {
259
==
260
261
document.getElementById('number3').value
document.getElementById('number4').value
document.getElementById('number 1').focus ();
}
//forbidden the submit if no correct number was guessed:
if (correctCount
document.getElementById('evalBtn').disabled true;
262
=
263
264
265
266
267
a) {
268
Transcribed Image Text:228 console.log(bol); let orangeCount if (bol 229 230 false) { == 231 '1 number in wrong place' orangeCount } for (let i if (evaluation [i] correctCount++; } evaluation [i] } 232 = 0; i < evaluation.length; i++) { 'green ') { 233 234 == 235 236 MakeDot (evaluation [i]); 237 238 // output guesses and evaluation: document.getElementById('results').innerHTML += 239 240 241 guess + 'ånbsp;ånbsp;&nbsp;&nbsp;&nbsp;&nbsp; + evaluation + 242 243 </br>'; // set focus back to first number input: document.getElementById('number 1').focus(); console.log(roundCounter); if (roundCounter document.getElementById('evalBtn').disabled = document.getElementById('number 1').disabled // disable the submit button alert( *You have guessed ${roundCounter} times. You have reached the guess limit, 244 245 246 247 248 guessLimit) { === 249 true; 250 true; 251 252 253 ) ; document.getElementById('txtcorrectans').innerHTML 254 255 goal; 256 return; } /clear the input if all numbers were guessed correctly: if (correctCount document.getElementById('number 1').value document.getElementById('number 2'). value 257 258 = 4) { 259 == 260 261 document.getElementById('number3').value document.getElementById('number4').value document.getElementById('number 1').focus (); } //forbidden the submit if no correct number was guessed: if (correctCount document.getElementById('evalBtn').disabled true; 262 = 263 264 265 266 267 a) { 268
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Managing System
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education