2013-05-11 7 views
1

Функция матричного детерминанта не работает, и я не знаю, почему. Мне кажется, все в порядке. Может кто-нибудь мне помочь.Определитель матрицы Javascript

Html

<div id = "table1"> 
      <div class = "header">Wyznacznik [2x2]</div> 
       <form id = "row1"> 
        <input type = "text" class = "det1"/><!--first row--> 
        <input type = "text" class = "det1"/> 
       </form> 
       <form id = "row2"> 
        <input type = "text" class = "det1"/><!-- second row--> 
        <input type = "text" class = "det1"/> 
       </form> 
      <div class = "count"><a href = "#" onclick="det('det1','caclValue2')">Wylicz</a></div> 
       <input type = "text" id = "calcValue2"/> 
      </div> 

Javascript

function det(className,outputId){ 
var arr = document.getElementsByClassName(className); 
var determinant = 0; 
if(arr.length == 2){ 
determinant = (arr[0].value*arr[3].value) - (arr[1].value*arr[2].value); 
} 
else if(arr.length == 3){ 
determinant = (arr[0].value*((arr[4].value*arr[8].value) - (arr[5].value *  arr[7].value))) - 
(arr[1].value*((arr[3].value*arr[8].value) - (arr[5].value * arr[6].value))) + 
(arr[2].value*((arr[3].value*arr[7].value) - (arr[4].value * arr[6].value))); 
} 
document.getElementById(outputId).value = determinant; 
return determinant; 
} 

EDIT !: если оператор должен иметь arr.length == 4 и еще если arr.length == 9.

ответ

0

Вы фильтровать длина массива равна 2, но после этого вы пытаетесь получить доступ к индексам, превышающим это:

if(arr.length == 2){ 
    determinant = (arr[0].value*arr[3].value) - (arr[1].value*arr[2].value); 
} 

То же самое для другого оператора if. Вы действительно видели ошибки в консоли?

+0

Консоль предоставила мне «Uncaught TypeError: Невозможно установить значение свойства« null »erron на 11 строке (document.getElementById (outputId) .value). – user2167174

+0

Это из-за опечатки. Измените 'caclValue2' на 'calcValue2'. –