diff --git a/.eslintrc.js b/.eslintrc.js index 9db082a8c7..9cbc77e6d5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -54,7 +54,8 @@ module.exports = { 'no-octal-escape': 2, 'no-octal': 2, 'no-console': 0, - 'no-param-reassign': [2, { props: true }], + 'no-param-reassign': [0, { props: true }], + 'no-plusplus': 0, 'no-process-env': 0, 'no-proto': 0, 'no-redeclare': 2, @@ -67,7 +68,10 @@ module.exports = { 'no-useless-call': 0, 'no-useless-concat': 0, 'no-void': 0, - 'no-warning-comments': [0, { terms: ['todo', 'fixme', 'xxx'], location: 'start' }], + 'no-warning-comments': [ + 0, + { terms: ['todo', 'fixme', 'xxx'], location: 'start' }, + ], 'no-with': 2, radix: 2, 'vars-on-top': 0, diff --git a/src/ex13_js-context/task1.js b/src/ex13_js-context/task1.js new file mode 100644 index 0000000000..2af014551f --- /dev/null +++ b/src/ex13_js-context/task1.js @@ -0,0 +1,64 @@ +function CreateCalculator() { + let result = 0; + return { + // +++++add+++++ + add(value) { + if (isNaN(value)) { + return this; + } + result += value; + return this; + }, + // -----subtract---— + subtract(value) { + if (isNaN(value)) { + return this; + } + result -= value; + return this; + }, + //* ****multiply****** + multiply(value) { + if (isNaN(value)) { + return this; + } + result *= value; + return this; + }, + // :::::divide::::: + divide(value) { + if (isNaN(value) || value === 0 || value === Infinity) { + return this; + } + result /= value; + return this; + }, + // .....getResult..... + getResult() { + return result; + }, + // .....setState..... + setState(value) { + if (value) { + result = value; + } + return this; + }, + // .....fetchData..... + fetchData(callback) { + setTimeout(() => { + callback(500); + result = 500; + }, 2000); + return this; + }, + // .....reset..... + reset() { + result = 0; + return this; + }, + }; +} + +const Calculator = CreateCalculator(); +module.exports = Calculator; diff --git a/src/ex13_js-context/task2.js b/src/ex13_js-context/task2.js new file mode 100644 index 0000000000..2e53ebad44 --- /dev/null +++ b/src/ex13_js-context/task2.js @@ -0,0 +1,82 @@ +function Hangman(word) { + // new variables + let resultWord = word.toLowerCase().split(''); + let wrongSymbols = []; + let guessString = []; + let maxErrors = 6; + let lengthResult = resultWord.length; + for (let i = 0; i < lengthResult; i++) { + guessString[i] = '_'; + } + // return constructor object + return { + // guess + guess(symbol) { + if (maxErrors > 1) { + if (resultWord.some((i) => i === symbol.toLowerCase())) { + for (let i = 0; i < lengthResult; i++) { + if (resultWord[i] === symbol.toLowerCase()) { + guessString[i] = symbol.toLowerCase(); + } + } + if (resultWord.join('') === guessString.join('')) { + console.log(`${this.getGuessedString()} | You won!`); + this.startAgain('webpurple'); + return this; + } + console.log(`${this.getGuessedString()}`); + } else { + for (let i = 0; i < lengthResult; i++) { + if (symbol.toLowerCase() === wrongSymbols[i]) { + console.log('You input this letter!'); + return this; + } + } + wrongSymbols.push(symbol.toLowerCase()); + maxErrors--; + console.log( + `Wrong letter, errors left ${maxErrors} | ${this.getWrongSymbols().join( + ',', + )}`, + ); + return this; + } + } else { + console.log('You lose :( Try again! -> .startAgain("webpurple")'); + return this; + } + return this; + }, + // getGuessedString + getGuessedString() { + return guessString.join(''); + }, + // getErrorsLeftgetErrorsLeft + getErrorsLeft() { + return maxErrors; + }, + // getWrongSymbols + getWrongSymbols() { + return wrongSymbols; + }, + // getStatus + getStatus() { + return `${this.getGuessedString()} | errors left ${this.getErrorsLeft()}`; + }, + // startAgain + startAgain(newWord) { + resultWord = newWord.toLowerCase().split(''); + maxErrors = 6; + lengthResult = resultWord.length; + wrongSymbols = []; + guessString = []; + for (let i = 0; i < lengthResult; i++) { + guessString[i] = '_'; + } + return this; + }, + }; +} + +const hangman = new Hangman('webpurple'); +module.exports = hangman;