문제

As an academic exercise I'm writing a function that takes an email address as a string and splits it in two and removes the "@" then places the two split strings in to an array. The two items placed in the array will be the local and domain names. e.g. bob@yahoo.com would become array = ['bob','yahoo.com']. I'm avoiding using the split() method and regEx for this exercise.

My code is breaking in the bottom for loop in the condition. The || in the condition is causing an infinite loop. I've tried each condition on its own and the loop runs ok. However I need both statements in the condition (as it checks for spaces at the end of the email as well as if there're no blank spaces).

I've tried swapping conditions over and can't find any reason why this is causing an infinite loop. Why is the code breaking here, how could it be resolved ?

var email = ' paul@hotmail.com ';

function checkE(email) {
  var arr = [];
  var local = '';
  var domain = '';
  var i = 0;

  while(email[i] == ' ') {

      i++
  }

for(var j = i; email[j] != '@'; j++) {

    local = local + email[j];
    i++
}
arr.push(local);

for(var k = i+1; email[k] != ' ' ||  k < email.length ; k++) {
    domain = domain + email[k];
}
arr.push(domain);
alert(arr);
}
checkE(email);
도움이 되었습니까?

해결책

One of the 2 conditions will most likely always be true.

Let's say email = 'paul@hotmail.com '; email.length equals 17.

email[16] equals ' ' so the email[k] != ' ' evaluates to false but we have not reached the length of email so k < email.length is true; so, we will enter the loop.

email[17] equals null so email[k] != ' ' evaluates to true. Even though we have reached the end of our string and k < email.length is false.

In other languages such as java you would get a IndexOutOfBounds exception for accessing an index in the array greater than the number of elements in the array. Javascript however allows you to do this and just returns null.

What you really want is to iterate only while both of the conditions are true, not one or the other.

var length = email.length;
for(var k = i+1; k < length && email[k] != ' '; k++)

This article on trimming a string may also be helpful/inciteful to you: http://blog.stevenlevithan.com/archives/faster-trim-javascript

다른 팁

Your problem is in this statement:

email[k] != ' '

If email[k] == '' or email[k] == null, your code breaks.

Replace the || with &&.

Because of the ||,

k < email.length

Will allow the loop to run while you haven't reached the end of email. Then,

email[k] != ' '

Will allow the loop to run because null != ' '.

The condition to terminate for loop is not correct. It should be

for(var k = i+1; email[k] != ' ' &&  k < email.length ; k++) {
    domain = domain + email[k];
}

Note that the condition (email[k] != ' ' || k < email.length) is always true. Let say when k reaches email.length - 1 then you have email[k] == ' ' but k < email.length. You continue with k = email.length, at this point email[k] == null so email[k] != ' ' is true. It is the same with k > email.length

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top