1

In my code, I want the function wrapInObject to take two arguments, and encapsulate them into an object that is returned.

My code:

let FirstObject = {
    k: 'FirstObject'
}
let SecondObject = {
    l: 'SecondObject'
}

let wrapInObject = function(FirstObject, SecondObject){
    return [FirstObject, SecondObject]

}

wrapInObject('FirstObject', 'SecondObject') ska shall return an object like { k: 'FirstObject', l: 'SecondObject' }.

But I get returned some errors:

index.js
    ✓ exists
    ✓ is valid JavaScript
    ✓ defines wrapInObject of the type Function
    1) defines wrapInObject such that it returns an object when called with the arguments 'FirstObject' and 'SecondObject'
    2) defines wrapInObject such that it returns an object with the same content as { k: 'FirstObject', l: 'SecondObject' } when called with the arguments 'FirstObject' and 'SecondObject'
    3) defines wrapInObject such that it returns the correct value when called with some other number arguments

What's wrong with my code?

  • *"defines wrapInObject such that it returns an object when called with the arguments 'FirstObject' and 'SecondObject'"* Whatever course or tutorial this is, frankly it doesn't seem very good. The description of the problem as quoted in your question is vague, and the list of errors seems to refer to strings as objects. – T.J. Crowder May 28 '20 at 08:05
  • 1
    @Andreas - I don't think so, but see my comment above, the whole assignment is vague and poorly-worded. – T.J. Crowder May 28 '20 at 08:06
  • @Andreas - But I should have left it closed, even though as a duplicate might not have been correct. A) It might have been, and B) The question shouldn't be open anyway. My bad. :-) – T.J. Crowder May 28 '20 at 08:09

4 Answers4

1

Looks like you want to merge these objects.

let wrapInObject = function(FirstObject, SecondObject){
    return {...FirstObject, ...SecondObject}
}

As for the third test case, you cold do

let wrapInObject = function(...objects){
    return objects.reduce((acc, object) => {...acc, ...object}, {})
}
Hobroker
  • 332
  • 4
  • 10
  • I don't think this is about merging objects. Note that the arguments are described as *strings*. – T.J. Crowder May 28 '20 at 08:03
  • @T.J.Crowder looking at his test cases I can't think of another solution – Hobroker May 28 '20 at 08:27
  • Using the first snippet, only the last two errors remain. Could you elaborate and explain the second snippet? –  May 28 '20 at 09:07
  • @left4read123 the second snippet accepts any number of arguments, assuming they are objects, and then merges all of them into a single object – Hobroker May 28 '20 at 09:48
  • @left4read123 could you post the inputs and outputs of the test cases too? – Hobroker May 28 '20 at 09:49
0

The assignment as quoted in your question seems vague and poorly-worded, and the errors you describe seem to refer to strings as objects. That makes it difficult to help (and presumably difficult to do the assignment!).

Primarily, the problem is you're returning an array, but the assignment asks you to return an object with the properties k and l. Instead of:

return [FirstObject, SecondObject]; // <===== Incorrect

return an object with the requested properties (k and l) with k set to FirstObject and l set to SecondObject. I'm not writing the code that will do that because it seems to me that whoever set you this assignment wants you to write that code.

A secondary problem is that you're defining two objects you don't need (the ones at the top of your code).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • `Primarily, the problem is you're returning an array, but the assignment asks you to return an object` Is it passible they mean `Array` or 'object' by 'object' since in JavaScript, an array is object too? – ABGR May 28 '20 at 08:13
  • 1
    @RahulDwivedi - You've left off an important part of my sentence: *"...but the assignment asks you to return an object **with the properties `k` and `l`**"* ;-) But given the apparent level of the assignment, I doubt they mean "array" when they say "object." It's true arrays are objects, but that's something you usually cover later on. – T.J. Crowder May 28 '20 at 08:16
  • `with the properties k and l"` ...ah, got that! "Didn't miss that out deliberately though" :D – ABGR May 28 '20 at 08:25
0

The problem is you're passing strings instead of object in the method this. Fixed that.

let FirstObject = {
    k: 'FirstObject'
}
let SecondObject = {
    l: 'SecondObject'
}

let wrapInObject = function(FirstObject, SecondObject){
    return [FirstObject, SecondObject]

}
console.log(wrapInObject(FirstObject, SecondObject))
ABGR
  • 4,631
  • 4
  • 27
  • 49
0
let FirstObject = {
k: 'FirstObject'    
}
let SecondObject = {
l: 'SecondObject'
}

let wrapInObject = function(FirstObject, SecondObject){

    return {FirstObject, SecondObject}
    }

Returned only the last two errors:

defines wrapInObject such that it returns an object with the same content as { k: 'FirstObject', l: 'SecondObject' } when called with the arguments 'FirstObject' and 'SecondObject'

defines wrapInObject such that it returns the correct value when called with some other number arguments