1

I do have this kind problem below that create a new constructor and must return a string in an object. My data is an object array like this

val _List = [{name: 'name1', number: 2, 'message': 'Hello World'},{name: 'name1', number: 2, 'message': 'Good bye'}]

And the code will be executed by a test

test2 = new ProgramDevice('2');

it('should send string exactly', function() {
    expect(test2.postStringReturn).toEqual('Hello world!');
}

The solution that I created is something like below but instead of string return it returns a function

this.postStringReturn= _List.find(data => {
    if(data.number == this.number ) {
      return data.message
    }
  });

Can someone explain where I did wrong? Sorry not really good at OOP, just someone tell me where is my mistake

Update //

If I create something like this

var ProgramDevice = function (num) { // i.e. '192.168.0.1'
  this.number= num;
}

ProgramDevice.prototype = {
   postStringReturn = _List.find(data => { data.number == this.number }).message
}

How can I get the this.number in using prototype

7
  • Have you run it first before running the test? Commented Jun 18, 2020 at 14:18
  • @Kholio yes, it should return a string and to return the message the same as HelloWorld im doing find function array to track the number, unfortunately my object returns a output like this Function[anonymouse()] I suspect that it is not correctly returning the data since postStringReturn calls as an object Commented Jun 18, 2020 at 14:20
  • @Kholio the things is how can I return the message if the data was stored on the object array and can be accessed using the number provided by the constructor Commented Jun 18, 2020 at 14:21
  • find doesn't return what you return in callback, it returns the item in the array that meets the condition. IOW: find will return the array item, so it will be there you want to get the message.. eg. this.postStringReturn= _List.find(data => data.number == this.number).message; Commented Jun 18, 2020 at 14:28
  • @Keith I inserted your suggested code but it is not getting the message as it is undefined Commented Jun 18, 2020 at 14:36

1 Answer 1

1

Well, try to replace this.number (only to evaluate this situation) with the digit 2, it should then return 'hello world' and 'Good Bye'. If this is the case, you might have some reference error.

Also the test might be failing because your initial array has 2 as a value of the property number in both the elements, that will not return what the test expected. It will return the other value 'Good bye' as well, and for testing this is a fail. Got my point?

Sign up to request clarification or add additional context in comments.

3 Comments

The test good, but I think the error is from the reference, I dont know how to deal with object that getting an constructor data
Can you share the original code, I'd need to trace your reference first.
here is the link repl.it/repls/SnowValidDoom it always returns a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.