Sam McHargue
Sam McHargue

Reputation: 3

Why is this code returning a runtime error in line 8

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var containsDuplicate = function(nums) {
    let base = nums.length;
    let diff = [...new Set(nums)].length;
    if base !== diff return true // <--- this line
    else {
        return false
    }
};

the console is saying there is an error with base in the 8th line. This is Leetcode question #217.

I've tried the following:

changing spelling from length and legnth
tried changing variablenames
tried switching return true and false
and changing the equation to !== instead of ====

Upvotes: 0

Views: 46

Answers (2)

student
student

Reputation: 26

Ok here is the syntax of if-else condition:

 if(some condition){
   do that;
 }else{
   do that;
}

in your case it should look like that: if (base !== diff) return true else { return false } and make sure when you use !== you checks also data type I hope that was helpful

Upvotes: 1

Sam McHargue
Sam McHargue

Reputation: 3

Found the answer

I forgot to put () after the if on line 8. Thanks to @tadman.

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var containsDuplicate = function(nums) {
    let base = nums.length;
    let diff = [...new Set(nums)].length;
    if (base !== diff) return true
    else {
        return false
    }
};

Upvotes: 0

Related Questions