Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,208,066 members, 8,001,377 topics. Date: Wednesday, 13 November 2024 at 09:35 AM |
Nairaland Forum / Science/Technology / Programming / Please I Need Solution To This Js Code (509 Views)
Wrote My First JS Code On VS Code.... Lol (pics) / What Would Be The Output Of This JS Code (pics) / Please I Need Explanation Of This Express.js Code (2) (3) (4)
Please I Need Solution To This Js Code by DDayve: 11:16am On Aug 31, 2021 |
Gurus in the house, please I need you to help me debug this code. I am still new in the business amazonBasket = { glasses: 1, books: 2, floss: 100 } function checkBasket(obj, searchItem){ let basket = Object.keys(obj) basket.forEach((value)=>{ if (String(value)===String(searchItem)){ return `${searchItem} is Available` } }) } checkBasket(amazonBasket, "books" I am expecting the code above to return books is Available but what i get is undefined. please what am I doing wrongly?
|
Re: Please I Need Solution To This Js Code by afuye(m): 1:56pm On Aug 31, 2021 |
const amazonBasket = { glasses: 1, books: 2, floss: 100 } function checkBasket(obj, searchItem){ let basket = Object.keys(obj); var item =""; basket.forEach((value)=>{ if ((value)===(searchItem)){ item = `${searchItem} is Available` } }) return item; } checkBasket(amazonBasket,'books') "books is Available" 1 Like |
Re: Please I Need Solution To This Js Code by lewis512(m): 2:41pm On Aug 31, 2021 |
You declared your text outside the function which will make it "undefined", I havent test the code but thats the first error I notice 2 Likes |
Re: Please I Need Solution To This Js Code by olubantylove(m): 3:01pm On Aug 31, 2021 |
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!! 2 Likes
|
Re: Please I Need Solution To This Js Code by divine404: 3:27pm On Aug 31, 2021 |
olubantylove:The "Object.keys(obj)" method returns an array of the obj keys, and assigns the array to the basket variable. 2 Likes |
Re: Please I Need Solution To This Js Code by DDayve: 3:32pm On Aug 31, 2021 |
olubantylove: Thank you so much boss. I appreciate you so much for setting out your precious time to explain the code. corrections taken!!! I actually had it solved with a for\in but you know we learners now, i want to test if i can solve it in another way. that is the reason i first used the Object.keys to convert to an array before using the forEach() function. |
Re: Please I Need Solution To This Js Code by DDayve: 3:44pm On Aug 31, 2021 |
afuye: Thanks boss. i want it to also return Not available when the item is not available. I have used the for\in loop to achieve it just as my other boss did below this post. But i want to know if its possible with forEach. I have done everything possible since yesterday, i am not able to achieve it. Can you help me please? |
Re: Please I Need Solution To This Js Code by olubantylove(m): 3:54pm On Aug 31, 2021 |
DDayve: You're the boss sir!! You can actually solve it in another way too.. There are lot of ways to solving problems you know?. |
Re: Please I Need Solution To This Js Code by olubantylove(m): 3:57pm On Aug 31, 2021 |
divine404: Alright Chief!!! Thanks for pointing this out.. We all learn everyday you know. |
Re: Please I Need Solution To This Js Code by divine404: 4:32pm On Aug 31, 2021 |
olubantylove:Yes, boss. You're welcome!!! |
Re: Please I Need Solution To This Js Code by etoluw: 5:10pm On Aug 31, 2021 |
DDayve: you don't need a for/in loop for that just use either obj[searchitem] or (searchitem in obj) |
(1) (Reply)
Can I Learn Dapp/web3 Development Without Any Knowledge Of Coding Or Programming / I Need Help With My Coding / How To Create A Banking App: Ultimate Guide From Hands-on Experts
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 37 |