Skip to content

Instantly share code, notes, and snippets.

@tonatos
Last active July 22, 2021 11:11
Show Gist options
  • Save tonatos/6d31bc93c17add8c1e17b4261de16d2d to your computer and use it in GitHub Desktop.
Save tonatos/6d31bc93c17add8c1e17b4261de16d2d to your computer and use it in GitHub Desktop.
Два куска кода, которые требуют рефакторинга
// code block #1
function func(s, a, b) {
if (s.match(/^$/)) {
return -1;
}
var i = s.length -1;
var aIndex = -1;
var bIndex = -1;
while ((aIndex == -1) && (bIndex == -1) && (i > 0)) {
if (s.substring(i, i +1) == a) {
aIndex = i;
}
if (s.substring(i, i +1) == b) {
bIndex = i;
}
i = i - 1;
}
if (aIndex != -1) {
if (bIndex == -1) {
return aIndex;
}
else {
return Math.max(aIndex, bIndex);
}
}
if (bIndex != -1) {
return bIndex;
}
else {
return -1;
}
}
// code block #2
function drawRating(vote) {
if (vote >= 0 && vote <= 20) {
return '★☆☆☆☆';
}
else if (vote > 20 && vote <= 40) {
return '★★☆☆☆';
}
else if (vote > 40 && vote <= 60) {
return '★★★☆☆';
}
else if (vote > 60 && vote <= 80) {
return '★★★★☆';
}
else if (vote > 80 && vote <= 100) {
return '★★★★★';
}
}
// Проверка работы результата
console.log(drawRating(0) ); // ★☆☆☆☆
console.log(drawRating(1) ); // ★☆☆☆☆
console.log(drawRating(50)); // ★★★☆☆
console.log(drawRating(99)); // ★★★★★
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment