Tell us what’s happening:
I am unable to pass the tests even though my code seems alright . Please help me figure out what’s wrong. Your code so far
class Set {
constructor() {
// Dictionary will hold the items of our set
this.dictionary = [];
this.length = 0;
}
// This method will check for the presence of an element and return true or false
has(element) {
return this.dictionary.indexOf(element) !== undefined;
}
// This method will return all the values in the set
values() {
return this.dictionary;
}
// Only change code below this line
add(item){
if(!~this.has(item)){
this.dictionary.push(item);
this.length++;
return true;
}
return false;
}
remove(item){
var index = this.dictionary.indexOf(item);
if(~index){
this.dictionary.splice(index,1);
this.length--;
}
}
size(){
return this.length;
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0.
You should only add the methods add, remove and size and don’t change the rest of the code.
So the dictionary is required to be an object, not an array. Btw. that’s why we also keep track of length. If we were to store dictionary as array, we could just get the length like this this.dictionary.length
Also, I’m not sure why you’re using ~ operator here. It’s enough if you use ! for negation.
Tell us what’s happening:
I think I’ve written the code according to the given conditions but it seems to faill nearly all the tests.
Your code so far
class Set {
constructor() {
// Dictionary will hold the items of our set
this.dictionary = {};
this.length = 0;
}
// This method will check for the presence of an element and return true or false
has(element) {
return this.dictionary.indexOf(element) > -1;
}
// This method will return all the values in the set
values() {
return Object.keys(this.dictionary);
}
add(item){
if(!this.has(item)){
dictionary.push(item);
return true;
}
return false;
}
remove(item){
var idx = this.dictionary.indexOf(item);
if(idx === -1){
return false;
}else{
return this.dictionary.splice(idx,1)[0];
}
}
size(){
return Object.keys(this.dictionary).length;
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0.
@sanadKadu I have merged your threads. Please do not create duplicate threads for the same question. You can always post your latest code in the thread, just remember to format it.
When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
so this means that the add() method should have two inputs? like value and item?
and are my conditionals ok?
like if i want to check whether the item already exists like !this.has(item) ?
and the size() method ?
@sanadKadu
yes if(!this.has(item)) for add function and if(this.has(item)) for remove function, and your size look good that should pass, and yes a value and item the value can be anything.