Manage your digital spending effortlessly with the Rewarble MasterCard $300 Gift Card! This prepaid Mastercard allows you to make payments across multiple platforms with ease. Whether you choose to reload it or use it once, this card offers the same safety and flexibility as any other Mastercard gift card, without the need for a credit card. Perfect for gifting, the Rewarble card ensures a seamless shopping experience.
To solve this problem, we need to reformat the given HTML based on the provided examples. The goal is to take an input JSON array of objects representing system requirements and convert it into a structured HTML format. The structured HTML should be clean and follow the specified format without any additional text or markdown.
### Approach
1. **Parse the Input:** The input is a JSON array of objects. Each object has keys representing different system requirements (like OS, Processor, etc.) and values representing their descriptions.
2. **Identify the System:** Each object may include a "system" key indicating whether it's for Windows, Mac, Linux, or a general PC system.
3. **Construct the HTML Structure:** For each system, create a div with a class "prod-spec". Inside this div, add an h4 heading indicating the system, a paragraph with "MINIMUM SPECS" in bold, and an unordered list where each list item contains a span with the key and the corresponding value.
4. **Handle Requirement HTML:** If the requirement is provided as an HTML string, parse this string to extract each list item. For each item, extract the key and value, then structure them into the required list items with spans.
5. **Output the HTML:** Ensure the output is clean HTML without any extra text or markdown. Avoid wrapping the HTML in any triple backticks or similar syntax.
### Solution Code
```javascript
function reformatHtml(input) {
let html = '';
input.forEach(item => {
// Determine the system or default to PC
const system = item.system || 'PC';
// Set the heading
const heading = `${system} System Requirements`;
// Extract requirements
let requirements = [];
if (item.requirement && typeof item.requirement === 'string') {
// Parse the requirement HTML string
const parser = new DOMParser();
const doc = parser.parseFromString(item.requirement, 'text/html');
const lis = doc.querySelectorAll('li');
requirements = Array.from(lis).map(li => {
const strong = li.querySelector('strong');
return {
key: strong.textContent.replace(/[\*\:]/g, '').trim() + ':',
value: li.textContent.replace(strong.textContent, '').trim()
};
});
} else {
// Direct mapping
requirements = Object.entries(item).map(([key, value]) => ({
key: key + ':',
value: value
}));
}
// Construct HTML
html += `
`;
html += `
${heading}
`;
html += `
`;
html += `
MINIMUM SPECS
`;
html += `
`;
requirements.forEach(req => {
html += `- `;
html += `${req.key}`;
html += req.value;
html += `
`;
});
html += `
`;
html += `
`;
html += `
`;
});
// Remove the last unnecessary
html = html.replace(/
$/g, '');
return html;
}
```
### Explanation
- **Parsing Input:** The function processes each object in the input array. If the object contains a "system" key, it uses that value; otherwise, it defaults to "PC".
- **Constructing HTML:** For each system, it creates a div with a heading, a paragraph for "MINIMUM SPECS", and a list of requirements. Each requirement is structured with a span for the key and the corresponding value.
- **Handling HTML Strings:** If the requirement is provided as an HTML string, it parses this string to extract each list item, ensuring that the extracted key and value are correctly formatted in the output.
- **Output:** The resulting HTML is a concatenation of these structured divs, each representing the system requirements for the specified system.
This approach ensures that the system requirements are presented in a clear, structured format, adhering to the specified guidelines and examples.
-
Purchase a Rewarble Mastercard voucher.
-
Visit the Rewarble redemption site at www.rewarble.com/redeem.
-
Enter and redeem your 16-digit Rewarble voucher. A virtual Mastercard will be created for you on Rewarble.
-
Use the card number, CVV, and expiration date provided to complete your transaction on any site accepting Mastercard.
Soyez le premier à donner votre avis sur ce produit !
PARTAGEZ