diff --git a/Code/Austen/js-01/index.html b/Code/Austen/js-01/v1/index.html similarity index 100% rename from Code/Austen/js-01/index.html rename to Code/Austen/js-01/v1/index.html diff --git a/Code/Austen/js-01/unit-converter.js b/Code/Austen/js-01/v1/unit-converter.js similarity index 100% rename from Code/Austen/js-01/unit-converter.js rename to Code/Austen/js-01/v1/unit-converter.js diff --git a/Code/Austen/js-01/v2/index.html b/Code/Austen/js-01/v2/index.html new file mode 100644 index 00000000..c2eefb1d --- /dev/null +++ b/Code/Austen/js-01/v2/index.html @@ -0,0 +1,18 @@ + + + + + + + Unit Converter + + + + + + diff --git a/Code/Austen/js-01/v2/unit-converter.js b/Code/Austen/js-01/v2/unit-converter.js new file mode 100644 index 00000000..627e147e --- /dev/null +++ b/Code/Austen/js-01/v2/unit-converter.js @@ -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); diff --git a/Code/Austen/js-01/v3/converter.js b/Code/Austen/js-01/v3/converter.js new file mode 100644 index 00000000..1a4f2e20 --- /dev/null +++ b/Code/Austen/js-01/v3/converter.js @@ -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 diff --git a/Code/Austen/js-01/v3/index.html b/Code/Austen/js-01/v3/index.html new file mode 100644 index 00000000..2910faea --- /dev/null +++ b/Code/Austen/js-01/v3/index.html @@ -0,0 +1,20 @@ + + + + + + + Unit Converter + + + +

+ + +