Day 1 Part 2
Took a lovely lunch break after the first quiz in the Flatiron SE program and now it's time to dive into Functions!
After a great lecture on some of the basics on functions and hoisting it is time for the first lab. The run down was to build 4 function to help small shipping company calculate distances from HQ and the destination based on city blocks. HQ was Positioned on 42 steet and each block was set at 264 feet. I guess this city on has one road running north to south. Here is Below you can see how I solved this
```const hq = 42;
function distanceFromHqInBlocks(street){
return Math.abs(hq - street);
}
function distanceFromHqInFeet(street){
return Math.abs(hq - street) * 264;
}
function distanceTravelledInFeet(start, destination){
return Math.abs(start - destination) * 264;
}
function calculatesFarePrice(start, destination){
const distance = distanceTravelledInFeet(start, destination);
if (distance < 400){
return 0
}else if (distance>= 400 && distance< 2000 ){
return .02 * (distance - 400);
}else if (distance >= 2000 && distance < 2500){
return 25
}else{
return cannot travel that far
;
}
}
Obviously there are a few different ways to solve this but using Math.abs really helps minimize the lines of code needed for each function.
Reading this in Blog form has made me realize that I need to be better about commenting in my code so it is easier to understand.
Thanks for joining me! See you next time!
Top comments (0)