diff --git a/tasks/math-funcs/funcs/cesar.js b/tasks/math-funcs/funcs/cesar.js new file mode 100644 index 0000000..16e43d5 --- /dev/null +++ b/tasks/math-funcs/funcs/cesar.js @@ -0,0 +1,40 @@ +function cesar(str, shift, action) { + const alphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'; + const alphabetUpper = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'; + let result = ''; + + for (let i = 0; i < str.length; i++) { + let char = str[i]; + let isUpper = false; + let index = -1; + + if (alphabetUpper.indexOf(char) !== -1) { + isUpper = true; + index = alphabetUpper.indexOf(char); + } else if (alphabet.indexOf(char) !== -1) { + index = alphabet.indexOf(char); + } + + if (index !== -1) { + let newIndex; + if (action === 'encode') { + newIndex = (index + shift) % alphabet.length; + } else { + newIndex = (index - shift + alphabet.length) % alphabet.length; + } + + if (isUpper) { + result += alphabetUpper[newIndex]; + } else { + result += alphabet[newIndex]; + } + } else { + result += char; + } + } + + return result; +} + +// Расшифровка сообщения "эзтыхз фзъзъз" с различными сдвигами: +// При shift = 8: "хакуна матата" \ No newline at end of file diff --git a/tasks/math-funcs/funcs/fibb.js b/tasks/math-funcs/funcs/fibb.js new file mode 100644 index 0000000..f9b1778 --- /dev/null +++ b/tasks/math-funcs/funcs/fibb.js @@ -0,0 +1,15 @@ +function fibb(n) { + if (n === 0) return 0; + if (n === 1) return 1; + + let prev = 0; + let current = 1; + + for (let i = 2; i <= n; i++) { + let next = prev + current; + prev = current; + current = next; + } + + return current; +} \ No newline at end of file diff --git a/tasks/math-funcs/funcs/gcd.js b/tasks/math-funcs/funcs/gcd.js new file mode 100644 index 0000000..8ceffb0 --- /dev/null +++ b/tasks/math-funcs/funcs/gcd.js @@ -0,0 +1,8 @@ +function gcd(a, b) { + while (b !== 0) { + let temp = b; + b = a % b; + a = temp; + } + return a; +} \ No newline at end of file diff --git a/tasks/math-funcs/funcs/getSortedArray.js b/tasks/math-funcs/funcs/getSortedArray.js new file mode 100644 index 0000000..42216db --- /dev/null +++ b/tasks/math-funcs/funcs/getSortedArray.js @@ -0,0 +1,12 @@ +function getSortedArray(array, key) { + for (let i = 0; i < array.length; i++) { + for (let j = 0; j < array.length - 1 - i; j++) { + if (array[j][key] > array[j + 1][key]) { + let temp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = temp; + } + } + } + return array; +} \ No newline at end of file diff --git a/tasks/math-funcs/funcs/minDigit.js b/tasks/math-funcs/funcs/minDigit.js new file mode 100644 index 0000000..1b6c6ce --- /dev/null +++ b/tasks/math-funcs/funcs/minDigit.js @@ -0,0 +1,11 @@ +function minDigit(x) { + let min = 9; + while (x > 0) { + let digit = x % 10; + if (digit < min) { + min = digit; + } + x = Math.floor(x / 10); + } + return min; +} \ No newline at end of file diff --git a/tasks/math-funcs/funcs/pluralizeRecords.js b/tasks/math-funcs/funcs/pluralizeRecords.js new file mode 100644 index 0000000..9e95830 --- /dev/null +++ b/tasks/math-funcs/funcs/pluralizeRecords.js @@ -0,0 +1,18 @@ +function pluralizeRecords(n) { + let recordForm = "записей"; + let wasForm = "было найдено"; + + let lastDigit = n % 10; + let lastTwoDigits = n % 100; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 14) { + //pass + } else if (lastDigit === 1) { + recordForm = "запись"; + wasForm = "была найдена"; + } else if (lastDigit >= 2 && lastDigit <= 4) { + recordForm = "записи"; + } + + return `В результате выполнения запроса ${wasForm} ${n} ${recordForm}`; +} \ No newline at end of file diff --git a/tasks/math-funcs/funcs/pow.js b/tasks/math-funcs/funcs/pow.js new file mode 100644 index 0000000..2a436a6 --- /dev/null +++ b/tasks/math-funcs/funcs/pow.js @@ -0,0 +1,7 @@ +function pow(x, n) { + let result = 1; + for (let i = 0; i < n; i++) { + result *= x; + } + return result; +} \ No newline at end of file diff --git a/tasks/math-funcs/index.html b/tasks/math-funcs/index.html new file mode 100644 index 0000000..65b0d41 --- /dev/null +++ b/tasks/math-funcs/index.html @@ -0,0 +1,234 @@ + + + + + + + Тестирование функций JavaScript + + + +
+

