I'm making a javascript algorithm for school and I have two problems!!
First here's the html with the script:
<!Doctype html>
<html>
<head>
<title>Estatística de dados de precipitação</title>
<style>
body {
font-family: calibri;
font-size: 15pt;
}
</style>
<meta charset="utf-8">
<script type="text/javascript" src="ficha6-11.js"></script>
</head>
<body>
<script>
var colortxt = prompt("What is the color?","")
var p = 0 //retirar depois
P = new Array(50)
do{
wrtmenu ()
opc = prompt("What option would you like to choose?","");
switch (opc){
case "1":
case " 1":
case "1 ":
case " 1 ":
case "1 ":
case "um":
case "Um":
case "uM":
case "UM":
P = itrdados (7)
break;
case "2":
case " 2":
case "2 ":
case " 2 ":
case "2 ":
case "dois":
case "Dois":
case "dOIS":
case "DOIS":
somavlrs (7)
dw("A soma é" + soma, colortxt)
break;
}
}
while (p==0)
</script>
</body>
And here's the javascript functions:
function dw (wrtext,colortxt){
document.write("<font color="+colortxt+">")
document.write(wrtext)
document.write("</font>")
}
function wrtmenu (){
dw("------------------------------------------------",colortxt)
dw("<br />Estatística de dados de precipitação",colortxt)
dw("<br />------------------------------------------------",colortxt)
dw("<br />1- Introduzir dados (últimos 7 dias)",colortxt)
dw("<br />2- Calcular a soma dos valores",colortxt)
dw("<br />3- Calcular o menor valor",colortxt)
dw("<br />4- Calcular a média dos valores",colortxt)
dw("<br />5- Terminar",colortxt)
}
function itrdados (n) {
introdados = new Array(50)
for (i = 1; i <= n; i++){
introdados[i] = prompt("Qual é o dado " + i + " de precipitação","")
}
return introdados
}
function somavlrs (n){
soma = 0
for(i = 1; 1 <=n; i++){
soma += P[i]
}
return soma
}
As you can probably see, there's a menu (in my language, portuguese), and if i choose 1, it'll ask me for the numbers, which works fine, but when i choose 2, which would add all the numbers, it doesn't. I saved the number on arrays and i don't know why that doesn't work.
And the second question is, is there a way that the text i wrote on a function appears before the prompt?
switch(opc.trim().toLowerCase())then you won't have to put so manycasestatements. What do you mean by "doesn't work"? What actually happens?1 <=nshould bei <= nin thesomavlrsfunction.