编程语言
首页 > 编程语言> > codewars练习(javascript)-2021/1/30

codewars练习(javascript)-2021/1/30

作者:互联网

文章目录

codewars-js练习

2021/1/30

github 地址

my 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

标签:console,log,javascript,codewars,checkExam,2021,var,com
来源: https://blog.csdn.net/FemaleHacker/article/details/113419904