Тестирование функций JavaScript

+ + +
+

1. Возведение в степень | Exponentiation

+

+ Функция pow(x,n) возвращает x в степени n. Иначе говоря, умножает x на себя n раз и возвращает результат. Функция обязана поддерживать только натуральные значения n. +

+
function pow(x, n) {
+  let result = 1;
+  for (let i = 0; i < n; i++) {
+    result *= x;
+  }
+  return result;
+}
+
+ + + +
+
+
+ + +
+

2. Нахождение НОД | Greatest common divisor (GCD)

+

+ Функция gcd(a,b) возвращает x - наибольший общий делитель двух неотрицательных чисел a и b. +

+
function gcd(a, b) {
+  while (b !== 0) {
+    let temp = b;
+    b = a % b;
+    a = temp;
+  }
+  return a;
+}
+
+ + + +
+
+
+ + +
+

3. Наименьшая цифра | Minimum digit

+

+ Функция minDigit(x) возвращает наименьшую цифру целого неотрицательного числа x. +

+
function minDigit(x) {
+  let min = 9;
+  while (x > 0) {
+    let digit = x % 10;
+    if (digit < min) {
+      min = digit;
+    }
+    x = Math.floor(x / 10);
+  }
+  return min;
+}
+
+ + +
+
+
+ + +
+

4. Pluralization

+

+ Функция pluralizeRecords(n) для любого целого неотрицательного значения n вернёт строку "В результате выполнения запроса было найдено n записей", в которой для каждого слова будет образована правильная форма множественного числа, в зависимости от конкретного значения n. +

+
function pluralizeRecords(n) {
+  let recordForm = "записей";
+  let wasForm = "было найдено";
+  
+  let lastDigit = n % 10;
+  let lastTwoDigits = n % 100;
+  
+  if (lastTwoDigits >= 11 && lastTwoDigits <= 14) {
+    //pass
+  } else if (lastDigit === 1) {
+    recordForm = "запись";
+    wasForm = "была найдена";
+  } else if (lastDigit >= 2 && lastDigit <= 4) {
+    recordForm = "записи";
+  }
+  
+  return `В результате выполнения запроса ${wasForm} ${n} ${recordForm}`;
+}
+
+ + +
+
+
+ + +
+

5. Числа Фибоначчи | Fibonacci numbers

+

+ Функция fibb(n) для любого целого неотрицательного числа n <= 1000 вернёт n-ое число из последовательности Фибоначчи. +

+
function fibb(n) {
+  if (n === 0) return 0;
+  if (n === 1) return 1;
+  
+  let prev = 0;
+  let current = 1;
+  
+  for (let i = 2; i <= n; i++) {
+    let next = prev + current;
+    prev = current;
+    current = next;
+  }
+  
+  return current;
+}
+
+ + +
+
+
+ + +
+

6. Сортировка объектов | Sorting objects

+

+ Функция getSortedArray(array, key) сортирует массив объектов. У функции два параметра: array - массив объектов, оторый нужно отсортировать, и key - ключ, по значению которого нужно произвести сортировку. Порядок сортировки - по возрастанию. +

+
function getSortedArray(array, key) {
+  for (let i = 0; i < array.length; i++) {
+    for (let j = 0; j < array.length - 1 - i; j++) {
+      if (array[j][key] > array[j + 1][key]) {
+        let temp = array[j];
+        array[j] = array[j + 1];
+        array[j + 1] = temp;
+      }
+    }
+  }
+  return array;
+}
+
+ + + +
+
+
+ + +
+

7. Шифр Цезаря | Caesar's cipher

+

+ Функция cesar(str, shift, action) производит шифрование и дешифровку строки str с использованием шифра Цезаря. В качестве алфавита используется русский алфавит. Параметр shift отвечает за сдвиг алфавита. Если action == 'encode', функция производит шифрование, а если action == 'decode' - дешифровку. +

