[英]I cant solve this: to find the minimum number from an array that is input through prompt()
I cant solve this homework that needs to ask the user to enter student marks and output the minimum mark of the student, can someone please help me solve this problem: 我无法解决需要要求用户输入学生成绩并输出学生最低成绩的作业,有人可以帮助我解决此问题:
<script>
function getMarks() {
var marks = prompt('Type the students marks, seperate each student mark with comma, do not write the percentage mark % .').split(',');
return marks;
}
var studentMarks = getMarks();
var arrayLength = studentMarks.length;
var studentNumber = 0;
var msg = '';
var i;
for (i = 0; i < arrayLength; i++) {
studentNumber = (i + 1);
msg += 'student ' + studentNumber + ': ';
msg += studentMarks[i] + '%' + '<br />';
} document.getElementById('marks').innerHTML = msg; document.getElementById('marke').innerHTML = math.min.apply(null, studentMarks) + '%';
</script>
I will do that in the following way: 我将通过以下方式进行操作:
function getMarks() { var marks = prompt('Type the students marks, seperate each student mark with comma, do not write the percentage mark % .'); return marks.split(',').map(n => Number(n)); } var marksArray = getMarks(); var studentMarks = Math.min(...marksArray); var position = marksArray.indexOf(studentMarks); var msg = 'Student ' + Number(position + 1) + ': '; document.getElementById('marks').innerHTML = msg + studentMarks + '%';
<p id="marks"></p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.