This feedback form allows the user to enter a numerical rating, calculates an average, and displays the result.
HTML:
Use standard divs and labels to provide the user with instructions.
The three ratings are "Service Quality", "Cleanliness" and "Value for Money".
Use for each question, setting ID= to an appropriate value.
Create a button that triggers the onclick event "calculateFeedback()".
Create a DIV with an appropriate ID to display the results.
JavaScript:
Create a function to calculate the feedback. (Tip: All the code described below can be contained in this one function.)
Create variables for "total" and "count", initialized to zero.
Create a array called "ratings" and assign it (=) the values the user placed in each input control. (
Tip: Use getElementById() to locate the input control
Tip: Use ".value" (after the parentheses) to collect the user's input
Tip: Remember to separate the values in the array with commas, like in the JSON launch file
Create a for-of loop that repeats for each rating in the array. (let X of myarrayname)
In the loop, add the rating to the total without overwriting its previous value, and then increment the counter
Tip: Use parseInt() to convert the input from text, e.g. "parseInt(X, 10);" (specifies base 10 for the number)
Hint: Count is not needed for the loop, but is needed to calculate the average rating
Create a new variable to store the average, then calculate the average with total divided by count.
Use conditional logic (IF) to give the user one of the following messages based on their average rating:
Average rating greater than 4: "Thank you! We are delighted to hear you had a positive experience."
Between 3 and 4: 'Thank you for your feedback. We are constantly working to improve our services.'
Below 3: 'We are sorry to hear about your experience. We appreciate your feedback and will work on making improvements.'
Display the message in the DIV created to display the results. Include (through concatnation) the result of the rating calculation (the average). Example: "Average Rating: 1.7 - We are sorry to hear about your experience. We appreciate your feedback and will work on making improvements."
Tip: You can trim the output to two decimal places using .toFixed(2).
This feedback form allows the user to enter a numerical rating, calculates an average, and displays the result.
HTML:
JavaScript: