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 from alterObjects.
I don't understand the problem completely but it's possible you are given a function constructor, and a string greeting, 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:
alterObjects=<codeyouprovided>;// return array of two objectsconstructor2=function(){return[{},{}]}// check that each object gets the greeting alterObjects(constructor2,"Greetings!!!")[0].greeting;// should == "Greetings!!!"alterObjects(constructor2,"Greetings!!!")[1].greeting;// should == "Greetings!!!"
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:
objects.forEach(function (object) {
^
TypeError: objects.forEach is not a function
at alterObjects (/usercode/index.js:5:13)
at executeTests (/usercode/index.js:85:3)
at main (/usercode/index.js:95:13)
at Object.<anonymous> (/usercode/index.js:115:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at loader (/usr/local/lib/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/babel-register/lib/node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)```
although I'm a beginner I was trying hardly to solve this
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, because forEach is a method present on arrays, but the variable objects 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:
alterObjects=function(constructor,greeting){// assuming constructor returns an objectobject=constructor();// add greeting attributeobject.greeting=greeting;// and return itreturnobject;}
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:
alterObjects=function(constructor,greeting){// assuming constructor returns an objectobject=constructor();// take a peek at what we get, display on the consoleconsole.log(object);// add greeting attributeobject.greeting=greeting;// and return itreturnobject;}
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.
For further actions, you may consider blocking this person and/or reporting abuse
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.