codewars练习(javascript)-2021/1/30
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了codewars练习(javascript)-2021/1/30,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1747字,纯文字阅读大概需要3分钟。
内容图文
![codewars练习(javascript)-2021/1/30](/upload/InfoBanner/zyjiaocheng/608/197b60db4a6e4dc1a8f67bf4cd51ccd0.jpg)
文章目录
codewars-js练习
2021/1/30
github 地址
【1】<8kyu>【Check the exam】
The first input array is the key to the correct answers to an exam, like [“a”, “a”, “b”, “d”]. The second one contains a student’s submitted answers.
The two arrays are not empty and are the same length. Return the score for this array of answers, giving +4 for each correct answer, -1 for each incorrect answer, and +0 for each blank answer, represented as an empty string (in C the space character is used).
If the score < 0, return 0.
example:
checkExam(["a", "a", "b", "b"], ["a", "c", "b", "d"]) → 6
checkExam(["a", "a", "c", "b"], ["a", "a", "b", ""]) → 7
checkExam(["a", "a", "b", "c"], ["a", "a", "b", "c"]) → 16
checkExam(["b", "c", "b", "a"], ["", "a", "a", "c"]) → 0
solution
<script type="text/javascript">
function checkExam(array1, array2) {
// console.log(array1,array2);
var correct = 0;
var error = 0;
var other = 0;
for(var i=0;i<array1.length;i++){
if(array1[i] == array2[i]){
correct ++;
}else if(array2[i] == ''){
other = 0;
}else{
error ++;
}
}
// console.log(correct,error);
var result = correct * 4 + error * (-1) + other;
if(result <0){
return 0;
}
return result;
}
验证
console.log(checkExam(["a", "a", "b", "b"], ["a", "c", "b", "d"]));// 6
console.log(checkExam(["a", "a", "c", "b"], ["a", "a", "b", ""]));//7
console.log(checkExam(["b", "c", "b", "a"], ["", "a", "a", "c"]));//0
</script>
【2】<7kyu>【Love vs friendship】
If a = 1, b = 2, c = 3 ... z = 26
Then l + o + v + e = 54
and f + r + i + e + n + d + s + h + i + p = 108
So friendship
is twice stronger than love
内容总结
以上是互联网集市为您收集整理的codewars练习(javascript)-2021/1/30全部内容,希望文章能够帮你解决codewars练习(javascript)-2021/1/30所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。