Day 9 - Issues with test scripts

Hello!
I don’t know how to create script.
I am stuck with logic.
How i can connect set.newrequest and defining maximum speed.
I checked a lot of data, spent hours but nothing works. Can you help me.
The latest 5th page is not working.
Also I don’t know how to define the maximum from 4 requests. I can do it only for 1 page.

Sorry in advance with my bad code and poor English.

var jsonData = JSON.parse(responseBody);
console.log("The number of starships : " + jsonData.count);

pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});

var page = pm.collectionVariables.get(“pageNumber”);
pm.collectionVariables.set(“pageNumber”, page+1);

if (jsonData.next !== null){
let fastestShipName = ‘Maksim’;
let fastestShipSpeed = 0;

pm.collectionVariables.set(‘fastestShip’, fastestShipName);
pm.collectionVariables.set(‘fastestSpeed’, String(fastestShipSpeed));

const responseJSData = pm.response.json();
const starShips = responseJSData.results;

for(const starship of starShips) {
if(Number(starship.max_atmosphering_speed) > fastestShipSpeed) {
fastestShipSpeed = Number(starship.max_atmosphering_speed);
fastestShipName = starship.name;
}
}
pm.collectionVariables.set(“fastestShip”, fastestShipName);
pm.collectionVariables.set(“fastestSpeed”, String(fastestShipSpeed));
console.log("The fastest ship name : " + pm.collectionVariables.get(“fastestShip”));
console.log("The fastest ship speed : " + pm.collectionVariables.get(“fastestSpeed”));
postman.setNextRequest(“get starships”);}
else {
pm.collectionVariables.set(“fastestShip”, fastestShipName);
pm.collectionVariables.set(“fastestSpeed”, String(fastestShipSpeed));
console.log("The fastest ship name : " + pm.collectionVariables.get(“fastestShip”));
console.log("The fastest ship speed : " + pm.collectionVariables.get(“fastestSpeed”));
postman.setNextRequest(null);
}

Hey @satellite-geologist6 , welcome to the Postman Community! :wave:

If you’re struggling with the current challenge it may mean you’re not quite ready for it, and this is completely fine. I would recommend watching this video from Valentin Despa teaching javascript in the context of Postman:

Once you’ve learned a bit more you can have another go at the challenge.

2 Likes

Just to extend on that link from @arlem

Have you gone through the Postman Galaxy 101 course yet?

The logic for most of this is covered in this course.

Postman uses JavaScript under the hood so w3Schools is a good starting place for any elements that you may struggle with.

The results should be an array, which you will need to combine with a forEach loop.

JavaScript Arrays (w3schools.com)

JavaScript Array forEach() Method (w3schools.com)

Break down your code into steps, so you can clearly see the steps and logic you need to code for (the same as how the tests are broken down in the Galaxy 101 training).

Write the steps first.

Some core logic (I’m not giving you the full answer as that would defeat the learning exercise).

// test response code
// parse response
// get current speed from collection variable.
// loop though ships setting variables as appropriate.
// if response.next is not null then setNextRequest

TLDR: Break your code into steps, and then learn how to achieve that step.

And as always, the console logs are your friend.

Last point is related to the forum. When pasting code, please use the preformatted text option in the editor as its stops all the code being aligned to the left which is nearly unreadable.

1 Like

Thank you, Arlemi!
Yeah, I didn’t expect that 9th day will be so hard in comparison to the previous tasks.

2 Likes

Thank you very much, Jones!
I am very beginner in programming.
Also 9th day is very special to the previous ones: much harder, real challenge!))
Wish you the best!
Thanks for sharing best pieces of advice.

1 Like

Fully agree with you: the 9th day is the hardest. All other are simpler. Much simple.