I was taken a javascript assessment freely available at educative.io.
In between encountered with a test (refer image for more info)
Here in comments given that
// Add the greeting field to all objects
// created by the constructor
tried in my way like this
alterObjects = function(constructor, greeting) {
const source = {greeting : greeting};
constructor = Object.assign(source);
}
Note: New to coding, may be helpful if u explain a little bit and suggest with some resources regarding the topic
Top comments (3)
It's tough to say too much without seeing how they're testing this (how do they call this function, and test the correct behavior).
Do you need to return anything from this function for it to have the correct effect?
undefined
is a value you get in javascript when a function is called but nothing is returned, so if they're seeing undefined it's possible they're checking the return values fromalterObjects
.I don't understand the problem completely but it's possible you are given a function
constructor
, and a stringgreeting
, and you're being asked to call the constructor (to generate objects), then add a greeting to each returned object (suggesting constructor should return a collection like an Array?), and then return those modified objects.I suspect they're doing something like this in their test cases - I am also limited by what I'm seeing in the screenshot and guessing as much as you are:
If that's the case - when I call your version of
alterObjects
I do get undefined back (the function doesn't return anything, it only reassigns the constructor value passed in) and the proposed test setup would fail. If they don't have a constructor that returns a single item, the example is probably close enough to test locally in a browser or repl - my first guess might look like this:Hey Daniel Uber , Thanks for your quick response
Here is what I'm getting in response of your suggested code,
community.codenewbie.org/remoteima...
Hi Shaik,
Well, that error message is more information than we had before - what it says is that the result returned from
constructor
is not an array (and might just be a normal object like{}
). This is something we can tell, becauseforEach
is a method present on arrays, but the variableobjects
didn't have that function present when it was called. It also says the code you wrote in the function is getting called (the error happened inside your function).You could try to see if you always get just a single object back, by doing something like this:
If that's still failing - it looks from the screenshot like you have a "show console" tab available in addition to the Results - you might try to get a look at the
constructor()
's output by trying this and checking that tab afterward to get a look at what you're trying to modify:It's possible that sometimes you get an array and sometimes you get a plain object, in which case the solution might have to check whether forEach is a function and decide what to do. It's also possible that I don't know any more than you about the problem, and the right answer might be "obvious" to someone who knew javascript better.