30 Days Developer Postman - Day 13

Guys, can anyone help me here please? Collection link is https://www.getpostman.com/collections/d3fec486cdc75d28e0a3

I try to find how many species included in the first page of results have an average height greater than 100, but it returns me always 15 items. My code below

pm.test("Response body contains", () => {
    let result = jsonData.results.find(a => a.average_height > 100)
    var count = Object.values(result).length;
    console.log(count)
    pm.collectionVariables.set("talls", count);

});

Hey @Sergey_K - awesome that you’re doing the challenge! Could you explain more about where you’re getting stuck? I think your collection link is still in a private or team workspace, so I’m unable to see it.

Edit: fixing punctuation

Oh, sorry, Just a second

I checked the visibility of my collection and it is public, I also updated the description.

1 Like

Hey @Sergey_K

Just out of interest, would this give you the expected result?

pm.test("Response body contains", () => {
    let result = jsonData.results.filter(a => a.average_height > 100)
    console.log(result.length)
    pm.collectionVariables.set("talls", result.length);
});
3 Likes

@Sergey_K JavaScript find()method returns the first value that matches your criteria. filter() as @dannydainton suggests will return all values that satisfy your criteria. I think your current code is counting the properties of your first (and only) result.

You can verify this with console.log(result) in your second test.

2 Likes

Victory! many thanks to @dannydainton and @joyce for your help and clarification :+1:

1 Like

Can anyone help
Assertion error while submitting the collection:

Tests and Scripts added correctly | AssertionError: check collection variable value: expected NaN to equal 7

Testscripts:



@bpricilla @neilstudd can any one help me on this

Hello @hameethapostman, you tried to print the variable “count”, Did you checked the value? There’s some problem in the value of the variable “count”.

As per the suggestion above by Danny, you can just assign the length of the result to the variable count and set it as collection variable.

Already you are filtering only the “average_height” key in result variable and there’s no need to use Object.values() method here :slightly_smiling_face:

When I set the collection variable to the number of species taller than 100 it does not persist the data to both field ( INITIAL VALUE and CURRENT VALUE).
pm.test(“Average height greater than 100”, () => {
let tall_height = jsonData.results.filter(a => a.average_height > 100)
console.log(tall_height.length)
pm.collectionVariables.set(“talls”, tall_height.length);
});

Hi @ihoss,

When we set a variable using script, it only updates the current value. To set the current value as initial value you may need to click persist all.

1 Like

Thank you for your prompt response!