首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数组的第一部分不保存学生成绩,并且警告框显示学生姓名和undefined或NaN

数组的第一部分不保存学生成绩,并且警告框显示学生姓名和undefined或NaN
EN

Stack Overflow用户
提问于 2020-01-13 05:03:35
回答 2查看 25关注 0票数 0

代码如下:

代码语言:javascript
复制
	function studentName(x)
	{
		while(x == '')
		{
			x = prompt('Cannot leave field blank. Enter again');
			
		}
		return(x)
	} 

	
	function studentScore(y)
	{
		while(y == '' || y > 100 || y < 0 || isNaN(y))
		{
			y = parseFloat(prompt('Invalid score. Enter score again'));
		}
		return(y)
	}

	
	function another(z,t)
	{
		while(z == '' && z != 'n' && z != 'N' && z != 'y' && z != 'Y')
		{
			
			if(z == 'y' || z == 'Y')
			{
				z = prompt('Invalid Option. Enter another score? Y or N')
			}
			else
			{
				t = prompt('Invalid Option. Enter another student? Y or N')
			}
		}
		return(z)
	}
			
	var names = []	
	var scores = []
	var redo = true
	var anotherName
	var redo2
	var retry = true
	var anotherScore
	var retry2
	var i = 0
	var a = 1
		while(redo == true)
		{
			var studentNames = prompt('Enter student name');
					var name = studentName(studentNames);
					names.push(name)
			while(retry == true)
			{
				var studentScores = parseFloat(prompt('Enter student score'));
				var score = score + studentScore(studentScores);

				retry = prompt('Enter another score? Y/N');
				retry2 = another(retry);
				if(retry == 'y' || retry == 'Y')
				{
					retry = true
					a++
				}
				else if(retry == 'n' || retry == 'N')
				{
					retry = false
				}
			}
			score = score / a
			scores[i] = score
			redo = prompt('Enter another student? Y/N');
			redo2 = another(redo);

			if(redo == 'y' || redo == 'Y')
			{
				redo = true
				retry = true
				i++;
				a = 1
				score = 0
			}
			else if(redo == 'n' || redo == 'N')
			{
				redo = false
			} 
		}
		
		for(y=0; y < names.length; y++)
		{
			alert(names[y] + " - " + scores[y]);
		}

试着运行代码来理解这是一个很难描述的问题,但基本上,我认为发生的问题是数组没有正确存储第一个分数,因为它一直说学生的名字是名字,而分数要么是NaN,要么是未定义的

EN

回答 2

Stack Overflow用户

发布于 2020-01-13 05:13:12

请在循环外初始化score,并从循环中删除var关键字。

代码语言:javascript
复制
<script>
function studentName(x)
{
    while(x == '')
    {
        x = prompt('Cannot leave field blank. Enter again');

    }
    return(x)
} 


function studentScore(y)
{
    while(y == '' || y > 100 || y < 0 || isNaN(y))
    {
        y = parseFloat(prompt('Invalid score. Enter score again'));
    }
    return(y)
}


function another(z,t)
{
    while(z == '' && z != 'n' && z != 'N' && z != 'y' && z != 'Y')
    {

        if(z == 'y' || z == 'Y')
        {
            z = prompt('Invalid Option. Enter another score? Y or N')
        }
        else
        {
            t = prompt('Invalid Option. Enter another student? Y or N')
        }
    }
    return(z)
}

var names = []  
var scores = []
var redo = true
var anotherName
var redo2
var retry = true
var anotherScore
var retry2
var i = 0
var a = 1
    while(redo == true)
    {
        var studentNames = prompt('Enter student name');
                var name = studentName(studentNames);
                names.push(name)
                var score = 0;
        while(retry == true)
        {
            var studentScores = parseFloat(prompt('Enter student score'));
            score = score + studentScore(studentScores);

            retry = prompt('Enter another score? Y/N');
            retry2 = another(retry);
            if(retry == 'y' || retry == 'Y')
            {
                retry = true
                a++
            }
            else if(retry == 'n' || retry == 'N')
            {
                retry = false
            }
        }
        score = score / a
        scores[i] = score
        redo = prompt('Enter another student? Y/N');
        redo2 = another(redo);

        if(redo == 'y' || redo == 'Y')
        {
            redo = true
            retry = true
            i++;
            a = 1
            score = 0
        }
        else if(redo == 'n' || redo == 'N')
        {
            redo = false
        } 
    }

    for(y=0; y < names.length; y++)
    {
        alert(names[y] + " - " + scores[y]);
    }
</script>

票数 0
EN

Stack Overflow用户

发布于 2020-01-13 05:15:53

当您的循环第一次运行这行代码时:

代码语言:javascript
复制
var score = score + studentScore(studentScores);

变量score尚未定义,因此上面的行将生成一个NaN

要解决这个问题,只需在循环之前定义score

代码语言:javascript
复制
var score = 0;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59708107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档