Day 9 - 15 days with Postman challenge

I get stuck in day 9, task 4.

    • Add another test script : Still under the Tests tab of the get starships request, add code to loop through the starships and determine which starship is the fastest.
  • Initialize two collection variables called fastestShip and fastestSpeed . The INITIAL values can be your first name and 0 .
  • You must cast a string to a number to compare numerical values, like speed.
  • Keep track of the fastest starship’s name and max_atmosphering_speed as collection variables, to use in the next step.

I added test script

var response = pm.response.json();
var starships = response.results;
var fastestShip = pm.collectionVariables.get("fastestShip");
var fastestSpeed = parseFloat(pm.collectionVariables.get("fastestSpeed"));

for (var i = 0; i < starships.length; i++) {
  var speed = parseFloat(starships[i].max_atmosphering_speed);
  if (speed > fastestSpeed) {
    fastestSpeed = speed;
    fastestShip = starships[i].name;

pm.collectionVariables.set("fastestShip", fastestShip);
pm.collectionVariables.set("fastestSpeed", fastestSpeed.toString());

But error There was an error in evaluating the test script: TypeError: Cannot read properties of undefined (reading ‘length’)
Could you give me a hint How I can solve it?

I can’t see anything particularly wrong with your loop (which is the only place I can see length referenced).

I’m assuming that you are getting the response back ok and that starships is an array. (Use console log to confirm).

In which case I can only suggest taking it back a step and getting the loop working, and then add the rest of the code bit by bit.

As the results is an array, you might also want to consider a forEach loop.

const response = pm.response.json();
let starships = response.results;


for (var i = 0; i < starships.length; i++) {

starships.forEach(ship => {

Shouldn’t it be parseInt rather than parseFloat. I don’t think there are any decimal places involved here.

Yes response back is an array, so how my script should looks fully?

I can’t tell you that. (Actually I can, but that would defeat the purpose of the exercise).

Get the loop working, and then start added the rest of the code back in step by step using the console log where appropriate to check variables.

As mentioned, I couldn’t really see anything wrong with your original loop, so adding the code back in step by step would be my recommendation.

1 Like