Different Approach : Simple Tip Calculator
up vote
1
down vote
favorite
Is it possible to assign the values of the different service levels in the tip() function under the service variable as I've started to do below? I know the alternative and likely preferred solution is to assign these values to the option values but would like to know if it can be done with if statements too.
function tip()
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
if service = "Good"
document.getElementById = ("service").innerHTML = .3;
document.getElementById("result").innerHTML = (tipp * service) / split;
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill">
<br><br> How was the Service <br><br>
<select id="service">
<option value="Good">Good</option>
<option value="Bad">Bad</option>
<option value="ok">ok</option>
</select>
<br><br> How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
javascript html
add a comment |
up vote
1
down vote
favorite
Is it possible to assign the values of the different service levels in the tip() function under the service variable as I've started to do below? I know the alternative and likely preferred solution is to assign these values to the option values but would like to know if it can be done with if statements too.
function tip()
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
if service = "Good"
document.getElementById = ("service").innerHTML = .3;
document.getElementById("result").innerHTML = (tipp * service) / split;
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill">
<br><br> How was the Service <br><br>
<select id="service">
<option value="Good">Good</option>
<option value="Bad">Bad</option>
<option value="ok">ok</option>
</select>
<br><br> How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
javascript html
WithinnerHTML=
on aselect
element you should only defineoption
elements. Anything else is not valid HTML, even if it is just.3
. Such text in adiv
oroption
is fine, just not directly in aselect
. What exactly is the problem?
– trincot
Nov 10 at 22:37
Thanks that makes sense. There is no problem. I can get the calc to work fine. I'm trying to learning HTML/JS and I think learning is best done by exploring all potential avenues by which problems might be solved, not just the most obvious ones.
– vicarus99
Nov 10 at 23:12
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Is it possible to assign the values of the different service levels in the tip() function under the service variable as I've started to do below? I know the alternative and likely preferred solution is to assign these values to the option values but would like to know if it can be done with if statements too.
function tip()
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
if service = "Good"
document.getElementById = ("service").innerHTML = .3;
document.getElementById("result").innerHTML = (tipp * service) / split;
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill">
<br><br> How was the Service <br><br>
<select id="service">
<option value="Good">Good</option>
<option value="Bad">Bad</option>
<option value="ok">ok</option>
</select>
<br><br> How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
javascript html
Is it possible to assign the values of the different service levels in the tip() function under the service variable as I've started to do below? I know the alternative and likely preferred solution is to assign these values to the option values but would like to know if it can be done with if statements too.
function tip()
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
if service = "Good"
document.getElementById = ("service").innerHTML = .3;
document.getElementById("result").innerHTML = (tipp * service) / split;
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill">
<br><br> How was the Service <br><br>
<select id="service">
<option value="Good">Good</option>
<option value="Bad">Bad</option>
<option value="ok">ok</option>
</select>
<br><br> How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
function tip()
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
if service = "Good"
document.getElementById = ("service").innerHTML = .3;
document.getElementById("result").innerHTML = (tipp * service) / split;
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill">
<br><br> How was the Service <br><br>
<select id="service">
<option value="Good">Good</option>
<option value="Bad">Bad</option>
<option value="ok">ok</option>
</select>
<br><br> How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
function tip()
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
if service = "Good"
document.getElementById = ("service").innerHTML = .3;
document.getElementById("result").innerHTML = (tipp * service) / split;
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill">
<br><br> How was the Service <br><br>
<select id="service">
<option value="Good">Good</option>
<option value="Bad">Bad</option>
<option value="ok">ok</option>
</select>
<br><br> How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
javascript html
javascript html
edited Nov 10 at 22:37
ksav
3,63221227
3,63221227
asked Nov 10 at 22:25
vicarus99
243
243
WithinnerHTML=
on aselect
element you should only defineoption
elements. Anything else is not valid HTML, even if it is just.3
. Such text in adiv
oroption
is fine, just not directly in aselect
. What exactly is the problem?
– trincot
Nov 10 at 22:37
Thanks that makes sense. There is no problem. I can get the calc to work fine. I'm trying to learning HTML/JS and I think learning is best done by exploring all potential avenues by which problems might be solved, not just the most obvious ones.
– vicarus99
Nov 10 at 23:12
add a comment |
WithinnerHTML=
on aselect
element you should only defineoption
elements. Anything else is not valid HTML, even if it is just.3
. Such text in adiv
oroption
is fine, just not directly in aselect
. What exactly is the problem?
– trincot
Nov 10 at 22:37
Thanks that makes sense. There is no problem. I can get the calc to work fine. I'm trying to learning HTML/JS and I think learning is best done by exploring all potential avenues by which problems might be solved, not just the most obvious ones.
– vicarus99
Nov 10 at 23:12
With
innerHTML=
on a select
element you should only define option
elements. Anything else is not valid HTML, even if it is just .3
. Such text in a div
or option
is fine, just not directly in a select
. What exactly is the problem?– trincot
Nov 10 at 22:37
With
innerHTML=
on a select
element you should only define option
elements. Anything else is not valid HTML, even if it is just .3
. Such text in a div
or option
is fine, just not directly in a select
. What exactly is the problem?– trincot
Nov 10 at 22:37
Thanks that makes sense. There is no problem. I can get the calc to work fine. I'm trying to learning HTML/JS and I think learning is best done by exploring all potential avenues by which problems might be solved, not just the most obvious ones.
– vicarus99
Nov 10 at 23:12
Thanks that makes sense. There is no problem. I can get the calc to work fine. I'm trying to learning HTML/JS and I think learning is best done by exploring all potential avenues by which problems might be solved, not just the most obvious ones.
– vicarus99
Nov 10 at 23:12
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
You can use an object to map the value against the service grade. Note: you'll need to coerce your input values from strings to numbers before you do any calculation on them.
// cache all the needed elements
const billTotal = document.querySelector('#bill');
const splitValue = document.querySelector('#split');
const service = document.querySelector('#service');
const result = document.querySelector('#result');
const button = document.querySelector('button');
// use a event listener instead of an inline function
button.addEventListener('click', calculate, false);
// map the values to service terms
const serviceMap =
good: 0.3,
ok: 0.25,
bad: 0.2
function calculate()
// get the input values
const bill = Number(billTotal.value);
const split = Number(splitValue.value);
// get the selected service grade
const serviceGrade = service.options[service.selectedIndex].value;
// get the service value from the object using the grade as the key
const serviceValue = serviceMap[serviceGrade];
// perform the calculation
const total = ((bill * serviceValue) / split).toFixed(2);
// output the result with `textContent`
result.textContent = `Total $total (($bill * $serviceValue) / $split)`;
input, button, div
display: block;
margin-bottom: 0.5em;
Bill <input id="bill" type="number" />
Split <input id="split" type="number" />
Service <select id="service">
<option value="good">Good</option>
<option value="ok">Bad</option>
<option value="bad">ok</option>
</select>
<button type="calculate">Calculate</button>
<div id="result"></div>
add a comment |
up vote
0
down vote
You can use an associative array. Your tip()
function could be like this.
function tip()
let service_values =
"Good": 0.3,
"ok": 0.25,
"Bad": 0.2
;
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service_label = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
let service = service_values[service_label];
document.getElementById("result").innerHTML= (tipp*service)/split;
EDIT: Check the code snippet below.
<!DOCTYPE html>
<html>
<h3> Tip Calculator <h3>
<script>
function tip()
let service_values =
"Good": 0.3,
"ok": 0.25,
"Bad": 0.2
;
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service_label = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
let service = service_values[service_label];
document.getElementById("result").innerHTML= (tipp*service)/split;
</script>
<body>
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill" value="10">
<br><br>
How was the Service <br><br>
<select id="service">
<option value ="Good">Good</option>
<option value ="Bad">Bad</option>
<option value ="ok">ok</option>
</select>
<br><br>
How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit" value="1">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
</body>
</html>
Something might be missing - the output when you try to CALCULATE is NaN. I get that you're trying to get the associated value of the inputted service level from the services value array with let service = service_values[service_label]; but is that just returning the actual string not the number?
– vicarus99
Nov 10 at 23:33
In javascript its called an object literal and not an associative array (which is a PHP term for the monstrosity that is the do everything arrays in PHP).
– max
Nov 10 at 23:50
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can use an object to map the value against the service grade. Note: you'll need to coerce your input values from strings to numbers before you do any calculation on them.
// cache all the needed elements
const billTotal = document.querySelector('#bill');
const splitValue = document.querySelector('#split');
const service = document.querySelector('#service');
const result = document.querySelector('#result');
const button = document.querySelector('button');
// use a event listener instead of an inline function
button.addEventListener('click', calculate, false);
// map the values to service terms
const serviceMap =
good: 0.3,
ok: 0.25,
bad: 0.2
function calculate()
// get the input values
const bill = Number(billTotal.value);
const split = Number(splitValue.value);
// get the selected service grade
const serviceGrade = service.options[service.selectedIndex].value;
// get the service value from the object using the grade as the key
const serviceValue = serviceMap[serviceGrade];
// perform the calculation
const total = ((bill * serviceValue) / split).toFixed(2);
// output the result with `textContent`
result.textContent = `Total $total (($bill * $serviceValue) / $split)`;
input, button, div
display: block;
margin-bottom: 0.5em;
Bill <input id="bill" type="number" />
Split <input id="split" type="number" />
Service <select id="service">
<option value="good">Good</option>
<option value="ok">Bad</option>
<option value="bad">ok</option>
</select>
<button type="calculate">Calculate</button>
<div id="result"></div>
add a comment |
up vote
0
down vote
You can use an object to map the value against the service grade. Note: you'll need to coerce your input values from strings to numbers before you do any calculation on them.
// cache all the needed elements
const billTotal = document.querySelector('#bill');
const splitValue = document.querySelector('#split');
const service = document.querySelector('#service');
const result = document.querySelector('#result');
const button = document.querySelector('button');
// use a event listener instead of an inline function
button.addEventListener('click', calculate, false);
// map the values to service terms
const serviceMap =
good: 0.3,
ok: 0.25,
bad: 0.2
function calculate()
// get the input values
const bill = Number(billTotal.value);
const split = Number(splitValue.value);
// get the selected service grade
const serviceGrade = service.options[service.selectedIndex].value;
// get the service value from the object using the grade as the key
const serviceValue = serviceMap[serviceGrade];
// perform the calculation
const total = ((bill * serviceValue) / split).toFixed(2);
// output the result with `textContent`
result.textContent = `Total $total (($bill * $serviceValue) / $split)`;
input, button, div
display: block;
margin-bottom: 0.5em;
Bill <input id="bill" type="number" />
Split <input id="split" type="number" />
Service <select id="service">
<option value="good">Good</option>
<option value="ok">Bad</option>
<option value="bad">ok</option>
</select>
<button type="calculate">Calculate</button>
<div id="result"></div>
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use an object to map the value against the service grade. Note: you'll need to coerce your input values from strings to numbers before you do any calculation on them.
// cache all the needed elements
const billTotal = document.querySelector('#bill');
const splitValue = document.querySelector('#split');
const service = document.querySelector('#service');
const result = document.querySelector('#result');
const button = document.querySelector('button');
// use a event listener instead of an inline function
button.addEventListener('click', calculate, false);
// map the values to service terms
const serviceMap =
good: 0.3,
ok: 0.25,
bad: 0.2
function calculate()
// get the input values
const bill = Number(billTotal.value);
const split = Number(splitValue.value);
// get the selected service grade
const serviceGrade = service.options[service.selectedIndex].value;
// get the service value from the object using the grade as the key
const serviceValue = serviceMap[serviceGrade];
// perform the calculation
const total = ((bill * serviceValue) / split).toFixed(2);
// output the result with `textContent`
result.textContent = `Total $total (($bill * $serviceValue) / $split)`;
input, button, div
display: block;
margin-bottom: 0.5em;
Bill <input id="bill" type="number" />
Split <input id="split" type="number" />
Service <select id="service">
<option value="good">Good</option>
<option value="ok">Bad</option>
<option value="bad">ok</option>
</select>
<button type="calculate">Calculate</button>
<div id="result"></div>
You can use an object to map the value against the service grade. Note: you'll need to coerce your input values from strings to numbers before you do any calculation on them.
// cache all the needed elements
const billTotal = document.querySelector('#bill');
const splitValue = document.querySelector('#split');
const service = document.querySelector('#service');
const result = document.querySelector('#result');
const button = document.querySelector('button');
// use a event listener instead of an inline function
button.addEventListener('click', calculate, false);
// map the values to service terms
const serviceMap =
good: 0.3,
ok: 0.25,
bad: 0.2
function calculate()
// get the input values
const bill = Number(billTotal.value);
const split = Number(splitValue.value);
// get the selected service grade
const serviceGrade = service.options[service.selectedIndex].value;
// get the service value from the object using the grade as the key
const serviceValue = serviceMap[serviceGrade];
// perform the calculation
const total = ((bill * serviceValue) / split).toFixed(2);
// output the result with `textContent`
result.textContent = `Total $total (($bill * $serviceValue) / $split)`;
input, button, div
display: block;
margin-bottom: 0.5em;
Bill <input id="bill" type="number" />
Split <input id="split" type="number" />
Service <select id="service">
<option value="good">Good</option>
<option value="ok">Bad</option>
<option value="bad">ok</option>
</select>
<button type="calculate">Calculate</button>
<div id="result"></div>
// cache all the needed elements
const billTotal = document.querySelector('#bill');
const splitValue = document.querySelector('#split');
const service = document.querySelector('#service');
const result = document.querySelector('#result');
const button = document.querySelector('button');
// use a event listener instead of an inline function
button.addEventListener('click', calculate, false);
// map the values to service terms
const serviceMap =
good: 0.3,
ok: 0.25,
bad: 0.2
function calculate()
// get the input values
const bill = Number(billTotal.value);
const split = Number(splitValue.value);
// get the selected service grade
const serviceGrade = service.options[service.selectedIndex].value;
// get the service value from the object using the grade as the key
const serviceValue = serviceMap[serviceGrade];
// perform the calculation
const total = ((bill * serviceValue) / split).toFixed(2);
// output the result with `textContent`
result.textContent = `Total $total (($bill * $serviceValue) / $split)`;
input, button, div
display: block;
margin-bottom: 0.5em;
Bill <input id="bill" type="number" />
Split <input id="split" type="number" />
Service <select id="service">
<option value="good">Good</option>
<option value="ok">Bad</option>
<option value="bad">ok</option>
</select>
<button type="calculate">Calculate</button>
<div id="result"></div>
// cache all the needed elements
const billTotal = document.querySelector('#bill');
const splitValue = document.querySelector('#split');
const service = document.querySelector('#service');
const result = document.querySelector('#result');
const button = document.querySelector('button');
// use a event listener instead of an inline function
button.addEventListener('click', calculate, false);
// map the values to service terms
const serviceMap =
good: 0.3,
ok: 0.25,
bad: 0.2
function calculate()
// get the input values
const bill = Number(billTotal.value);
const split = Number(splitValue.value);
// get the selected service grade
const serviceGrade = service.options[service.selectedIndex].value;
// get the service value from the object using the grade as the key
const serviceValue = serviceMap[serviceGrade];
// perform the calculation
const total = ((bill * serviceValue) / split).toFixed(2);
// output the result with `textContent`
result.textContent = `Total $total (($bill * $serviceValue) / $split)`;
input, button, div
display: block;
margin-bottom: 0.5em;
Bill <input id="bill" type="number" />
Split <input id="split" type="number" />
Service <select id="service">
<option value="good">Good</option>
<option value="ok">Bad</option>
<option value="bad">ok</option>
</select>
<button type="calculate">Calculate</button>
<div id="result"></div>
answered Nov 10 at 23:10
Andy
27.2k63159
27.2k63159
add a comment |
add a comment |
up vote
0
down vote
You can use an associative array. Your tip()
function could be like this.
function tip()
let service_values =
"Good": 0.3,
"ok": 0.25,
"Bad": 0.2
;
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service_label = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
let service = service_values[service_label];
document.getElementById("result").innerHTML= (tipp*service)/split;
EDIT: Check the code snippet below.
<!DOCTYPE html>
<html>
<h3> Tip Calculator <h3>
<script>
function tip()
let service_values =
"Good": 0.3,
"ok": 0.25,
"Bad": 0.2
;
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service_label = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
let service = service_values[service_label];
document.getElementById("result").innerHTML= (tipp*service)/split;
</script>
<body>
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill" value="10">
<br><br>
How was the Service <br><br>
<select id="service">
<option value ="Good">Good</option>
<option value ="Bad">Bad</option>
<option value ="ok">ok</option>
</select>
<br><br>
How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit" value="1">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
</body>
</html>
Something might be missing - the output when you try to CALCULATE is NaN. I get that you're trying to get the associated value of the inputted service level from the services value array with let service = service_values[service_label]; but is that just returning the actual string not the number?
– vicarus99
Nov 10 at 23:33
In javascript its called an object literal and not an associative array (which is a PHP term for the monstrosity that is the do everything arrays in PHP).
– max
Nov 10 at 23:50
add a comment |
up vote
0
down vote
You can use an associative array. Your tip()
function could be like this.
function tip()
let service_values =
"Good": 0.3,
"ok": 0.25,
"Bad": 0.2
;
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service_label = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
let service = service_values[service_label];
document.getElementById("result").innerHTML= (tipp*service)/split;
EDIT: Check the code snippet below.
<!DOCTYPE html>
<html>
<h3> Tip Calculator <h3>
<script>
function tip()
let service_values =
"Good": 0.3,
"ok": 0.25,
"Bad": 0.2
;
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service_label = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
let service = service_values[service_label];
document.getElementById("result").innerHTML= (tipp*service)/split;
</script>
<body>
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill" value="10">
<br><br>
How was the Service <br><br>
<select id="service">
<option value ="Good">Good</option>
<option value ="Bad">Bad</option>
<option value ="ok">ok</option>
</select>
<br><br>
How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit" value="1">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
</body>
</html>
Something might be missing - the output when you try to CALCULATE is NaN. I get that you're trying to get the associated value of the inputted service level from the services value array with let service = service_values[service_label]; but is that just returning the actual string not the number?
– vicarus99
Nov 10 at 23:33
In javascript its called an object literal and not an associative array (which is a PHP term for the monstrosity that is the do everything arrays in PHP).
– max
Nov 10 at 23:50
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use an associative array. Your tip()
function could be like this.
function tip()
let service_values =
"Good": 0.3,
"ok": 0.25,
"Bad": 0.2
;
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service_label = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
let service = service_values[service_label];
document.getElementById("result").innerHTML= (tipp*service)/split;
EDIT: Check the code snippet below.
<!DOCTYPE html>
<html>
<h3> Tip Calculator <h3>
<script>
function tip()
let service_values =
"Good": 0.3,
"ok": 0.25,
"Bad": 0.2
;
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service_label = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
let service = service_values[service_label];
document.getElementById("result").innerHTML= (tipp*service)/split;
</script>
<body>
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill" value="10">
<br><br>
How was the Service <br><br>
<select id="service">
<option value ="Good">Good</option>
<option value ="Bad">Bad</option>
<option value ="ok">ok</option>
</select>
<br><br>
How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit" value="1">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
</body>
</html>
You can use an associative array. Your tip()
function could be like this.
function tip()
let service_values =
"Good": 0.3,
"ok": 0.25,
"Bad": 0.2
;
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service_label = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
let service = service_values[service_label];
document.getElementById("result").innerHTML= (tipp*service)/split;
EDIT: Check the code snippet below.
<!DOCTYPE html>
<html>
<h3> Tip Calculator <h3>
<script>
function tip()
let service_values =
"Good": 0.3,
"ok": 0.25,
"Bad": 0.2
;
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service_label = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
let service = service_values[service_label];
document.getElementById("result").innerHTML= (tipp*service)/split;
</script>
<body>
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill" value="10">
<br><br>
How was the Service <br><br>
<select id="service">
<option value ="Good">Good</option>
<option value ="Bad">Bad</option>
<option value ="ok">ok</option>
</select>
<br><br>
How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit" value="1">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
</body>
</html>
<!DOCTYPE html>
<html>
<h3> Tip Calculator <h3>
<script>
function tip()
let service_values =
"Good": 0.3,
"ok": 0.25,
"Bad": 0.2
;
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service_label = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
let service = service_values[service_label];
document.getElementById("result").innerHTML= (tipp*service)/split;
</script>
<body>
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill" value="10">
<br><br>
How was the Service <br><br>
<select id="service">
<option value ="Good">Good</option>
<option value ="Bad">Bad</option>
<option value ="ok">ok</option>
</select>
<br><br>
How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit" value="1">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
</body>
</html>
<!DOCTYPE html>
<html>
<h3> Tip Calculator <h3>
<script>
function tip()
let service_values =
"Good": 0.3,
"ok": 0.25,
"Bad": 0.2
;
var tipp = document.getElementById("bill").value;
var split = document.getElementById("billsplit").value;
var service_label = document.getElementById("service").value; //good=.3,ok=.25,bad=.2
let service = service_values[service_label];
document.getElementById("result").innerHTML= (tipp*service)/split;
</script>
<body>
Enter Total Bill<br><br>
<input type="number" name="bill" id="bill" value="10">
<br><br>
How was the Service <br><br>
<select id="service">
<option value ="Good">Good</option>
<option value ="Bad">Bad</option>
<option value ="ok">ok</option>
</select>
<br><br>
How many People are sharing Bill?<br><br>
<input type="number" name="billsplit" id="billsplit" value="1">
<button type="button" onclick="tip()">CALCULATE</button>
<br><br>Result : <span id="result"></span>
</body>
</html>
edited Nov 10 at 23:45
answered Nov 10 at 22:40
Dan D.
2447
2447
Something might be missing - the output when you try to CALCULATE is NaN. I get that you're trying to get the associated value of the inputted service level from the services value array with let service = service_values[service_label]; but is that just returning the actual string not the number?
– vicarus99
Nov 10 at 23:33
In javascript its called an object literal and not an associative array (which is a PHP term for the monstrosity that is the do everything arrays in PHP).
– max
Nov 10 at 23:50
add a comment |
Something might be missing - the output when you try to CALCULATE is NaN. I get that you're trying to get the associated value of the inputted service level from the services value array with let service = service_values[service_label]; but is that just returning the actual string not the number?
– vicarus99
Nov 10 at 23:33
In javascript its called an object literal and not an associative array (which is a PHP term for the monstrosity that is the do everything arrays in PHP).
– max
Nov 10 at 23:50
Something might be missing - the output when you try to CALCULATE is NaN. I get that you're trying to get the associated value of the inputted service level from the services value array with let service = service_values[service_label]; but is that just returning the actual string not the number?
– vicarus99
Nov 10 at 23:33
Something might be missing - the output when you try to CALCULATE is NaN. I get that you're trying to get the associated value of the inputted service level from the services value array with let service = service_values[service_label]; but is that just returning the actual string not the number?
– vicarus99
Nov 10 at 23:33
In javascript its called an object literal and not an associative array (which is a PHP term for the monstrosity that is the do everything arrays in PHP).
– max
Nov 10 at 23:50
In javascript its called an object literal and not an associative array (which is a PHP term for the monstrosity that is the do everything arrays in PHP).
– max
Nov 10 at 23:50
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244023%2fdifferent-approach-simple-tip-calculator%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
With
innerHTML=
on aselect
element you should only defineoption
elements. Anything else is not valid HTML, even if it is just.3
. Such text in adiv
oroption
is fine, just not directly in aselect
. What exactly is the problem?– trincot
Nov 10 at 22:37
Thanks that makes sense. There is no problem. I can get the calc to work fine. I'm trying to learning HTML/JS and I think learning is best done by exploring all potential avenues by which problems might be solved, not just the most obvious ones.
– vicarus99
Nov 10 at 23:12