未知语法错误:意外的令牌变量

时间:2017-11-02 02:13:41

标签: javascript

我在处理leetcode问题时难以接受,以下Javascript代码会引发语法错误:第33行:SyntaxError:意外的令牌变量。我的代码可能对于leetcode问题是错误的,但我特别询问我的语法出错了。

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function(l1, l2) {
    let list = new ListNode(0);
    let head = list;
    let carry = 0;
    while (l1.next != null || l2.next != null){
        let x = l1.val;
        let y = l2.val;
        if (x + y > 9){
            list.val = ((x + y) % 10) + carry;
            carry = 1;
        } else {
            list.val = x + y + carry;
            carry = 0;
        }
        list = list.next;
        l1 = l1.next;
        l2 = l2.next;
    }
    return head;
}

1 个答案:

答案 0 :(得分:1)

尝试在函数定义的末尾添加分号:

var func = function(){

}; // <- check here

注意结尾处的分号。 某些代码中可能缺少未包含在您的响应中的代码,其中可能包含另一个函数定义。

HTH

var addTwoNumbers = function(l1, l2) {
    let list = new ListNode(0);
    let head = list;
    let carry = 0;
    while (l1.next != null || l2.next != null){
        let x = l1.val;
        let y = l2.val;
        if (x + y > 9){
            list.val = ((x + y) % 10) + carry;
            carry = 1;
        } else {
            list.val = x + y + carry;
            carry = 0;
        }
        list = list.next;
        l1 = l1.next;
        l2 = l2.next;
    }
    return head;
}; // <------------ here