2017-02-14 240 views
0

我的程序有問題。首先,我會告訴你它是做什麼的: 它應該爲你做畢達哥拉斯定理。 Theorem Explanation Here.if else if else語句(JS)

這裏是數學式:(A * A)+(B * B)=(C * C)

下面是本程序:

function pythagorean (a, b, c) { 
if (c === null){ 
    return 'c = ' + Math.sqrt((a*a) + (b*b)); 
} else if (b === null) { 
    return 'b = ' + Math.sqrt((c*c) - (a*a)); 
} else if (a === null) { 
    return 'a = ' + Math.sqrt((c*c) - (b*b)); 
} else { 
    return "I'm sorry, I don't understand." 
}; 
}; 

我決定被看中和儘量使它如此,以便如果雙方:A和B爲空,程序將假設=== b,並且它的公式將變成:[不管是C] c = Math.sqrt(2 *( a * a)+(b * b)))

(對不起,令人困惑)

所以,我想執行一些代碼,使其工作:

function pythagorean (a, b, c) { 
if (c === null){ 
    return 'c = ' + Math.sqrt((a*a) + (b*b)); 
} else if (a && b === null){ 
    return 'if a and b are equal, then a and b both equal ' + Math.sqrt((c * c)/2); 
} else if (b === null) { 
    return 'b = ' + Math.sqrt((c*c) - (a*a)); 
} else if (a === null) { 
    return 'a = ' + Math.sqrt((c*c) - (b*b)); 
} else { 
    return "I'm sorry, I don't understand." 
}; 
}; 

但每當我得到C,一個值,但既是& b === null,則古怪只是跳過else if語句可與去直到b === null否則if語句並給我:'b ='和一些隨機答案。我需要你們男孩/女孩的幫助!

+0

這將是更容易,如果(現在或下一時間),你可以把它通過jsfiddle或通過stackoverflow轉換爲可運行的代碼(點擊第二列,從左邊的第二列)。 –

+4

'a && b === null'不會做你認爲它做的事。它檢查'a'是否爲真,並且'b === null'爲真。 – Biffen

+0

a === null && b === null。你必須分別檢查每一個。 – shadymoses

回答

0

if (a && b === null)不檢查,如果他們都null正確的方法,它的作用是檢查atruthybnull,有效:

if ((a) && (b === null)) 

你應該改用類似的東西:

if ((a === null) && (b === null)) 

這是解決你的眼前問題,但是,如果你正在尋找能夠處理所有錯誤的情況下(兩側是null等爲一體時斜邊也是空)的解決方案,你可能會更好地擴大您的支票一點。您可能會認爲這會使代碼複雜化,但您也可以通過了解以前的檢查無法做到的情況,從而大大簡化工作。

因爲我不是if (something) return else範例的忠實粉絲(在這種情況下,else總是多餘的),所以我刪除了它們。我也傾向於更喜歡稍微有意義的變量名稱。

下面的代碼包含了所有這些變化,並添加註釋解釋每個部分:

// pythagorean: 
// Returns string indicating "missing" info for a 
// right-angled triangle (two regular sides and the 
// hypotenuse). Basic outcome is: 
//  Hypotenuse null, sides given, return hypotenuse. 
//  Sides null, hypotenuse given, return equal sides. 
//  Hypotenuse and only one side given, return other side. 
//  Anything else, error given. 

function pythagorean (s1, s2, hyp) { 
    // At least one value must be null. 

    if ((hyp !== null) && (s1 !== null) && (s2 !== null)) { 
     return "ERROR: all fields given, don't know what you want"; 
    } 

    // If hypotenuse is null, need both other sides or error. 
    // Returns hypotenuse. 

    if (hyp === null) { 
     if ((s1 === null) || (s2 === null)) { 
      return "ERROR: hypotenuse is null, other sides cannot be"; 
     } 
     return "hypotenuse is " + Math.sqrt((s1 * s1) + (s2 * s2)); 
    } 

    // At this point, we KNOW we have hypotenuse, don't check again. 
    // If BOTH sides are missing, assume equal. 

    if ((s1 === null) && (s2 === null)) { 
     return "sides are both " + Math.sqrt((hyp * hyp)/2); 
    } 

    // At this point, we have hypotenuse and exactly one side 
    // (not the other), so just return the other side. 

    if (s1 === null) { 
     return "side 1 is " + Math.sqrt((hyp * hyp) - (s2 * s2)); 
    } 
    return "side 2 is " + Math.sqrt((hyp * hyp) - (s1 * s1)); 
} 
0

有條件的經營者應該是這樣的

一個===空& & b ===空

因爲它會檢查爲空和b也爲空。

一個& & b ===空

上述條件將檢查是否有一些值(例如1),不應該是 「0」 和空,NaN時, '' 和 「」 和b一片空白。

function pythagorean (a, b, c) { 
 
if (c === null){ 
 
return 'c = ' + Math.sqrt((a*a) + (b*b)); 
 
} else if (a === null && b === null){ 
 
return 'if a and b are equal, then a and b both equal ' + Math.sqrt((c * c)/2); 
 
} else if (b === null) { 
 
return 'b = ' + Math.sqrt((c*c) - (a*a)); 
 
} else if (a === null) { 
 
return 'a = ' + Math.sqrt((c*c) - (b*b)); 
 
} else { 
 
return "I'm sorry, I don't understand." 
 
}; 
 
}; 
 

 
var abc = pythagorean(null,null,2); 
 
alert(abc);

+0

「有些價值」?你的意思是,像0或「」? :-)我認爲你要找的短語是「truthy」。 – paxdiablo

+0

@paxdiablo不,它應該是真正的價值。 –

+0

我想你會發現'0','false','null','NaN',''''和'「」'都是值。 – paxdiablo

0

這個看起來正確

else if (a==0&b==0)