2016-09-12 78 views
-3
function isBlank(s){ 
    var len = s.length 
    var i 
    for(i=0; i<len; ++i) { 
     if(s.charAt(i)!= " ") return false 
    } 
    return true 
} 

我完全不熟悉JavaScript和編碼。請有人解釋我是如何工作的。 我知道它是用來檢查一個輸入框是否有一些值,但我不知道更多。瞭解此javascript功能代碼

問題更新.... 在上面的代碼中,請參閱for循環運行,如果字符串不爲空,它將返回false。 現在爲循環結束和瀏覽器讀取下一行 - 返回true--。所以不是最終返回true的函數。無論中間是否有回報錯誤。

+0

@ OrangeFlash81你爲什麼拒絕我的編輯? –

+0

@MehdiDehghani在我意識到您有待編輯之前,我做出了幾乎相同的更改。抱歉。 –

+3

爲什麼你不能只讀這些方法,屬性等文件?這是非常基本的javascript,你可以在5分鐘內學到 –

回答

1

它遍歷字符串s並檢查每個字符是否爲空格。如果有空格以外的字符,則函數返回false,因爲該字符串不是空白。如果字符串爲空或僅包含空格,則它返回true,因爲該字符串爲空。

0
function isBlank(s){ // it is a function named 'isBlank' that accept one parameter, that the parameter is something passed from the outside 
    var len = s.length // Assign the length of parameter 's' into a local variable 'len' 
    var i // Declare a new local variable 'i' 
    for(i=0;i<len;++i) { // This is a 'loop', you can google it 
     if(s.charAt(i)!= " ") return false // if any character inside the parameter 's' is not an empty space, that means it isn't blank, so return false 
    } 
    return true; // If code reach this line that means 's' is either with 0 length or all characters of it are an empty space 
} 

通過使用上述函數:

alert(isBlank("123")); // false

alert(isBlank("")); // true

alert(isBlank(" ")); //true

0

的函數檢查字符串是否是空的或不是。

for(i=0;i<len;++i) { // iterates through the string 
    if(s.charAt(i)!= " ") // checks whether character at index i of string s is not equal to " ". 
     return false 
} 

它循環通過串和如果任何字符是不等於「」 .s.charAt(ⅰ)在字符串s的索引i返回字符返回false。 如果條件不滿足每個字符,則返回true。