Unlock seamless travel with the Grab Rides ₱100 Gift Card! This gift card provides access to reliable ride-hailing services across Southeast Asia, ensuring safe and convenient transportation at your fingertips. Perfect for commuting, airport transfers, or exploring new destinations, Grab Rides makes urban commuting easy and efficient. Enjoy cashless payments and priority bookings while earning rewards with every ride. Gift the convenience of Grab Rides today!
To address this task, we need to transform a JSON input containing system requirements into a structured HTML format. The approach involves parsing each system's requirements, structuring the HTML accordingly, and ensuring all elements are correctly formatted.
### Approach
1. **Understand the Input Structure**: Each JSON object represents a system (e.g., Windows, Mac, Linux) with nested requirements. Each requirement is a key-value pair.
2. **HTML Structuring**: Each system's requirements are enclosed in a `div` with the class "prod-spec". This includes a header, a paragraph for "MINIMUM SPECS", and an unordered list (`ul`) where each requirement is a list item (`li`).
3. **Handling Nested Requirements**: If a requirement is an HTML string (e.g., containing `
` elements), it is parsed to extract individual key-value pairs, which are then formatted into list items.
4. **Consistency and Formatting**: Ensure all keys are consistently formatted within spans and values are placed appropriately. Special characters are handled to prevent formatting issues.
### Solution Code
```javascript
function formatSystemRequirements(inputJson) {
const parser = new DOMParser();
let output = '';
inputJson.forEach(system => {
const systemDiv = document.createElement('div');
systemDiv.className = 'prod-spec';
const heading = document.createElement('h4');
heading.textContent = `${system.system} System Requirements`;
systemDiv.appendChild(heading);
const br = document.createElement('br');
systemDiv.appendChild(br);
const minSpecs = document.createElement('p');
minSpecs.innerHTML = 'MINIMUM SPECS';
systemDiv.appendChild(minSpecs);
const ul = document.createElement('ul');
systemDiv.appendChild(ul);
let requirements = [];
if (typeof system.requirement === 'string' && system.requirement.startsWith('')) {
const doc = parser.parseFromString(system.requirement, 'text/html');
const lis = doc.querySelectorAll('ul li');
lis.forEach(li => {
const spans = li.querySelectorAll('span');
const key = spans[0]?.textContent?.trim();
const value = li.textContent.replace(spans[0]?.textContent || '', '').trim();
if (key) {
requirements.push({ key, value });
}
});
} else {
const parsed = JSON.parse(system.requirement);
Object.entries(parsed).forEach(([key, value]) => {
requirements.push({ key, value });
});
}
requirements.forEach(({ key, value }) => {
const li = document.createElement('li');
const span = document.createElement('span');
span.textContent = `${key}:`;
li.appendChild(span);
li.innerHTML += ` ${value}`;
ul.appendChild(li);
});
output += systemDiv.outerHTML;
});
return output;
}
```
### Explanation
1. **Parsing the JSON**: The input JSON is parsed and each system's requirements are processed individually.
2. **HTML Structure Creation**: For each system, a `div` with class "prod-spec" is created. It includes a header, a paragraph for specifications, and a list for each requirement.
3. **Handling Requirements**: Requirements are parsed into key-value pairs. If the requirement string is HTML, it is parsed to extract key-value pairs; otherwise, it is treated as JSON.
4. **Building HTML Elements**: For each requirement, a list item is created with a span for the key and the value appended next. These are added to the list within the system's `div`.
5. **Output Construction**: All system `div`s are concatenated into the final HTML output.
This approach ensures that system requirements are presented in a clear, structured manner, adhering to the specified format and guidelines.
-
Launch your Grab App.
-
Click on the Gifts Icon at the upper right corner.
-
Paste or enter your gift card code.
-
Click on Pick your Gift, then proceed with saving the voucher.
-
Choose a service and pick a gift breakdown.
-
Your Grab Gift voucher will then be stored in the app's My Rewards tab.
-
Note: The code is valid for 3 months!
分享