Skip to content

Instantly share code, notes, and snippets.

@frankhn
Created January 23, 2023 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankhn/2e38378b639bdad2491c4309359387c4 to your computer and use it in GitHub Desktop.
Save frankhn/2e38378b639bdad2491c4309359387c4 to your computer and use it in GitHub Desktop.
// Segments { length, type: {urban, high-way}}
/**
*
*[
{
distance: Number,
type: urban | high-way
}
]
urban : 9 / 100
*/
const CalculateFuelBurned = (segments) => {
const consumptionInUrban = 9 / 100;
const consumptionInEnRoute = 7 / 100;
let consumption = 0; // in liters
segments.forEach(el => {
el.type === 'urban'
? consumption += el.distance * consumptionInUrban
: consumption += el.distance * consumptionInEnRoute
});
return consumption;
}
const segments = [
{ distance: 75, type: 'urban' },
{ distance: 50, type: 'en-route' },
{ distance: 25, type: 'urban' }
]
console.log(CalculateFuelBurned(segments), " liters");
// Arrow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment