【问题标题】:first part of the array dosent save the students score and the alert box says the student name and undefined or NaN数组的第一部分不保存学生分数,警告框显示学生姓名和未定义或 NaN
【发布时间】:2020-01-12 21:03:35
【问题描述】:

代码如下:

	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 还是未定义

【问题讨论】:

    标签: javascript arrays function


    【解决方案1】:

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

    <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>

    【讨论】:

      【解决方案2】:

      当你的循环第一次运行这一行时:

      var score = score + studentScore(studentScores);
      

      变量score还没有定义,所以上面的行结果是NaN

      要修复,只需在循环之前定义 score

      var score = 0;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-14
        相关资源
        最近更新 更多