I am currently in a web programming class that is taught by a business professor. Naturally he doesn't know much about code and just points me to the book (which is a giant mess of spaghetti everywhere).
The exact assignment I am having issues with is here in in quotes. Hopefully this doesn't look too messy.
DATA AVAILABLE
There is a local storage value you will need named: qualifier
There is a local storage value you will need named: factor
There is an array you will need named: sales
The array and local storage exist so just use them.
WHAT TO RETURN
Return the average bonus.
HOW TO CALCULATE AVERAGE BONUS
- Each amount in the array sales[] that is more than qualifier gets a bonus.
Remember: array means you think for() loop.- The bonus is calculated as that sales amount times factor.
- If the sale is greater than qualifier add the bonus to the bonus total.
- Calculate the average by dividing the total by the number of bonuses that qualify.
Remember: you are returning the average of bonuses that qualify; not the total of bonuses.
I understand that this site is not for you folks to simply do my homework for me, I do not expect that to happen. Below is the current code of what I have. I should note that the teacher has us submit our code into a console that is developed by the university. The reason I say this is because in this assignment, certain local storage variables are already defined, as well as the array. I have been trying to test this function out using notepad and saving it in an HTML document and testing it with a browser. You will notice that my code below is written to be tested with a browser, and that I have defined the array and localstorage variables myself. Hope that makes sense.
Here is my JavaScript:
sales = new [Array]();
sales[0] = 3;
sales[1] = 167;
sales[2] = 191;
sales[3] = 1;
sales[4] = 45;
localStorage.qualifier=5;
localStorage.factor=2;
total = 0;
function myFunction() {
for (i=0;i<sales.length;i++){
if (sales[i]>localStorage.qualifier) {
bonus = sales[i]*localStorage.factor;
total = total + bonus;
};
avg = total/bonus;
};
return avg;
};
myFunction();
alert(extraPay())
I want to write the function to calculate the average bonus. I tried using this web-based text editor from repl.it, and am getting a type error saying the object is not a function. I googled the error, which led me to this site and subsequently creating an account. Thank you so much for your time, if you could point out where my code goes wrong, or if I am just crazy all over the place, that would really help. Thanks! Sorry for the excruciatingly long post.
I am currently in a web programming class that is taught by a business professor. Naturally he doesn't know much about code and just points me to the book (which is a giant mess of spaghetti everywhere).
The exact assignment I am having issues with is here in in quotes. Hopefully this doesn't look too messy.
DATA AVAILABLE
There is a local storage value you will need named: qualifier
There is a local storage value you will need named: factor
There is an array you will need named: sales
The array and local storage exist so just use them.
WHAT TO RETURN
Return the average bonus.
HOW TO CALCULATE AVERAGE BONUS
- Each amount in the array sales[] that is more than qualifier gets a bonus.
Remember: array means you think for() loop.- The bonus is calculated as that sales amount times factor.
- If the sale is greater than qualifier add the bonus to the bonus total.
- Calculate the average by dividing the total by the number of bonuses that qualify.
Remember: you are returning the average of bonuses that qualify; not the total of bonuses.
I understand that this site is not for you folks to simply do my homework for me, I do not expect that to happen. Below is the current code of what I have. I should note that the teacher has us submit our code into a console that is developed by the university. The reason I say this is because in this assignment, certain local storage variables are already defined, as well as the array. I have been trying to test this function out using notepad and saving it in an HTML document and testing it with a browser. You will notice that my code below is written to be tested with a browser, and that I have defined the array and localstorage variables myself. Hope that makes sense.
Here is my JavaScript:
sales = new [Array]();
sales[0] = 3;
sales[1] = 167;
sales[2] = 191;
sales[3] = 1;
sales[4] = 45;
localStorage.qualifier=5;
localStorage.factor=2;
total = 0;
function myFunction() {
for (i=0;i<sales.length;i++){
if (sales[i]>localStorage.qualifier) {
bonus = sales[i]*localStorage.factor;
total = total + bonus;
};
avg = total/bonus;
};
return avg;
};
myFunction();
alert(extraPay())
I want to write the function to calculate the average bonus. I tried using this web-based text editor from repl.it, and am getting a type error saying the object is not a function. I googled the error, which led me to this site and subsequently creating an account. Thank you so much for your time, if you could point out where my code goes wrong, or if I am just crazy all over the place, that would really help. Thanks! Sorry for the excruciatingly long post.
Share Improve this question edited Jun 26, 2014 at 3:29 KyleMit♦ 30.8k72 gold badges511 silver badges702 bronze badges asked Feb 11, 2014 at 17:34 MG_72MG_72 131 gold badge1 silver badge4 bronze badges 2-
2
Maybe it's
sales = new [Array]();
- I don't think that's allowed? Just usevar sales = [];
– Ian Commented Feb 11, 2014 at 17:41 - My professor "demands" that the code be written in "his" way, which is frustrating since it usually doesn't end up working. I will try your way though to see if it will at least work. – MG_72 Commented Feb 12, 2014 at 17:47
2 Answers
Reset to default 4Your error is likely because of this line:
alert(extraPay());
You are alerting to a method that does not exist, unless it is defined elsewhere.
Additionally, it is much faster to declare arrays using just bracket notation:
var sales = [];
If you are going to use the object creation notation, you should do this:
var sales = new Array();
Finally, based on the assignment, and your code, I don't think you will get what you want. You are calculating the total, and dividing it by the last bonus amount. I think what you need to do instead is calculate the total and keep track of how many times you have a bonus. Then divide total by the bonus counter.
function myFunction() {
var counter = 0;
for (i=0;i<sales.length;i++){
if (sales[i]>localStorage.qualifier) {
bonus = sales[i]*localStorage.factor;
total = total + bonus;
counter++;
};
avg = total/counter;
};
return avg;
};
myFunction();
Everyone's pointed out some of the syntax errors and general weirdness with your code. I will point out that you've read one of the instructions wrong:
Calculate the average by dividing the total by the number of bonuses that qualify.
Currently you're doing this...
total = total + bonus;
...when you should be dividing by the number of qualifying bonuses instead. To do that you need a new variable called bonusesQualified
:
function calculate(sales) {
var total = 0, bonusesQualified = 0;
for (i = 0; i < sales.length; i++) {
if (sales[i] > qualifier) {
bonus = sales[i] * factor;
total = total + bonus;
bonusesQualified++;
};
avg = total / bonusesQualified;
};
return avg;
};
Full demo