Why the object would not be removed from localstorage?

Why the object would not be removed from localstorage?
0

#1

I am trying to store the favorite jokes into a localstorage and when the user uncheck the favorites, that object should be removed from the localstorage.

But in this case it would not be removed as you might notice.

So how couldI fix this.


#2

I’m sorry, I tried parsing through your code but I’ve run out of time.

First of all, I think you need to clean things up a bit. Like the old restaurant saying goes, “clean as you go”.

In your HTML, you have a bunch of things that codepen doesn’t like. For one, you are trying to call local js and css files - you can’t do that in codepen. Everything is self contained. And linking to things in jQuery? You should let codepen handle that in the settings/javascript - import it there. For codepen, there should be nothing inside the HTML except for what would normally go in the body - without the body tags. This is different than how a web page is set up in real life, but codepen is a controlled environment.

In your javascript, there are some minor formatting issues. But what struck me most is mixing vanilla JS and jQuery. Why? It would be like wearing a belt and suspenders at the same time. You have the line:

let favorite = document.getElementById('favorites');

and then later on:

$target = $(favorite),

So, you are caching your vanilla JS selector, and then recaching it as a jQuery selector, using the vanilla JS selector? Does that work? I guess it does, but why do it?

I’m guessing you’ve done some cutting and pasting here without completely understanding what you’re pasting. I’d recommend taking a step back and understanding what you’re doing first. Write some small test programs first.

As to removing a joke, since it is all stored under a single key, AFAIK you need to reset that key, just like you did for the first one. Just save the new array/string of all the jokes to that key. If you think that that is what you are doing but it is not working, then put in some console.log statements and figure out what is supposed to be happening that isn’t. Also remember that you can view local storage directly in the browser console. On my chrome browser console, it is Application/Storage/Local Storage/https://s.codepen.io.

Sorry I don’t have time for better advice, but the wife is calling.


#3

Giving a lot of feedback without giving a solution is like talking without doing.


#4

I think @kevinSmith gave you a good start here, spent his time generously assessing your code. He explained to you why he wasn’t able to give you more than he could. Be grateful, not spiteful. Usually a simple thank you is appropriate in these situations.


#5

Actually, @kevinSmith gave you advice on how to figure it out on your own. Many of us could “give” you a solution, but it would be better for your learning experience to take the small hints and advice to attempt to better your current solution. You can always ask more questions after you attempt your own fix first. We will always try to assist in your learning here, but we will not always give you an entire solution. As an FCC student, you should strive to make your own solutions. In the end, you will get much more satisfaction on developing your own solutions with as little “help” as possible.


#6

Yep, pretty much. That’s because the “doing” is your job. Don’t expect others to do your work for you. A teacher that did the student’s work would be a terrible teacher.


#7

At the risk of stirring the pot …

I’m sorry that you feel that my answer was insufficient. As others have pointed out, we’re not here to do your project for you. But if you learn how to ask questions, you will find that this is an amazingly supportive group.

As I said, I feel like you are trying to use to many things that you don’t quite understand yet. It’s like you handed me a ball of metal pipes and strips of rubber held together with duct tape and said, “I’m trying to build a bicycle so I can learn to be a bicycle builder. Please fix it for me and make it work.” Yes, I could take it back to my shop and spend a few hours fixing your bicycle for you, but what would you learn? In order to learn, there would be some fundamental concepts you’d have to understand better.

As I said, it looked to me like you were cutting and pasting from other sources. That is fine - we all do that. But it is important to understand what you are cutting and pasting and often have to make changes.

I won’t bother relisting through the problems I saw in your code. I will add that I wasn’t quite sure what you wanted to do and I couldn’t find where you were trying to do it.

I would suggest taking a step back and starting from scratch. I’ve had to do this on several projects. Now that you have some information and know what you need to understand better. Build some small projects to test that knowledge. Don’t try to build a bicycle until you understand how a wheel works and have proven to yourself that you can build a good wheel.

The other comment I’d make is that there were some errors in your browser console. As someone that often helps people with problems like this. it is a pet peeve of mine when open up the console and find errors that they are ignoring. Those errors may not be related to the problem, but it tells me that this person is trying to do things beyond their ken.

As I said, build some smaller test projects. If you have a problem, ask the forum. Provide a link to your pen and say exactly what you want it to do and what it is doing instead. You can even cut and paste the code you think is the problem in the post, or at least a line number so we don’t have to parse all of your code.

Keep questions small and specific. Format your code, and put in comments where needed. And monitor your browser consoles for errors.

Good luck.


#8

Hello Kevin,

You are right. I need to first understand the basics well in order to build small reusable, maintainable and testable functions.