Reputation: 317
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?
Upvotes: 0
Views: 93
Reputation: 1562
Just looking over your code, I think the problem is the for loop in the somavlrs function, try this instead.
function somavlrs (n){
soma = 0
for(i = 1; i <=n; i++){
soma += P[i]
}
return soma
}
Regarding #2, I think you could move the initial javascript logic into a function then call that via the onload attribute on the body tag. This would prevent that logic from executing until the html document has loaded.
Also, "p" needs to be set to something other than 0 in the do while loop or you'll have an infinite loop.
function onLoad() {
var colortxt = prompt("What is the color?", "")
var p = 0 //remove later
P = new Array(50)
do {
wrtmenu(colortxt)
opc = prompt("What option would you like to choose?", "");
switch (opc.trim().toLowerCase()) {
case "1":
case "um":
P = itrdados(7)
break;
case "2":
case "dois":
somavlrs(7)
dw("The sum is" + soma, colortxt)
break;
}
//p needs to be set somewhere or you'll have an infinite loop
p = 1;
}
while (p == 0)
}
function dw(wrtext, colortxt) {
document.write("<font color=" + colortxt + ">")
document.write(wrtext)
document.write("</font>")
}
function wrtmenu(colortxt) {
dw("------------------------------------------------", colortxt)
dw("<br />Precipitation data statistics", colortxt)
dw("<br />------------------------------------------------", colortxt)
dw("<br />1- Enter data (last 7 days)", colortxt)
dw("<br />2- Calculate the sum of the values", colortxt)
dw("<br />3- Calculate the lowest value", colortxt)
dw("<br />4- Calculate the mean values", colortxt)
dw("<br />5- End up", colortxt)
}
function itrdados(n) {
introdados = new Array(50)
for (i = 1; i <= n; i++) {
introdados[i] = prompt("What is the die " + i + " of precipitation", "")
}
return introdados
}
function somavlrs(n) {
soma = 0
for (i = 1; i <= n; i++) {
soma += P[i]
}
return soma
}
<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 onload="onLoad()">
</body>
Upvotes: 1
Reputation: 366
About the second question, current browsers won't display the page until it has finished loading, including any JavaScript running. This means that until the script is out of the do while, the menu and other text you write won't be shown.
Upvotes: 0