2016-09-12 3 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 및 코딩에 완전히 익숙하다. 누군가이 코드가 어떻게 작동하는지 설명해주십시오. 입력 상자에 값이 있는지 여부를 확인하는 데 사용 중이라는 것을 알고 있지만 더 이상 알지 못합니다.이 자바 스크립트 기능 코드를 이해

질문 업데이트 .... 위의 코드에서 for 루프가 실행되고 문자열이 비어 있지 않으면 false를 반환합니다. for 루프가 끝나면 브라우저는 다음 행을 읽습니다. - true를 반환합니다. 그래서 함수가 마침내 true를 반환하지는 않습니다. 중간에 거짓이 있으면 상관 없습니다.

+0

@ OrangeFlash81 왜 내 편집을 거부 했습니까?! –

+0

@MehdiDehghani 보류중인 편집을 알게되기 전에 거의 동일한 변경을했습니다. 죄송합니다. –

+3

왜이 메소드, 속성 등의 문서를 읽을 수 없습니까? 이것은 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 (내가) 문자열의 인덱스 난에있는 문자를 돌려줍니다 같지 않으면 false를 반환합니다. 조건이 모든 문자에 대해 충족되지 않으면 true를 반환합니다.