否则,如果语法错误?

时间:2014-08-04 22:13:45

标签: javascript arrays if-statement for-loop

var to = ['user1', '@user2', 'user3@email.com']

以下代码集应保留' user1'在to数组中,删除' @'从' @ user2'开头,移动' user3@email.com'到toEmail数组

目前,else if (toName[j] == '@') {正在记录'语法错误'

var toEmail = [];
var pushToEmail;
for (i=0; i<to.length; i++) {
  var pushToEmail = false;
  var toName = to[i];
  for (j=0; j<toName.length; j++) {
    if (toName[0] == '@') {
      toName[0] = toName[0].replace(/@/g, ''); // remove '@''
    }
    console.log('toName[0] = ' + toName[0]);
    else if (toName[j] == '@') {
      pushToEmail = true;
      break;
    }
  }
  if (pushToEmail == true) {
    toEmail += to.splice(INDEX, 1)[i];
  }
}

1 个答案:

答案 0 :(得分:2)

您遇到语法错误。您不能只在<{strong> <{1}}和console.log之间的空格<{1}}中投放ifelse必须附加到else语句,您已尝试将if附加到else