简体   繁体   English

如何在javascript中将数组中的值添加到另一个值中?

[英]how can i add a value from an array to a value from an another one in javascript?

So, I have two arrays: grade[] and grade2[] that contains elements I give from windows.prompt . 因此,我有两个数组:grade []和grade2 [],它们包含我从windows.prompt提供的元素。 I want to add a value from grade with a value from grade2 so i can calculate "media". 我想将年级的值与年级2的值相加,以便可以计算“媒体”。 But the code that i wrote just concats those two values, doesn't add them actually. 但是我编写的代码只是合并了这两个值,实际上并没有将它们相加。 I am a beginner and I hope you can help me. 我是一个初学者,希望您能帮助我。 Thank you! 谢谢!

function display_student() 
{ 
 var n=window.prompt("Number of students: "+"");
 var name= new Array(n);
 var grade=new Array(n); 
 var grade2=new Array(n); 
 var y=0; 
 for (y=0; y<n; y++) 
 { 
name[y]=window.prompt("Student name:","");
grade[y]=window.prompt("Grade 1: ","5");
grade2[y]=window.prompt("Grade 2: ","5")

document.write("</br>"+"Student name: "+name[y]+"</br>"+"Grade 1: "+grade[y]+"    </br>"+"Grade 2: "+grade2[y]+"</br>");
var media=(grade[y]+grade2[y])/2;

document.write("Media: "+media+"</br>");
if(media<5)
document.write("Failed");
else
document.write("Promoted");
  } 
}    

添加之前,请使用parseInt函数。

var media=(parseInt(grade[y]) + parseInt(grade2[y]))/2;

Because they are strings and no numbers. 因为它们是字符串,没有数字。
Take a look at parseInt(string, radix) 看看parseInt(string, radix)

The values in the array are of type String ( as returned by the window.prompt function ) you need to convert them to numbers to add them together : 数组中的值是String类型( 由window.prompt函数返回 ),您需要将它们转换为数字以将它们加在一起:

function display_student() {
    var n = window.prompt("Number of students: " + "");
    var name = new Array(n);
    var grade = new Array(n);
    var grade2 = new Array(n);
    var y = 0;
    for (y = 0; y < n; y++) {
        name[y] = window.prompt("Student name:", "");
        grade[y] = window.prompt("Grade 1: ", "5");
        grade2[y] = window.prompt("Grade 2: ", "5"); // added semi-colon here too

        document.write("</br>" + "Student name: " + name[y] + "</br>" + "Grade 1: " + grade[y] + "    </br>" + "Grade 2: " + grade2[y] + "</br>");
        var media = (parseFloat(grade[y]) + parseFloat(grade2[y])) / 2;

        document.write("Media: " + media + "</br>");
        if (media < 5) {
            document.write("Failed");
        } else {
            document.write("Promoted");
        }
    }
}

Working example : http://jsfiddle.net/z5cUw/ 工作示例: http : //jsfiddle.net/z5cUw/

Use: 采用:

var media=(parseInt(grade[y], 10)+parseInt(grade2[y], 10))/2;

Assuming your grades are integers. 假设您的成绩是整数。 Otherwise, use parseFloat (in that case, you won't need the second parameter). 否则,请使用parseFloat (在这种情况下,您将不需要第二个参数)。

使用parseFloat:

var media=(parseFloat(grade[y])+parseFloat(grade2[y]))/2;

This is an indirect answer because this is tagged homework. 这是一个间接答案,因为这被标记为作业。

Because window.prompt returns a string by default, you need to convert this string to a number. 由于window.prompt默认返回一个字符串,因此您需要将此字符串转换为数字。 There are several methods to do this. 有几种方法可以做到这一点。

  • You can use parseInt() as several people have suggested. 您可以按照几个人的建议使用parseInt() Returns an integer value. 返回一个整数值。
  • You can use parseFloat() as suggested. 您可以按照建议使用parseFloat()
  • You can force it using a simple number division on the prompt: window.prompt("Grade 1: ","5")/1; 您可以在提示符下使用简单的数字除法来强制它: window.prompt("Grade 1: ","5")/1; or during the media calculation: var media = (grade[y]/1 + grade2[y]/1) / 2; 或在媒体计算期间: var media = (grade[y]/1 + grade2[y]/1) / 2;
  • You can use the Number() function: Number(window.prompt("Grade 1: ", "5")); 您可以使用Number()函数: Number(window.prompt("Grade 1: ", "5"));

It is up to you to determine which is the appropriate method for your needs. 由您决定哪种方法适合您的需求。 What if the value entered is NOT a number? 如果输入的值不是数字怎么办? Study each of these methods to determine which best suites your needs. 研究每种方法,以确定最适合您的需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将值从一个数组添加到另一个数组 - How to add value from one array to another array 如何使用另一个数组 javascript 中的值从一个数组中检索 - How to retrieve from one array using the value in another array javascript 从一个数组获取值并将其添加到另一个数组的最后一个值 - Take value from one array and add it on to the last value in another array 我可以在不引用 javascript 的情况下从对象数组中的另一个值分配一个对象数组中的项目的值吗? - Can I assign a value, of an item in an array of objects, from another value from an array of objects without referencing in javascript? Javascript-如何将一个数组中的点添加到另一个数组中-所以我可以将它们相加并比较两个数组 - Javascript - how to add points from one array to another - so I can tally them up and compare the two 如何判断JavaScript中一个数组中的值是否等于另一个数组中的值? - How can I tell if a value in one array is equal to a value in another array in Javascript? 如何将一个查询的值添加到另一个查询 - How to add value from one query to another 如何仅将特定键值对从一个 object 添加到另一个? - How can I add only particular key value pairs from one object to another? 如何使用JavaScript将值从一个HTML页面传递到另一个HTML页面? - How can I pass a value from one HTML page to another using JavaScript? 在 Javascript 中的另一个数组中搜索一个数组中的每个值 - Search for every value from one array in another array in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM