最后一行為什么這樣輸出?用return可以得到預(yù)期效果,為什么document.write不行?
?function pk(x,y){
? ? ?if(x>y)
? ? ? ? ?{return x;}
? ? ? else if(y>x)
? ? ? ? ?{return y;}
? ? ? else
? ? ? ? ?{document.write("相等!")}
? ? ?}
? document.write(" 5 和 4 的較大值是:"+pk(5,4)+"<br>");
? document.write(" 1 和 3 的較大值是:"+pk(1,3)+"</br>" );
? document.write(" 4 和 4 的較大值是:"+pk(4,4)+"<br>");
////
輸出:
5 和 4 的較大值是:5
1 和 3 的較大值是:3
相等! 4 和 4 的較大值是:undefined
2016-08-15
document.write(" 4 和 4 的較大值是:"+pk(4,4)+"<br>");
換成
document.write(" 4 和 4 的較大值是:");
pk(4,4);
2016-08-12
?function pk(x,y){
? ? ?if(x>y)
? ? ? ? ?{return x;}
? ? ? else if(y>x)
? ? ? ? ?{return y;}
? ? ? else
? ? ? ? ?{return "相等!";}
? ? ?}
? document.write(" 5 和 4 的較大值是:"+pk(5,4)+"<br>");
? document.write(" 1 和 3 的較大值是:"+pk(1,3)+"</br>" );
? document.write(" 4 和 4 的較大值是:"+pk(4,4)+"<br>");