Last active
December 7, 2023 04:29
-
-
Save jericbas/a39c3246422a4c3991d6e3514d747d1e to your computer and use it in GitHub Desktop.
Loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Certainly! Let's create more realistic test cases for each of the first seven functional methods, using scenarios related to vehicle, house, or land insurance. Each example will use an array with real-life-like data. | |
1. **forEach (Logging Vehicle Insurance IDs)**: | |
```javascript | |
const vehicleInsurances = [ | |
{ id: 'V1001', premium: 300 }, | |
{ id: 'V1002', premium: 400 }, | |
{ id: 'V1003', premium: 500 }, | |
{ id: 'V1004', premium: 600 } | |
]; | |
vehicleInsurances.forEach(insurance => { | |
console.log(`Insurance ID: ${insurance.id}, Premium: ${insurance.premium} dollars.`); | |
}); | |
``` | |
This logs the ID and premium of each vehicle insurance policy. | |
2. **map (Calculating House Insurance Premiums)**: | |
```javascript | |
const houseValues = [150000, 250000, 350000, 450000]; | |
const insurancePremiums = houseValues.map(value => value * 0.003); // 0.3% of house value | |
console.log(insurancePremiums); | |
``` | |
This creates a new array with the insurance premiums calculated as 0.3% of each house value. | |
3. **filter (Active Land Insurance Policies)**: | |
```javascript | |
const landInsurances = [ | |
{ id: 'L101', isActive: true }, | |
{ id: 'L102', isActive: false }, | |
{ id: 'L103', isActive: true }, | |
{ id: 'L104', isActive: true } | |
]; | |
const activeInsurances = landInsurances.filter(insurance => insurance.isActive); | |
console.log(activeInsurances); | |
``` | |
This filters out the land insurances that are currently active. | |
4. **reduce (Total Vehicle Insurance Premium)**: | |
```javascript | |
const vehiclePremiums = [300, 400, 500, 600]; | |
const totalPremium = vehiclePremiums.reduce((total, premium) => total + premium, 0); | |
console.log(`Total vehicle insurance premium: ${totalPremium} dollars`); | |
``` | |
This calculates the total premium for all vehicle insurances. | |
5. **find (House Insurance with Maximum Coverage)**: | |
```javascript | |
const houseInsurances = [ | |
{ id: 'H001', coverage: 'basic', value: 100000 }, | |
{ id: 'H002', coverage: 'premium', value: 200000 }, | |
{ id: 'H003', coverage: 'basic', value: 150000 }, | |
{ id: 'H004', coverage: 'premium', value: 300000 } | |
]; | |
const highestValuePremium = houseInsurances.find(insurance => insurance.coverage === 'premium' && insurance.value === Math.max(...houseInsurances.filter(ins => ins.coverage === 'premium').map(ins => ins.value))); | |
console.log(highestValuePremium); | |
``` | |
This finds the house insurance with 'premium' coverage and the highest value. | |
6. **every (Check if All Lands are Insured)**: | |
```javascript | |
const landInsurances = [ | |
{ id: 'L201', isInsured: true }, | |
{ id: 'L202', isInsured: true }, | |
{ id: 'L203', isInsured: true }, | |
{ id: 'L204', isInsured: true } | |
]; | |
const allInsured = landInsurances.every(insurance => insurance.isInsured); | |
console.log(`All lands are insured: ${allInsured}`); | |
``` | |
This checks if every piece of land is insured. | |
7. **some (Any Expired Vehicle Insurance)**: | |
```javascript | |
const vehicleInsurances = [ | |
{ id: 'V2001', isExpired: false }, | |
{ id: 'V2002', isExpired: true }, | |
{ id: 'V2003', isExpired: false }, | |
{ id: 'V2004', isExpired: false } | |
]; | |
const anyExpired = vehicleInsurances.some(insurance => insurance.isExpired); | |
console.log(`Any vehicle insurance expired: ${anyExpired}`); | |
``` | |
This checks if there is any vehicle insurance that has expired. | |
These examples are designed to mimic real-life scenarios in insurance management, demonstrating how each functional method can be applied in practical situations. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Certainly! Let's create another set of examples for the first seven functional methods in JavaScript, this time with different variable names and scenarios related to vehicle, home, and land insurance. Each example will reflect a realistic use case. | |
1. **forEach (Displaying Car Insurance Details)**: | |
```javascript | |
const carInsurances = [ | |
{ policyNumber: 'C123', premium: 350 }, | |
{ policyNumber: 'C124', premium: 450 }, | |
{ policyNumber: 'C125', premium: 550 }, | |
{ policyNumber: 'C126', premium: 650 } | |
]; | |
carInsurances.forEach(insurance => { | |
console.log(`Policy Number: ${insurance.policyNumber}, Annual Premium: ${insurance.premium} dollars.`); | |
}); | |
``` | |
This logs the policy number and annual premium for each car insurance policy. | |
2. **map (Calculating Home Insurance Fees)**: | |
```javascript | |
const propertyValues = [200000, 300000, 400000, 500000]; | |
const homeInsuranceFees = propertyValues.map(value => value * 0.004); // 0.4% of property value | |
console.log(homeInsuranceFees); | |
``` | |
This creates a new array with home insurance fees calculated as 0.4% of each property value. | |
3. **filter (Filtering Active Land Titles)**: | |
```javascript | |
const landTitles = [ | |
{ titleId: 'L301', isActive: true }, | |
{ titleId: 'L302', isActive: false }, | |
{ titleId: 'L303', isActive: true }, | |
{ titleId: 'L304', isActive: true } | |
]; | |
const activeTitles = landTitles.filter(title => title.isActive); | |
console.log(activeTitles); | |
``` | |
This filters out the land titles that are currently active. | |
4. **reduce (Sum of Car Insurance Premiums)**: | |
```javascript | |
const premiums = [350, 450, 550, 650]; | |
const totalCarInsurancePremium = premiums.reduce((total, premium) => total + premium, 0); | |
console.log(`Total car insurance premium: ${totalCarInsurancePremium} dollars`); | |
``` | |
This calculates the total premium for all car insurances. | |
5. **find (First Expensive Home Insurance)**: | |
```javascript | |
const homeInsurances = [ | |
{ policyId: 'H101', value: 250000, premium: 1000 }, | |
{ policyId: 'H102', value: 500000, premium: 2000 }, | |
{ policyId: 'H103', value: 750000, premium: 3000 }, | |
{ policyId: 'H104', value: 1000000, premium: 4000 } | |
]; | |
const expensivePolicy = homeInsurances.find(policy => policy.value >= 500000); | |
console.log(expensivePolicy); | |
``` | |
This finds the first home insurance policy with a value of $500,000 or more. | |
6. **every (All Lands Insured)**: | |
```javascript | |
const landPolicies = [ | |
{ plotNumber: 'P401', isInsured: true }, | |
{ plotNumber: 'P402', isInsured: true }, | |
{ plotNumber: 'P403', isInsured: true }, | |
{ plotNumber: 'P404', isInsured: true } | |
]; | |
const allLandsInsured = landPolicies.every(policy => policy.isInsured); | |
console.log(`All lands are insured: ${allLandsInsured}`); | |
``` | |
This checks if every land plot is insured. | |
7. **some (Any Vehicle Insurance Due for Renewal)**: | |
```javascript | |
const vehiclePolicies = [ | |
{ registration: 'V5001', isDueForRenewal: false }, | |
{ registration: 'V5002', isDueForRenewal: true }, | |
{ registration: 'V5003', isDueForRenewal: false }, | |
{ registration: 'V5004', isDueForRenewal: false } | |
]; | |
const anyDueForRenewal = vehiclePolicies.some(policy => policy.isDueForRenewal); | |
console.log(`Any vehicle insurance due for renewal: ${anyDueForRenewal}`); | |
``` | |
This checks if any vehicle insurance policy is due for renewal. | |
These examples provide a realistic depiction of how functional methods can be applied in the context of insurance for vehicles, homes, and lands. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Certainly! Let's create another set of examples for the first seven functional methods in JavaScript, this time with different variable names and scenarios related to vehicle, home, and land insurance. Each example will reflect a realistic use case. | |
1. **forEach (Displaying Car Insurance Details)**: | |
```javascript | |
const carInsurances = [ | |
{ policyNumber: 'C123', premium: 350 }, | |
{ policyNumber: 'C124', premium: 450 }, | |
{ policyNumber: 'C125', premium: 550 }, | |
{ policyNumber: 'C126', premium: 650 } | |
]; | |
carInsurances.forEach(insurance => { | |
console.log(`Policy Number: ${insurance.policyNumber}, Annual Premium: ${insurance.premium} dollars.`); | |
}); | |
``` | |
This logs the policy number and annual premium for each car insurance policy. | |
2. **map (Calculating Home Insurance Fees)**: | |
```javascript | |
const propertyValues = [200000, 300000, 400000, 500000]; | |
const homeInsuranceFees = propertyValues.map(value => value * 0.004); // 0.4% of property value | |
console.log(homeInsuranceFees); | |
``` | |
This creates a new array with home insurance fees calculated as 0.4% of each property value. | |
3. **filter (Filtering Active Land Titles)**: | |
```javascript | |
const landTitles = [ | |
{ titleId: 'L301', isActive: true }, | |
{ titleId: 'L302', isActive: false }, | |
{ titleId: 'L303', isActive: true }, | |
{ titleId: 'L304', isActive: true } | |
]; | |
const activeTitles = landTitles.filter(title => title.isActive); | |
console.log(activeTitles); | |
``` | |
This filters out the land titles that are currently active. | |
4. **reduce (Sum of Car Insurance Premiums)**: | |
```javascript | |
const premiums = [350, 450, 550, 650]; | |
const totalCarInsurancePremium = premiums.reduce((total, premium) => total + premium, 0); | |
console.log(`Total car insurance premium: ${totalCarInsurancePremium} dollars`); | |
``` | |
This calculates the total premium for all car insurances. | |
5. **find (First Expensive Home Insurance)**: | |
```javascript | |
const homeInsurances = [ | |
{ policyId: 'H101', value: 250000, premium: 1000 }, | |
{ policyId: 'H102', value: 500000, premium: 2000 }, | |
{ policyId: 'H103', value: 750000, premium: 3000 }, | |
{ policyId: 'H104', value: 1000000, premium: 4000 } | |
]; | |
const expensivePolicy = homeInsurances.find(policy => policy.value >= 500000); | |
console.log(expensivePolicy); | |
``` | |
This finds the first home insurance policy with a value of $500,000 or more. | |
6. **every (All Lands Insured)**: | |
```javascript | |
const landPolicies = [ | |
{ plotNumber: 'P401', isInsured: true }, | |
{ plotNumber: 'P402', isInsured: true }, | |
{ plotNumber: 'P403', isInsured: true }, | |
{ plotNumber: 'P404', isInsured: true } | |
]; | |
const allLandsInsured = landPolicies.every(policy => policy.isInsured); | |
console.log(`All lands are insured: ${allLandsInsured}`); | |
``` | |
This checks if every land plot is insured. | |
7. **some (Any Vehicle Insurance Due for Renewal)**: | |
```javascript | |
const vehiclePolicies = [ | |
{ registration: 'V5001', isDueForRenewal: false }, | |
{ registration: 'V5002', isDueForRenewal: true }, | |
{ registration: 'V5003', isDueForRenewal: false }, | |
{ registration: 'V5004', isDueForRenewal: false } | |
]; | |
const anyDueForRenewal = vehiclePolicies.some(policy => policy.isDueForRenewal); | |
console.log(`Any vehicle insurance due for renewal: ${anyDueForRenewal}`); | |
``` | |
This checks if any vehicle insurance policy is due for renewal. | |
These examples provide a realistic depiction of how functional methods can be applied in the context of insurance for vehicles, homes, and lands. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment