Skip to content

Conversation

@MalusiS
Copy link

@MalusiS MalusiS commented Aug 15, 2025

Learners, PR Template

Self checklist

  • I have committed my files one by one, on purpose, and for a reason
  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • I have tested my changes
  • My changes follow the style guide
  • My changes meet the requirements of this task

Changelist

Summary:
This PR introduces a small web app that fetches and displays a random XKCD comic with the ability to load a new one at the click of a button. The implementation uses HTML, CSS, and JavaScript with Fetch API calls to retrieve comic data from https://xkcd.now.sh.

Details of Implementation:

HTML (index.html):

  • Created page structure with a title, a comic display container, and a "Show me another comic" button.

CSS (style.css):

  • Styled the comic container, image, and button for a clean and responsive layout.

JavaScript (script.js):

  • Fetches the latest comic number to determine the range for random selection.
  • Randomly selects and retrieves a comic using the XKCD API.
  • Displays the comic number, title, and image in the DOM.
  • Includes error handling for network or API issues.
  • Added button click functionality to load a new random comic without refreshing the page.

Testing & Verification:

  1. Open index.html in a browser.
  2. Confirm that a comic loads with its number, title, and image displayed.
  3. Check the console to verify the fetched JSON data is logged.
  4. Click "Show me another comic" and confirm a different random comic appears without a page refresh.
  5. Turn off the internet connection and try clicking the button — an error message should be displayed in the DOM and an error logged in the console.
  6. Resize the browser window and confirm the image scales responsively.

Questions

Ask any questions you have for your reviewer.

@github-actions
Copy link

Your PR's title isn't in the expected format.

Please check the expected title format, and update yours to match.

Reason: Sprint part (Data Flows) doesn't match expected format (example: 'Sprint 2', without quotes)

5 similar comments
@github-actions
Copy link

Your PR's title isn't in the expected format.

Please check the expected title format, and update yours to match.

Reason: Sprint part (Data Flows) doesn't match expected format (example: 'Sprint 2', without quotes)

@github-actions
Copy link

Your PR's title isn't in the expected format.

Please check the expected title format, and update yours to match.

Reason: Sprint part (Data Flows) doesn't match expected format (example: 'Sprint 2', without quotes)

@github-actions
Copy link

Your PR's title isn't in the expected format.

Please check the expected title format, and update yours to match.

Reason: Sprint part (Data Flows) doesn't match expected format (example: 'Sprint 2', without quotes)

@github-actions
Copy link

Your PR's title isn't in the expected format.

Please check the expected title format, and update yours to match.

Reason: Sprint part (Data Flows) doesn't match expected format (example: 'Sprint 2', without quotes)

@github-actions
Copy link

Your PR's title isn't in the expected format.

Please check the expected title format, and update yours to match.

Reason: Sprint part (Data Flows) doesn't match expected format (example: 'Sprint 2', without quotes)

@MalusiS MalusiS added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Aug 15, 2025
@github-actions
Copy link

Your PR's title isn't in the expected format.

Please check the expected title format, and update yours to match.

Reason: Sprint part (Data Flows) doesn't match expected format (example: 'Sprint 2', without quotes)

@MalusiS MalusiS changed the title ZA | 25-ITP-May | Malusi Skunyana | Data Flows | Programmer Humour ZA | 25-ITP-May | Malusi Skunyana | Sprint 3 | Programmer Humour Aug 15, 2025
Comment on lines 9 to 26
const latestResponse = await fetch('https://xkcd.now.sh/?comic=latest');
if (!latestResponse.ok) {
throw new Error(`HTTP error getting latest: ${latestResponse.status}`);
}
const latestData = await latestResponse.json();
latestNum = latestData.num;
}

// Pick a random comic number between 1 and latestNum
const randomNum = Math.floor(Math.random() * latestNum) + 1;

// Fetch the random comic
const response = await fetch(`https://xkcd.now.sh/?comic=${randomNum}`);
if (!response.ok) {
throw new Error(`HTTP error getting comic: ${response.status}`);
}

const data = await response.json();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have two similar sets of code for fetching data. It might be a good idea to refactor the code into a function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for great feedback! Indeed, the fetch logic was duplicated in two places. I have now refactored it into a reusable fetchJson() helper function. This keeps the code DRY, improves readability, and ensures that any future changes to error handling or response parsing only need to be made in one place. It should also make the code easier to extend if we add more API calls later.

}

const data = await response.json();
console.log('Fetched comic data:', data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When submitting a PR, it's best practices to remove debugging code to keep the code clean.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed the console.log statement so the code is cleaner. I will make sure to keep debugging output out of future PRs. Thank you for great feedback.

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Aug 16, 2025
@MalusiS MalusiS added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Aug 16, 2025
@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Aug 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants