Last active
July 22, 2021 11:11
-
-
Save tonatos/6d31bc93c17add8c1e17b4261de16d2d to your computer and use it in GitHub Desktop.
Два куска кода, которые требуют рефакторинга
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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