Skip to content

Ex13 js context #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
64 changes: 64 additions & 0 deletions src/ex13_js-context/task1.js
Original file line number Diff line number Diff line change
@@ -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;
82 changes: 82 additions & 0 deletions src/ex13_js-context/task2.js
Original file line number Diff line number Diff line change
@@ -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;