0

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
}
  1. 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.

  2. And the second question is, is there a way that the text i wrote on a function appears before the prompt?

2
  • 2
    If you do switch(opc.trim().toLowerCase()) then you won't have to put so many case statements. What do you mean by "doesn't work"? What actually happens? Commented May 13, 2017 at 13:22
  • 1 <=n should be i <= n in the somavlrs function. Commented May 13, 2017 at 13:27

2 Answers 2

1

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>

Sign up to request clarification or add additional context in comments.

2 Comments

If you run your snippet, the second alert comes before the text is put on the page. I think his second question is, is there a way to load the text before the alert?
oh ok, I think he could move that logic into a function and invoke it via the onload attribute on the body tag. Then it won't actually execute until the html document has loaded. Take a look at the snippet now, does that look right?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.