Практикуя функции в программе, чтобы найти объем три angular призмы. Пробовал отлаживать мой код, но не могу понять, почему он не работает - PullRequest
1 голос
/ 04 апреля 2020

Я новичок ie, практикующий написание функций с этой программой, чтобы найти объем призмы tri angular, используя 3 различные функции. Несмотря на все мои усилия по отладке, я не могу понять, почему это не работает. Я проверил свою математику и математические работы. Любая помощь приветствуется.

<!DOCTYPE html>
<html lang="en-us">
    <head>
        <meta charset="utf-8">
        <title>Volume of a Triangular Prism</title>
        <script>
            /* Function triangleArea() is called by prismVolume(), which is called by 
               doInputOutput(). 
             * doInputOutput() is the only function that takes use input and outputs result to a div.
            */

            function doInputOutput() {
                var a = parseFloat(document.getElementById('aInput').value);
                var b = parseFloat(document.getElementById('bInput').value);
                var c = parseFloat(document.getElementById('cInput').value);
                var length = parseFloat(document.getElementById('lengthInput').value);

                var vol = prismVolume(a, b, c, length);

                // Rounds to 2 places.
                var digits = 2;
                var mult = Math.pow(10, digits);
                var volume = Math.round(tVol * mult) / mult;

                document.getElementById('volDiv').innerHTML = volume;
            }

            function prismVolume(a, b, c, length) {
                var s = (a + b + c) / 2;
                var area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

                var tVol = area * length;
                return tVol;
            }

            function triangleArea(a, b, c) {
                var s = (a + b + c) / 2;
                var area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
                return area;
            }
        </script>
    </head>
    <body>
        This program will compute the volume of a triangular prism.<br>
        Please enter the following triangular prism measurements:<br>
        Side 1: <input id="text" id="aInput"><br>
        Side 2: <input id="text" id="bInput"><br>
        Side 3: <input id="text" id="cInput"><br>
        Length: <input id="text" id="lengthInput"><br>
        <button type="button" onclick =doInputOutput()>Volume</button>
        <div id="volDiv"></div>
    </body>
</html>

Ответы [ 3 ]

0 голосов
/ 04 апреля 2020

У вас есть несколько вопросов. Но самое главное, что tVol не определено. Это приводит к запуску функции до sh, поэтому вы не получите никакого результата. Вы можете увидеть это в браузере, используя консоль в средствах отладки.

Сначала измените свои входные данные:

  Side 1: <input type="text" id="aInput"><br>
  Side 2: <input type="text" id="bInput"><br>
  Side 3: <input type="text" id="cInput"><br>
  Length: <input type="text" id="lengthInput"><br>

Во-вторых:

tVol - неправильная переменная , Измените свою функцию следующим образом:

function doInputOutput() {
  var a = parseFloat(document.getElementById('aInput').value);
  var b = parseFloat(document.getElementById('bInput').value);
  var c = parseFloat(document.getElementById('cInput').value);
  var length = parseFloat(document.getElementById('lengthInput').value);

  var vol = prismVolume(a, b, c, length);

  // Rounds to 2 places.
  var digits = 2;
  var mult = Math.pow(10, digits);
  var volume = Math.round(vol * mult) / mult;

  document.getElementById('volDiv').innerHTML = volume;
}

вы заметите, что в Math.round(vol * mult) / mult; я изменил tVol на vol

0 голосов
/ 04 апреля 2020

В вашем коде много проблем. Я исправил этот код, и, пожалуйста, попытайтесь сопоставить его с существующей кодовой базой и поймите логики c, почему мы столкнулись с этой проблемой.

<!DOCTYPE html>
<html lang="en-us">
    <head>
        <meta charset="utf-8">
        <title>Volume of a Triangular Prism</title>
        <script>
            /* Function triangleArea() is called by prismVolume(), which is called by 
               doInputOutput(). 
             * doInputOutput() is the only function that takes use input and outputs result to a div.
            */

            function doInputOutput() {
                var a = parseFloat(document.getElementById('aInput').value);
                var b = parseFloat(document.getElementById('bInput').value);
                var c = parseFloat(document.getElementById('cInput').value);
                var length = parseFloat(document.getElementById('lengthInput').value);

                var tVol = prismVolume(a, b, c, length);

                // Rounds to 2 places.
                var digits = 2;
                var mult = Math.pow(10, digits);
                var volume = Math.round(tVol * mult) / mult;

                document.getElementById('volDiv').innerHTML = volume;
            }

            function prismVolume(a, b, c, length) {
                var s = (a + b + c) / 2;
                var area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

                var tVol = area * length;
                return tVol;
            }

            function triangleArea(a, b, c) {
                var s = (a + b + c) / 2;
                var area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
                return area;
            }
        </script>
    </head>
    <body>
        This program will compute the volume of a triangular prism.<br>
        Please enter the following triangular prism measurements:<br>
        Side 1: <input id="aInput"><br>
        Side 2: <input id="bInput"><br>
        Side 3: <input id="cInput"><br>
        Length: <input id="lengthInput"><br>
        <button type="button" onclick=doInputOutput()>Volume</button>
        <div id="volDiv"></div>
    </body>
</html>

Теперь ваша программа работает правильно.

0 голосов
/ 04 апреля 2020

Измените свои input s следующим образом

Side 1: <input type="text" id="aInput"><br>
Side 2: <input type="text" id="bInput"><br>
Side 3: <input type="text" id="cInput"><br>
Length: <input type="text" id="lengthInput"><br>

В настоящее время вы даете элементам input 2 идентификатора, но вам нужно установить тип текста, а затем настроить id.

Также необходимо переместить переменную tVol в prismVolume():

/* Function triangleArea() is called by prismVolume(), which is called by 
          doInputOutput(). 
        * doInputOutput() is the only function that takes use input and outputs result to a div.
      */

function doInputOutput() {
  var a = parseFloat(document.getElementById("aInput").value);
  var b = parseFloat(document.getElementById("bInput").value);
  var c = parseFloat(document.getElementById("cInput").value);
  var length = parseFloat(document.getElementById("lengthInput").value);

  var volume = prismVolume(a, b, c, length);

  document.getElementById("volDiv").innerHTML = volume;
}

function prismVolume(a, b, c, length) {
  var s = (a + b + c) / 2;
  var area = Math.sqrt(s * (s - a) * (s - b) * (s - c));

  var tVol = area * length;

  // Rounds to 2 places.
  var digits = 2;
  var mult = Math.pow(10, digits);
  var volume = Math.round(tVol * mult) / mult;
  
  return volume;
}

function triangleArea(a, b, c) {
  var s = (a + b + c) / 2;
  var area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
  return area;
}
This program will compute the volume of a triangular prism.<br />
Please enter the following triangular prism measurements:<br />
Side 1: <input type="text" id="aInput" /><br />
Side 2: <input type="text" id="bInput" /><br />
Side 3: <input type="text" id="cInput" /><br />
Length: <input type="text" id="lengthInput" /><br />
<button type="button" onclick="doInputOutput()">Volume</button>
<div id="volDiv"></div>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...