'use strict'; var optionsArray = [1, 9, 0], solutionValue, randomValueOne, randomValueTwo; window.addEventListener('load', createNewTask); function generateRandomValue (min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function handleKeyPress(event) { var solutionInputText = document.getElementById('solutionInput'); if (event.key === 'Enter') switch(solutionInputText.value.trim()){ default: /*console.log('keyPress Case \D:'+solutionInputText.value.trim()+'.');*/ checkTask(); break; case !/\D/: /*console.log('keyPress default:'+solutionInputText.value.trim()+'.');*/ solutionInputText.value = ""; break; } } function createNewTask(){ /*loadOptions();*/ var solutionInputText = document.getElementById('solutionInput'); var taskLabel = document.getElementById('taskLabel'); var radioButtons = document.getElementsByName("operation"); var linkValue = optionsArray[2]; solutionInputText.value = ""; randomValueOne = generateRandomValue(optionsArray[0], optionsArray[1]); randomValueTwo = generateRandomValue(optionsArray[0], optionsArray[1]); if (optionsArray[2]=='4') linkValue = generateRandomValue(1,4); /*console.log('linkValue:'+linkValue);*/ switch (linkValue){ /*case 0: solutionValue = addValues(randomValueOne, randomValueTwo); taskLabel.innerHTML = randomValueOne +' + '+ randomValueTwo; console.log('Case 0: '+solutionValue); break;*/ case 1: solutionValue = subValues(randomValueOne, randomValueTwo); taskLabel.innerHTML = randomValueOne +' - '+ randomValueTwo; console.log('Case 1: '+solutionValue); break; case 2: solutionValue = multiValues(randomValueOne, randomValueTwo); taskLabel.innerHTML = randomValueOne +' * '+ randomValueTwo; console.log('Case 2: '+solutionValue); break; case 3: solutionValue = divValues(randomValueOne*randomValueTwo, randomValueTwo); taskLabel.innerHTML = randomValueOne*randomValueTwo +' / '+ randomValueTwo; console.log('Case 3: '+solutionValue); break; default: solutionValue = addValues(randomValueOne, randomValueTwo); taskLabel.innerHTML = randomValueOne +' + '+ randomValueTwo; console.log('Default: '+solutionValue); break; } } function addValues(valueOne, valueTwo){ return valueOne + valueTwo; } function subValues(valueOne, valueTwo){ return valueOne - valueTwo; } function multiValues(valueOne, valueTwo){ return valueOne * valueTwo; } function divValues(valueOne, valueTwo){ return valueOne / valueTwo; } function checkTask(){ var solutionInputText = document.getElementById('solutionInput'); console.log('solution'); if (solutionInputText.value == solutionValue){ console.log('right solution'); solutionInputText.style.backgroundColor= 'rgba(200, 200, 100, 1)'; createNewTask() } else { solutionInputText.style.backgroundColor= 'red'; solutionInputText.value = ""; console.log('wrong solution'); } } /*---------------------------OPTIONS-WINDOW---------------------------*/ var digitsQuantity = 1; function showOptionsWindow(){ var radioButtons = document.getElementsByName("operation"); var optionsWindow= document.getElementById('optionsWindow'); var digitselector = document.querySelector('#dig'); switch(optionsArray[1]){ case 99: optionsArray[1] = 99; digitselector.value = 2; break; case 999: optionsArray[1] = 999; digitselector.value = 3; break; default: optionsArray[1] = 9; digitselector.value = 1; break; } optionsWindow.style.display ='block'; radioButtons[optionsArray[2]].checked = true; } document.addEventListener('DOMContentLoaded', function () { document.querySelector('#dig') .addEventListener('input', digitsSelection); }); function digitsSelection() { var selectedDigitDisplay = document.querySelector('#selectedDigitDisplay'); var digitselector = document.querySelector('#dig'); selectedDigitDisplay.innerHTML = Math.floor(parseInt(digitselector.value)); digitsQuantity = Math.floor(parseInt(digitselector.value)); return false; } function saveOptionButton(){ var radioButtons = document.getElementsByName("operation"); var optionsWindow = document.getElementById('optionsWindow'); switch(digitsQuantity){ default: optionsArray[1] = 9; break; case 2: optionsArray[1] = 99; break; case 3: optionsArray[1] = 999; break; } optionsWindow.style.display ='none'; for (var i = 0; i < radioButtons.length; i++) { if (radioButtons[i].checked) { optionsArray[2] = i; } } createNewTask(); console.log('optionsArray['+optionsArray[0]+', '+optionsArray[1]+', '+optionsArray[2]+']'); } function cancelOptionButton(){ var optionsWindow = document.getElementById('optionsWindow'); optionsWindow.style.display ='none'; }