Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

austen js 01 resubmit #291

Merged
merged 10 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
18 changes: 18 additions & 0 deletions Code/Austen/js-01/v2/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Unit Converter</title>
</head>
<body>
<style>
body {
color: white;
background-color: black;
}
</style>
<script src="./unit-converter.js"></script>
</body>
</html>
35 changes: 35 additions & 0 deletions Code/Austen/js-01/v2/unit-converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const roundAccurately = (number, decimalPlaces) =>
Number(Math.round(number + "e" + decimalPlaces) + "e-" + decimalPlaces);

function convert(unitFrom, unitTo, amount, rates) {
for (let i = 0; i < rates.length; i++) {
if (unitFrom === rates[i].name) {
let unitrates = rates[i].rates;
for (let i = 0; i < unitrates.length; i++) {
if (unitTo === unitrates[i].name) {
let rate = unitrates[i].rate;
let calc = amount * rate;
return roundAccurately(calc, 3);
}
}
}
}
}
let rates = [
{
name: "feet",
rates: [
{ name: "inches", rate: 12 },
{ name: "meters", rate: 0.3048 },
],
},
{ name: "meters", rates: [{ name: "feet", rate: 1 / 0.3048 }] },
];
alert("Welcome to the converter.");
let unitFrom = prompt("Unit from:\nenter 'feet' or 'meters': ");
let amount = prompt("Enter the number of " + unitFrom + ": ");
let unitTo = prompt(
"What unit would you like to convert to?\nif you entered\n feet: enter 'inches' or 'meters'\n meters: enter 'feet'"
);
let output = convert(unitFrom, unitTo, amount, rates);
alert(amount + " " + unitFrom + " = " + output + " " + unitTo);
51 changes: 51 additions & 0 deletions Code/Austen/js-01/v3/converter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function convert(amount, from, to){
let rates = {
feet: 1,
meters: 0.3048,
miles: 1 / 5280,
kilometers: 0.0003048
};
const round = (number, decimalPlaces) =>
Number(Math.round(number + "e" + decimalPlaces) + "e-" + decimalPlaces);
amount = parseInt(amount)
let feet = amount / rates[from]
let conversion = feet * rates[to]
if (conversion > 99){
conversion = round(conversion, 0)}
if (conversion > 10){
conversion = round(conversion, 1)}
if (conversion > 1){
conversion = round(conversion, 2)}
if (conversion < 1){
conversion = round(conversion, 4)}
return `${conversion} ${to}`
}
function format(array){
let string = ''
let l = array.length
for (let i = 0; i < l; i++){
if (i === l-1){
string += `and ${array[i]}.`
}
else {
string += `${array[i]}, `
}
}
return string
}



let options = ['feet', 'miles', 'meters', 'kilometers' ]
let string = format(options)
let from = prompt(`Welcome to the distance converter.\n available units: ${string}\nEnter the starting unit:`);

let option = options.indexOf(from)
options.splice(option, 1)
string = format(options)
let amount = prompt(`How many ${from}: `);

let to = prompt(`Now what unit would you like to convert ${from} to?\n available units: ${string}`);

let conversion = convert(amount, from, to)
document.getElementById('target').innerHTML = conversion
20 changes: 20 additions & 0 deletions Code/Austen/js-01/v3/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Unit Converter</title>
</head>
<body>
<style>
body {
color: white;
background-color: black;
text-align: center;
}
</style>
<h1 id="target"></h1>
<script src="./converter.js"></script>
</body>
</html>