Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: Code: !DOCTYPE html> <html> <head> <title>c</title> </head> <body> <sc

ID: 3548831 • Letter: Q

Question

Question:



Code:


!DOCTYPE html>
<html>

<head>
    <title>c</title>
</head>

<body>

    <script>
    
        function input()
        {
            var usage, category, category_name, rate, total;
            
            usage=document.bill.usage.value;
            
            var i=0;
            
            for(i=0; i<document.bill.category.length; i++)
            {
                if(document.bill.category[i].selected)
                {
                category=document.bill.category[i].value;
                }
                
            category_name=get_category_name(category);
            
            rate=get_rate(category_name,usage);
            
            total=rate*usage;
            
            document.write("Total bill : RM"+total);
            
            }
        
        
        }
        
        function get_category_name(category)
        {
        
            var category_name;
            
            if(category=="R")
            {
                category_name="Residential";
            }
            
            else if(category="I")
            {
                category_name="Industry"
            }
            
            return category_name;
        
        }
        
        function get_rate(category_name,usage)
        {
        
            var rate;
        
            switch (category_name)
            {
            
            case "Residential":
            
                if(usage<20)
                {
                
                    rate=0.20;
                
                }
                
                else if(usage>20&&usage<35)
                {
                
                    rate=0.30;
                
                }
                
                else if(usage>35)
                {
                
                    rate=0.45;
                
                }
            
            break;

            case "Industry":
            
                if(usage<20)
                {
                
                    rate=0.10;
                
                }
                
                else if(usage>20&&usage<35)
                {
                
                    rate=0.20;
                
                }
                
                else if(usage>35)
                {
                
                    rate=0.35;
                
                }
            
            break;
            }
            
            return rate;
        
        }
    
    </script>
    
    <form name="bill">
    
        <p>Water Usage : <input type="text" name="uasge"/> m<sup>2</sup></p>
        
        <p>Category:
                <select name="category">
                    <option value="R">Residential</option>
                    <option value="I">Industry</option>
                </select>
        </p>
        
        <p><input type="submit" name="submitbtn" value="get Bill"/>
    
    </form>

</body>

</html>

This program will calculate the cost of water usage. When the button is clicked, it will call function input(). function input(): Get the usage and category from the form. Call function get_category_name(...) and pass the category code to get the category name. Call function get_rate(...) and pass usage and category name to get the rate. Calculate the bill and display on the browser as shown below. function get_category_name(...): Based on the category code, identify the category name using an if else statement. If "R", the name is "Residential". If "I", the name is "Industry". function get_rate(...): Based on the category name, identify the rate using switch statement and if else statement.

Explanation / Answer

The problem was in line 126 where the word usage was not spelt correctly.

The correct code is:




!DOCTYPE html>

<html>


<head>

<title>c</title>

</head>


<body>


<script>


function input()

{

var usage, category, category_name, rate, total;


usage=document.bill.usage.value;


var i=0;


for(i=0; i<document.bill.category.length; i++)

{

if(document.bill.category[i].selected)

{

category=document.bill.category[i].value;

}


category_name=get_category_name(category);


rate=get_rate(category_name,usage);


total=rate*usage;


document.write("Total bill : RM"+total);


}



}


function get_category_name(category)

{


var category_name;


if(category=="R")

{

category_name="Residential";

}


else if(category="I")

{

category_name="Industry"

}


return category_name;


}


function get_rate(category_name,usage)

{


var rate;


switch (category_name)

{


case "Residential":


if(usage<20)

{


rate=0.20;


}


else if(usage>20&&usage<35)

{


rate=0.30;


}


else if(usage>35)

{


rate=0.45;


}


break;


case "Industry":


if(usage<20)

{


rate=0.10;


}


else if(usage>20&&usage<35)

{


rate=0.20;


}


else if(usage>35)

{


rate=0.35;


}


break;

}


return rate;


}


</script>


<form name="bill">


<p>Water Usage : <input type="text" name="usage"/> m<sup>2</sup></p>


<p>Category:

<select name="category">

<option value="R">Residential</option>

<option value="I">Industry</option>

</select>

</p>


<p><input type="submit" name="submitbtn" value="get Bill"/>


</form>


</body>


</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote