How do I pass this string in number to calculate in another array?

0

<script type="text/javascript">

var alunos = new Array();

alunos[0] = new Array();
alunos[0] [0] = "Aline dos Santos";
alunos[0] [1] = "6,5";
alunos[0] [2] = "2,9";

alunos[1] = new Array();
alunos[1] [0] = "Bianca da Silva";
alunos[1] [1] = "7,8";
alunos[1] [2] = "8,6";

var medias = new Array();

medias[0]=((alunos[0][1]+2)*alunos[0][2]/3);

alert(medias[0]);

</script>
    
asked by anonymous 06.02.2018 / 00:12

2 answers

2

Use # to convert from text to number, eg:

var alunos = new Array();

alunos[0] = new Array();
alunos[0] [0] = "Aline dos Santos";
alunos[0] [1] = "6,5";
alunos[0] [2] = "2,9";

alunos[1] = new Array();
alunos[1] [0] = "Bianca da Silva";
alunos[1] [1] = "7,8";
alunos[1] [2] = "8,6";

var medias = new Array();

medias[0]=((parseFloat(alunos[0][1])+2)*parseFloat(alunos[0][2])/3);

alert(medias[0]);
    
06.02.2018 / 00:31
0

When you work with arithmetic operations you have to pay attention to certain points. See:

var alunos = new Array();

alunos[0] = new Array();
alunos[0] [0] = "Aline dos Santos";
alunos[0] [1] = "6,5";
alunos[0] [2] = "2,9";

alunos[1] = new Array();
alunos[1] [0] = "Bianca da Silva";
alunos[1] [1] = "7,8";
alunos[1] [2] = "8,6";

var medias = new Array();

medias[0]=((alunos[0][1]+2)*alunos[0][2]/3);

alert(medias[0]);

Your code is dealing with strings, not numbers. So I ask you for your greater understanding to look for more and to know more and more. Small mistakes are for growing up, or something like that!

Code working:

var alunos = new Array();

alunos[0] = new Array();
alunos[0] [0] = "Aline dos Santos";
alunos[0] [1] = 6,5; // Retirei aspas
alunos[0] [2] = 2,9; // Retirei aspas

alunos[1] = new Array();
alunos[1] [0] = "Bianca da Silva";
alunos[1] [1] = 7,8; // Retirei aspas
alunos[1] [2] = 8,6; // Retirei aspas

var medias = new Array();

medias[0]=((alunos[0][1]+2)*alunos[0][2]/3);

alert(medias[0]);

Do not forget to leave the vote (if I helped you)!

    
06.02.2018 / 00:41