codewars-js练习
2021/2/24
github 地址
【1】<8kyu>【Grasshopper - Grade book】
完成该函数,以便它找到传递给它的三个分数的平均值,并返回与该成绩相关的字母值。
example:
Numerical Score Letter Grade
90 <= score <= 100 'A'
80 <= score < 90 'B'
70 <= score < 80 'C'
60 <= score < 70 'D'
0 <= score < 60 'F'
solution
<script type="text/javascript">
function getGrade (s1, s2, s3) {
console.log(s1,s2,s3)
var score = (s1 + s2 + s3)/3
if(score<60 && score >=0)return 'F';
else if(score<70 && score >=60 )return 'D'
else if(score<80 && score >=70 )return 'C'
else if(score<90 && score >=80 )return 'B'
else return 'A'
}
// 验证
console.log(getGrade(95,90,93));// 'A'
console.log(getGrade(44,55,52));// 'F'
</script>
【2】<8kyu>【Stringy Strings】
write me a function stringy
that takes a size
and returns a string
of alternating '1s'
and '0s'
.
example:
the string should start with a 1.
a string with size 6 should return :'101010'.
with size 4 should return : '1010'.
with size 12 should return : '101010101010'.
The size will always be positive and will only use whole numbers.
solution
<script type="text/javascript">
function stringy(size) {
var str='';
for( var i=1; i<=size; i++ )
str+=i%2;
return str;
}
// 验证
console.log(stringy(1))//1
console.log(stringy(6));// '101010'
console.log(stringy(4));// '1010'
</script>
以上为自己思路供大家参考,可能有更优的思路。