Bunnings Warehouse gift cards are a perfect gift Idea. The choice of the whole warehouse. Any occasions; birthdays, congratulations, anniversaries, house warmings, weddings.
To solve this problem, we need to reformat system requirements into a structured HTML format. The system requirements can be provided in two different formats, and our task is to handle both formats appropriately.
### Approach
The problem involves converting system requirements into a specific HTML structure. The input can be in two formats:
1. An array of objects where each object contains a single requirement (e.g., OS, Processor).
2. An array of objects where each object contains a system name and an HTML string representing the requirements.
The solution involves:
1. Detecting the format of the input.
2. Generating the appropriate HTML structure based on the detected format.
If the input is structured with "system" and "requirement" keys, each system's requirements are included as is. Otherwise, the requirements are extracted from key-value pairs and structured into an unordered list.
### Solution Code
```javascript
function reformatRequirements(input) {
let output = '';
if (input.length > 0 && 'system' in input[0]) {
for (let req of input) {
let system = req.system;
let requirementHtml = req.requirement.replace(/'/g, ''');
output += `
${system} System Requirements
MINIMUM SPECS
${requirementHtml}
`;
}
} else {
let system = input[0] ? input[0].OS || 'PC' : 'PC';
let requirements = input.flatMap(obj => {
let key = Object.keys(obj)[0];
let value = Object.values(obj)[0];
return `
${key}: ${value}`;
}).join('');
output = `
${system} System Requirements
MINIMUM SPECS
`;
}
return output;
}
```
### Explanation
1. **Input Detection**: The function first checks if the input is structured with "system" and "requirement" keys. If so, each requirement is processed individually.
2. **HTML Generation for Structured Input**: For each system, the HTML is generated by including the system name, followed by the requirement HTML string.
3. **HTML Generation for Unstructured Input**: If the input does not contain "system" keys, the function assumes all objects are requirements for a single system. The requirements are extracted from key-value pairs and structured into an unordered list.
This approach ensures that the requirements are always presented in a consistent and user-friendly format, regardless of the input structure.
分享