You need to do the following: The user enters degrees Fahrenheit, the program considers the formula ((our value) - 32) / (5/9). At what it should still add 1 value 5 times and output as a table.

Here is what I have at the moment:

<form id='temp'> <input id="t" placeholder="Введите температуру по F"/> <button onclick='temp()'>Перевести температуру</button> </form> 

then I get the value from the input:

 function temp() { var t = document.getElementById('t').value; for (var i = t; i >= t; i++) { var c = (i - 32) / (5/9); alert(++c); } } 

My for cycle is no longer correct, I know it, but I cannot figure out how to do it right. The output should be either through an alert or through document.write, the main thing is that all values ​​appear together.

  • on pure javascripte need to implement? When entering the same value, it is necessary to vivod 5 subsequent, for example, if you entered 5 then vivodit 5, 6, 7, 8, 9? - vov4ok
  • It is desirable to clean, but not necessarily. Yes, about the input is correct, when you enter one value, it displays this value and 5 more next .... - Nikita

2 answers 2

For a constantly increasing variable of cycle i, the condition i >= t is always true, since i = t already at the first iteration. In the meantime, this condition is true, the cycle will not stop. That is, it is infinite. To fix this, you can either change the condition to i <= t+5 , or the loop with the for counter can generally be replaced with a loop with a check of the while condition. In the second case, the code will be less with the same functionality. Now, regarding alert(++c); Why do you increase the value с before output? This makes no sense, since the values ​​of с calculated for i at each iteration. According to the meaning of the problem, c = f (i), and you now have: c = f (i) +1. I am silent about the fact that alert does not make a table, it throws out to the user a pop-up window with a single value that blocks further program execution until it is closed. The formula for converting degrees to Fahrenheit to degrees Celsius is wrong. I sketched a correction for your task here:

 <script type="text/javascript"> function temp() { var t = document.getElementById("t").value; //Делаем заголовок таблицы document.write("<table border=1>"); document.write("<tr><th>F</th><th>C</th></tr>"); for (var i = t; i <= +t+5; i++) { var c = (i - 32) * (5/9); document.write('<tr><td>'+i+'</td><td>'+c+'</td></tr>'); } document.write('</table>'); } </script> <form id='temp'> <input id="t" placeholder="Введите температуру по F"/> <button onclick='temp()'>Перевести температуру</button> </form> 

I made a standard frame for the table and merged html and js into one file only in order to understand it faster, without being distracted by the separation of code by files and design. Redo as you need. Unary plus in the condition i<=+t+5 necessary for the correct type conversion. Without it, if, for example, t = 7, t + 5 = 75, and with it - t + 5 = 12.

     function temp() { var t = +document.getElementById('t').value; for (var i = 0; i < 5; i++) { var a = (t+i) + " ==== "+((t+i) - 32) * (5/9) + "<br>"; document.write(a); } } 
      <input id="t" placeholder="Введите температуру по F"/> <button onclick='temp()'>Перевести температуру</button> 

    • Is it possible to withdraw everything in one alert? - Nikita
    • Can vivod where necessary. So go? - vov4ok