+
function cesar(str, shift, action) {
+  const alphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя';
+  const alphabetUpper = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ';
+  let result = '';
+  
+  for (let i = 0; i < str.length; i++) {
+    let char = str[i];
+    let isUpper = false;
+    let index = -1;
+    
+    if (alphabetUpper.indexOf(char) !== -1) {
+      isUpper = true;
+      index = alphabetUpper.indexOf(char);
+    } else if (alphabet.indexOf(char) !== -1) {
+      index = alphabet.indexOf(char);
+    }
+    
+    if (index !== -1) {
+      let newIndex;
+      if (action === 'encode') {
+        newIndex = (index + shift) % alphabet.length;
+      } else {
+        newIndex = (index - shift + alphabet.length) % alphabet.length;
+      }
+      
+      if (isUpper) {
+        result += alphabetUpper[newIndex];
+      } else {
+        result += alphabet[newIndex];
+      }
+    } else {
+      result += char;
+    }
+  }
+  
+  return result;
+}
+
+// Расшифровка: "эзтыхз фзъзъз" со сдвигом 8 = "хакуна матата"
+
+ + + + +
+
+
+
+ + + + + + + + + + + + \ No newline at end of file diff --git a/tasks/math-funcs/script.js b/tasks/math-funcs/script.js new file mode 100644 index 0000000..84f2b1c --- /dev/null +++ b/tasks/math-funcs/script.js @@ -0,0 +1,51 @@ +function testPow() { + let x = parseFloat(document.getElementById('pow-x').value); + let n = parseInt(document.getElementById('pow-n').value); + let result = pow(x, n); + document.getElementById('pow-result').textContent = 'Результат: ' + result; +} + +function testGcd() { + let a = parseInt(document.getElementById('gcd-a').value); + let b = parseInt(document.getElementById('gcd-b').value); + let result = gcd(a, b); + document.getElementById('gcd-result').textContent = 'Результат: ' + result; +} + +function testMinDigit() { + let x = parseInt(document.getElementById('minDigit-x').value); + let result = minDigit(x); + document.getElementById('minDigit-result').textContent = 'Результат: ' + result; +} + +function testPluralize() { + let n = parseInt(document.getElementById('pluralize-n').value); + let result = pluralizeRecords(n); + document.getElementById('pluralize-result').textContent = result; +} + +function testFibb() { + let n = parseInt(document.getElementById('fibb-n').value); + let result = fibb(n); + document.getElementById('fibb-result').textContent = 'Результат: ' + result; +} + +function testSort() { + try { + let arrayStr = document.getElementById('sort-array').value; + let key = document.getElementById('sort-key').value; + let array = JSON.parse(arrayStr); + let result = getSortedArray(array, key); + document.getElementById('sort-result').textContent = 'Результат: ' + JSON.stringify(result, null, 2); + } catch (e) { + document.getElementById('sort-result').textContent = 'Ошибка: ' + e.message; + } +} + +function testCesar() { + let str = document.getElementById('cesar-str').value; + let shift = parseInt(document.getElementById('cesar-shift').value); + let action = document.getElementById('cesar-action').value; + let result = cesar(str, shift, action); + document.getElementById('cesar-result').textContent = 'Результат: ' + result; +} \ No newline at end of file diff --git a/tasks/math-funcs/style.css b/tasks/math-funcs/style.css new file mode 100644 index 0000000..3ea9e80 --- /dev/null +++ b/tasks/math-funcs/style.css @@ -0,0 +1,101 @@ +body { + font-family: Arial, sans-serif; + background-color: #f5f5f5; + margin: 0; + padding: 20px; +} + +.container { + max-width: 900px; + margin: 0 auto; + background: white; + padding: 30px; + border-radius: 8px; + box-shadow: 0 2px 8px rgba(0,0,0,0.1); +} + +h1 { + text-align: center; + color: #333; + margin-bottom: 40px; +} + +.function-block { + margin-bottom: 40px; + padding-bottom: 30px; + border-bottom: 2px solid #e0e0e0; +} + +.function-block:last-child { + border-bottom: none; +} + +h2 { + color: #333; + margin-bottom: 15px; +} + +.description { + color: #666; + line-height: 1.6; + margin-bottom: 15px; +} + +.code { + background-color: #f8f8f8; + border: 1px solid #ddd; + border-radius: 4px; + padding: 15px; + overflow-x: auto; + font-family: 'Courier New', monospace; + font-size: 14px; + line-height: 1.5; + margin-bottom: 20px; +} + +.test-section { + display: flex; + flex-wrap: wrap; + gap: 10px; + align-items: center; +} + +input, select, textarea { + padding: 10px; + border: 1px solid #ddd; + border-radius: 4px; + font-size: 14px; + flex: 1; + min-width: 150px; +} + +textarea { + width: 100%; + font-family: 'Courier New', monospace; + resize: vertical; +} + +button { + background-color: #4CAF50; + color: white; + padding: 10px 25px; + border: none; + border-radius: 4px; + cursor: pointer; + font-size: 14px; +} + +button:hover { + background-color: #45a049; +} + +.result { + width: 100%; + margin-top: 10px; + padding: 15px; + background-color: #f0f0f0; + border-radius: 4px; + min-height: 20px; + font-weight: bold; + color: #333; +} \ No newline at end of file diff --git a/tasks/math-game/index.html b/tasks/math-game/index.html new file mode 100644 index 0000000..df3894c --- /dev/null +++ b/tasks/math-game/index.html @@ -0,0 +1,36 @@ + + + + + + + Math Game + + + +
+

Math Game

+
+ Уровень: Начальный + Время: 5:00 +
+
+ Правильно: 0 + Неправильно: 0 + Осталось: 10 +
+
+

+ + +
+
+ +
+ + + \ No newline at end of file diff --git a/tasks/math-game/script.js b/tasks/math-game/script.js new file mode 100644 index 0000000..085aa8b --- /dev/null +++ b/tasks/math-game/script.js @@ -0,0 +1,195 @@ +let currentLevel = 1; +let correctAnswers = 0; +let wrongAnswers = 0; +let questionsOnLevel = 0; +let currentQuestion = null; +let usedQuestions = []; +let timerInterval = null; +let timeLeft = 300; + +const levelNames = { + 1: 'Начальный', + 2: 'Средний', + 3: 'Продвинутый' +}; + +function startTimer() { + if (timerInterval) { + clearInterval(timerInterval); + } + + timeLeft = 300; + updateTimerDisplay(); + + timerInterval = setInterval(function() { + timeLeft--; + updateTimerDisplay(); + + if (timeLeft <= 0) { + clearInterval(timerInterval); + showResult('Время вышло! Вы не успели завершить уровень.'); + } + }, 1000); +} + +function updateTimerDisplay() { + let minutes = Math.floor(timeLeft / 60); + let seconds = timeLeft % 60; + let display = minutes + ':' + (seconds < 10 ? '0' : '') + seconds; + document.getElementById('timer').textContent = display; +} + +function generateQuestion() { + let question, answer; + let questionText; + + do { + if (currentLevel === 1) { + let num1 = Math.floor(Math.random() * 20) + 1; + let num2 = Math.floor(Math.random() * 20) + 1; + let operators = ['+', '-', '*']; + let operator = operators[Math.floor(Math.random() * operators.length)]; + + questionText = num1 + ' ' + operator + ' ' + num2; + answer = eval(questionText); + } else if (currentLevel === 2) { + let num1 = Math.floor(Math.random() * 20) + 1; + let num2 = Math.floor(Math.random() * 20) + 1; + let operators = ['>', '<', '==']; + let operator = operators[Math.floor(Math.random() * operators.length)]; + + questionText = num1 + ' ' + operator + ' ' + num2; + answer = eval(questionText) ? 'true' : 'false'; + } else { + let types = ['logical', 'binary']; + let type = types[Math.floor(Math.random() * types.length)]; + + if (type === 'logical') { + let bool1 = Math.random() > 0.5; + let bool2 = Math.random() > 0.5; + let operators = ['&&', '||']; + let operator = operators[Math.floor(Math.random() * operators.length)]; + + questionText = bool1 + ' ' + operator + ' ' + bool2; + answer = eval(questionText) ? 'true' : 'false'; + } else { + let num = Math.floor(Math.random() * 16); + questionText = 'Двоичное представление числа ' + num; + answer = num.toString(2); + } + } + } while (usedQuestions.includes(questionText)); + + usedQuestions.push(questionText); + currentQuestion = { text: questionText, answer: String(answer) }; + + document.getElementById('question').textContent = questionText; + document.getElementById('answer').value = ''; + document.getElementById('feedback').textContent = ''; + document.getElementById('answer').focus(); +} + +function checkAnswer() { + let userAnswer = document.getElementById('answer').value.trim(); + + if (userAnswer === '') { + alert('Пожалуйста, введите ответ'); + return; + } + + if (userAnswer === currentQuestion.answer) { + correctAnswers++; + document.getElementById('feedback').textContent = 'Правильно!'; + document.getElementById('feedback').style.color = '#4CAF50'; + } else { + wrongAnswers++; + document.getElementById('feedback').textContent = 'Неправильно! Правильный ответ: ' + currentQuestion.answer; + document.getElementById('feedback').style.color = '#f44336'; + } + + questionsOnLevel++; + updateDisplay(); + + if (questionsOnLevel >= 10) { + clearInterval(timerInterval); + checkLevelProgress(); + } else { + setTimeout(generateQuestion, 1500); + } +} + +function updateDisplay() { + document.getElementById('correct').textContent = correctAnswers; + document.getElementById('wrong').textContent = wrongAnswers; + document.getElementById('remaining').textContent = 10 - questionsOnLevel; + document.getElementById('level').textContent = levelNames[currentLevel]; +} + +function checkLevelProgress() { + let percentage = (correctAnswers / (correctAnswers + wrongAnswers)) * 100; + + if (currentLevel < 3 && percentage >= 80) { + setTimeout(() => { + alert('Поздравляем! Вы переходите на следующий уровень!'); + currentLevel++; + questionsOnLevel = 0; + correctAnswers = 0; + wrongAnswers = 0; + usedQuestions = []; + updateDisplay(); + startTimer(); + generateQuestion(); + }, 1500); + } else if (currentLevel === 3 && percentage >= 80) { + setTimeout(() => { + showResult('Поздравляем! Вы успешно завершили все уровни игры!'); + }, 1500); + } else { + setTimeout(() => { + showResult('К сожалению, вы не смогли перейти на следующий уровень. Попробуйте еще раз!'); + }, 1500); + } +} + +function showResult(message) { + clearInterval(timerInterval); + document.getElementById('question-container').style.display = 'none'; + document.getElementById('feedback').style.display = 'none'; + document.getElementById('result-container').style.display = 'block'; + document.getElementById('result-message').textContent = message; +} + +function restartGame() { + currentLevel = 1; + correctAnswers = 0; + wrongAnswers = 0; + questionsOnLevel = 0; + usedQuestions = []; + + document.getElementById('question-container').style.display = 'block'; + document.getElementById('feedback').style.display = 'block'; + document.getElementById('result-container').style.display = 'none'; + + updateDisplay(); + startTimer(); + generateQuestion(); +} + +function exitGame() { + if (confirm('Вы уверены, что хотите выйти из игры?')) { + clearInterval(timerInterval); + window.close(); + } +} + +document.getElementById('answer').addEventListener('keypress', function(event) { + if (event.key === 'Enter') { + checkAnswer(); + } +}); + +window.onload = function() { + updateDisplay(); + startTimer(); + generateQuestion(); +}; \ No newline at end of file diff --git a/tasks/math-game/style.css b/tasks/math-game/style.css new file mode 100644 index 0000000..9a29cf2 --- /dev/null +++ b/tasks/math-game/style.css @@ -0,0 +1,103 @@ +body { + font-family: Arial, sans-serif; + display: flex; + justify-content: center; + align-items: center; + min-height: 100vh; + margin: 0; + background-color: #f5f5f5; +} + +.container { + background: white; + padding: 40px; + border-radius: 8px; + box-shadow: 0 2px 8px rgba(0,0,0,0.1); + max-width: 400px; + width: 100%; +} + +h1 { + text-align: center; + margin: 0 0 30px 0; + color: #333; +} + +.game-info { + display: flex; + justify-content: space-between; + margin-bottom: 15px; + padding-bottom: 15px; + border-bottom: 1px solid #e0e0e0; + color: #666; +} + +.game-stats { + display: flex; + justify-content: space-between; + margin-bottom: 30px; + color: #666; +} + +.game-info span, +.game-stats span { + font-size: 14px; +} + +#question-container { + text-align: center; + margin-bottom: 20px; +} + +#question { + font-size: 32px; + margin-bottom: 25px; + color: #333; +} + +#answer { + width: 100%; + padding: 12px; + font-size: 16px; + border: 1px solid #ddd; + border-radius: 4px; + box-sizing: border-box; + margin-bottom: 15px; +} + +#answer:focus { + outline: none; + border-color: #4CAF50; +} + +button { + background-color: #4CAF50; + color: white; + padding: 12px 30px; + border: none; + border-radius: 4px; + cursor: pointer; + font-size: 16px; + margin: 5px; +} + +button:hover { + background-color: #45a049; +} + +#feedback { + text-align: center; + font-size: 16px; + font-weight: bold; + min-height: 25px; + margin-top: 15px; +} + +#result-container { + text-align: center; +} + +#result-container h2 { + margin-bottom: 25px; + color: #333; +} \ No newline at end of file