CodeNewbie Community

loading...

Alter multiple objects at once

Shaik
・1 min read

I was taken a javascript assessment freely available at educative.io.

Alt Text

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

Discussion (3)

Collapse
djuber profile image
Daniel Uber • Edited

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 = < code you provided >;

// return array of two objects
constructor2 = function() { return [{}, {}] }

// check that each object gets the greeting 
alterObjects(constructor2, "Greetings!!!")[0].greeting;  // should == "Greetings!!!"
alterObjects(constructor2, "Greetings!!!")[1].greeting;  // should == "Greetings!!!"
Enter fullscreen mode Exit fullscreen mode

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:

alterObjects = function(constructor, greeting) {
  objects = constructor();
  objects.forEach( (object) => object.greeting = greeting );
  return objects;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
shaikmdmusayyeb profile image
Shaik Author • Edited

Hey Daniel Uber , Thanks for your quick response

Here is what I'm getting in response of your suggested code,
community.codenewbie.org/remoteima...

  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
Enter fullscreen mode Exit fullscreen mode
Collapse
djuber profile image
Daniel Uber

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, 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 object
  object = constructor();

  // add greeting attribute
  object.greeting = greeting;

 // and return it
  return object;
}
Enter fullscreen mode Exit fullscreen mode

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 object
  object = constructor();

  // take a peek at what we get, display on the console
  console.log(object);

  // add greeting attribute
  object.greeting = greeting;

 // and return it
  return object;
}
Enter fullscreen mode Exit fullscreen mode

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.