2014-02-24 48 views
0

我想創建一個簡單的二進制到JavaScript中使用邏輯函數的十進制表。輸出應如下所示:在javascript中創建一個簡單的二進制到十進制表 - 家庭作業

Binary |十進制
CCCC
| 0 1 2 3 4 5 6 7 8 9
0 0 0 0 | 1 0 0 0 0 0 0 0 0 0
0 0 0 1 | 0 1 0 0 0 0 0 0 0 0
0 0 1 0 | 0 0 1 0 0 0 0 0 0 0
0 0 1 1 | 0 0 0 1 0 0 0 0 0 0
0 1 0 0 | 0 0 0 0 1 0 0 0 0 0
0 1 0 1 | 0 0 0 0 0 1 0 0 0 0
0 1 1 0 | 0 0 0 0 0 0 1 0 0 0
0 1 1 1 | 0 0 0 0 0 0 0 1 0 0
1 0 0 0 | 0 0 0 0 0 0 0 0 1 0
1 0 0 1 | 0 0 0 0 0 0 0 0 0 1

這是我到目前爲止,不知道爲什麼它不工作 - 我想邏輯函數有什麼問題,任何幫助將不勝感激:)

<script language="JavaScript"> 

var C3 = new Array(0,0,0,0,0,0,0,0,1,1); 
var C2 = new Array(0,0,0,0,1,1,1,1,0,0); 
var C1 = new Array(0,0,1,1,0,0,1,1,0,0); 
var C0 = new Array(0,1,0,1,0,1,0,1,0,1); 

var D0,D1,D2,D3,D4,D5,D6,D7,D8,D9; 

// printing out the header of the table 
document.write(" C3 C2 C1 C0| 0 1 2 3 4 5 6 7 8 9 <BR>"); 


for (var i = 0; i <= 9; i++) 
{ 
// calculating values according to logical equations 
D0 = AND(NOT(C3[i]),NOT(C2[i]),NOT(C1[i]),NOT(C0[i])); 
D1 = AND(NOT(C3[i]),NOT(C2[i]),NOT(C1[i]),C0[i]); 
D2 = AND(NOT(C3[i]),NOT(C2[i]),C1[i],NOT(C0[i])); 
D3 = AND(NOT(C3[i]),NOT(C2[i]),C1[i],C0[i]); 
D4 = AND(NOT(C3[i]),C2[i],NOT(C1[i]),NOT(C0[i])); 
D5 = AND((NOT(C3[i]),C2[i],NOT(C1[i]),C0[i]); 
D6 = AND(NOT(C3[i]),C2[i],C1[i],NOT(C0[i])); 
D7 = AND(NOT(C3[i]),C2[i],C1[i],C0[i]); 
D8 = AND(C3[i],NOT(C2[i]),NOT(C1[i]),NOT(C0[i])); 
D9 = AND(C3[i],NOT(C2[i]),NOT(C1[i]),C0[i]); 

// printing the outputs 
document.write(C3[i] + " " + C2[i] + " " C1[i] + " " + C0[i] + " | " + D0 + " " + 
D1 + " " + D2 + " " + D3 + " " + D4 + " " + D5 + " " + D6 + " " + D7 + " " + D8 
+ " + D9 + "<BR>"); 
} 

// defining function NOT 
function NOT(inp) { 
var out = 1 & (!inp); 
return out; 
} 

// defining function AND 
function AND(in1,in2,in3,in4) 
{ 
var out = in1 && in2 && in3 && in4; 
return out 
} 

</script> 

回答

0

在運行代碼之前,您正在定義ANDNOT函數。在運行for循環之前剪切它們並粘貼它們,並查看它是否有效。

+0

謝謝,這與在D5中刪除一個錯字使它:) – Vero

+0

@Vero,如果我的答案解決了你的問題,你介意接受它嗎?謝謝。 :D – Atutouato

+0

當然,再次感謝! – Vero