Programming / Re: I Need Designs by divine404: 11:13am On Jun 16, 2022 |
Think4Myself: i want to start building projects with HTML css and vanilla JS for now but I don't know where to find free designs with source files from someone should recommend sites where i can find this https://www.frontendmentor.io/ |
Programming / Re: Can Someone Help Me With With This Javascript Exercise Please by divine404: 7:06pm On Jun 13, 2022 |
solution 2: // Get the index of the first letter of the 2nd sentence. const index = myStr.lastIndexOf("T" // Slice it and get the length. const result = myStr.slice(index).length; console.log(result); |
Programming / Re: How I Would Go About Learning Programming If I Was To Start Today (2021/2022) by divine404: 5:00pm On Nov 01, 2021 |
tensazangetsu20:
Yeah you dont need jquery. Vanilla JS and PHP is okay but I will advise you to look at jamstack with React as that's the future of wordpress. Okay, I'll do that. Thank you. |
Programming / Re: How I Would Go About Learning Programming If I Was To Start Today (2021/2022) by divine404: 3:03pm On Nov 01, 2021 |
Op, i've been reading your posts as a guest for a while, and your story really motivates me to keep learning. Pls, is Jquery necessary for wordpress development, or is vanila js and php ok to get started? |
Programming / Re: Please Help A Beginner, CSS Not Working. by divine404: 7:07am On Oct 14, 2021 |
The "type" attribute in the <style> tag is not required in modern web documents, and linking to an external stylesheet using the <link> tag is preferred. |
Programming / Re: Please I Need Solution To This Js Code by divine404: 4:32pm On Aug 31, 2021 |
olubantylove:
Alright Chief!!! Thanks for pointing this out.. We all learn everyday you know. Yes, boss. You're welcome!!! |
Programming / Re: Please I Need Solution To This Js Code by divine404: 3:27pm On Aug 31, 2021 |
olubantylove: Hello OP, I hope you are taking your time to enjoy JS? Lol. So, to what you asked.
First I’ll point out some errors you made and then provide solutions to them. 1. You have an object called amazonBasket [/b]right? You initialized it without an identifier like no [b]const or let declaration. JavaScript is a forgiving language though, but this is a bad practice as it leads to hoisting just like what the var [/b]does then.
[b]Solution: Add a keyword (const or let) to that.
2. Remember, the amazonBasket is an object not an array, and the obj is an argument pointing to the parameter amazonBasket when you call the checkBasket function. So the obj is still an object right? The forEach method does not work on an object, it only works on arrays and some iterables like nodelist. Solution: When looping through an object you use the “ for in loop”. Like so:
for(const objKey in amazonBasket){ //Execute this code }
3. Why did you resort to using the Object and String objects in your code? To me it is not necessary.
So the solution to what you want to do can be achieved in two ways.
First Option(First Picture) Noticed I only used the in keyword to check if the searchItem [/b]property is present in the [b]basket object. Yes it is possible. And it is common to object. You use the[b] in keyword[/b] to check if a property exist in an object. So the if statement checks that is books present in the basket object as a property? If it is, then it returns books is available else it returns books is not available.
Second Option(second picture) I looped through the basket object to get all of its property using the[b] “for in loop”[/b] I mentioned earlier. And the looped property are stored in the key variable. After which I check for type and value equality, so if any of property that is stored in key is equal to the searchItem, then it is available.
OP I hope you know by property I mean the books, flosses and glasses that are declared inside your amazonBasket object . Also careful how you work with variables lexical environment. For instance, you declared the text [/b]variable has a global variable, you don’t need to return it again inside the function. It will obviously take the recent value even if it is used inside a function. Anyways, I moved it inside the function for you, since you want to return the [b]checkBasket [/b]function. So with [b]console.log, you can access the return value which in this case the [b]text [/b]variable.
I hope this helps OP. Enjoy JavaScript and Coding!!
The "Object.keys(obj)" method returns an array of the obj keys, and assigns the array to the basket variable. 2 Likes |