Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 ====

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading