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>












share|improve this question























  • 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














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>












share|improve this question























  • 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












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>












share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 22:37









ksav

3,63221227




3,63221227










asked Nov 10 at 22:25









vicarus99

243




243











  • 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
















  • 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















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












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>








share|improve this answer



























    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>








    share|improve this answer






















    • 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











    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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>








    share|improve this answer
























      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>








      share|improve this answer






















        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>








        share|improve this answer












        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>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 23:10









        Andy

        27.2k63159




        27.2k63159






















            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>








            share|improve this answer






















            • 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















            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>








            share|improve this answer






















            • 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













            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>








            share|improve this answer














            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>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            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

















            • 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


















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線