Click here to Skip to main content
15,883,817 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The first parameter is an object, the second parameter will be a
string and the third parameter will be a value. Return the object
adding the second parameter as a key using the third parameter as its value.


Some reason I can only ever either set the string or key and not both and everything I find online does the same. X.X Starting to really hate Javascript Objects and value Python's classes.


Examples:

let obj = {}
setKeyInObject(obj, "apple", "yum"); // => {apple: "yum"}

let obj1 = {str: "hello"}
setKeyInObject(obj1, "num", 3); // => {str: "hello", num: 3}



function setKeyInObject(obj, string, value) {

let obj1 = {string: value}
return obj1;

}

What I have tried:

Finally figured this much out but now it fails for the second test.

function setKeyInObject(obj, string, value) {
  // Your code here


  return obj[string] = {[string]: value};
 

}
Posted
Updated 5-Oct-22 18:41pm
v5

1 solution

Yeaup two hours + just to figure this simple thing out while breezing through more complicated problems, nice.

function setKeyInObject(obj, string, value) {
  // Your code here


  return obj[string] = value;
 

}
 
Share this answer
 
v2
Comments
CHill60 6-Oct-22 5:55am    
Just a gentle warning that you are starting to look like a "reputation point hunter" - you are posting questions then posting solutions to your own questions just a few hours later, then "Accepting" your own solution as the best answer.
Site abusers such as spammers behave like this to gain sufficient reputation points to post spam in their profiles, so many members will see this behaviour from you as abuse, report it as such, and if sufficient members agree, your membership of the site will be terminated.
Take care out there.
Chris Aug2022 6-Oct-22 21:17pm    
I could careless about Reputation points.
I only post a question if I've been stuck on it for some time, I post here then take the dog for a walk and come back and see if there is an answer, otherwise I go back to the question and try to figure it out. It's not my fault that something so easy for an experienced programmer couldn't be answered in that amount of time and I ended up coming up with the answer. I "Accepted" my own solution because NO ONE answered this question, whatsoever.

Anyone that looks at my history and actually LOOKS at it will see that. It's funny because you must've been the one that downvoted the stars on the answer - I could careless however it shows EXTREME pettiness and an elitist mentality. If someone would've answered my question I wouldn't have had to spend an embarrassingly LARGE amount of time(Over two hours) trying to figure out something EXTREMELY simple.

I'm not like you, I could care less about points. I am homeless and trying to learn to code to get out of being homeless.You're just some elitist going around dogging people for asking a question, not getting an answer, then eventually figuring it out on there own because NO ONE helped.

Good job.
CHill60 7-Oct-22 3:18am    
What makes you think it was me that down voted? I offered you some advice based on years of observing behaviours on here and that makes me petty and an elitist?
Two hours is not a long time to wait, some people wait for days - there are many time zones involved. We help here in our spare time too, so waiting 8 hours for an expert to finish work would not be unreasonable.
Get over yourself and learn some patience
Chris Aug2022 7-Oct-22 6:30am    
Strawman Fallacy - A straw man fallacy occurs when someone takes another person’s argument or point, distorts it or exaggerates it in some kind of extreme way, and then attacks the extreme distortion, as if that is really the claim the first person is making. -------- This has nothing to do with my patience nor did I make it about that. Get over yourself. The Point is if I answer my own question after figuring out the answer after HOURS and I get modded out for that, well then this is 100% nepotistic, cronyist bs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900