1

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

  1. Each amount in the array sales[] that is more than qualifier gets a bonus.
    Remember: array means you think for() loop.
  2. The bonus is calculated as that sales amount times factor.
  3. If the sale is greater than qualifier add the bonus to the bonus total.
  4. 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.

2
  • 2
    Maybe it's sales = new [Array](); - I don't think that's allowed? Just use var sales = []; 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. Commented Feb 12, 2014 at 17:47

2 Answers 2

4

Your 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();
Sign up to request clarification or add additional context in comments.

4 Comments

"extraPay" is the name of the function the professor wants us to use. When I started writing the code, I used the name myFunction on accident. Silly mistake there. D'oh.
I will try your other suggestions here in a second. The professor is very picky about the way he wants us to write our good, which is annoying when it contradicts what most JavaScript tutorials say to do.
It worked, and returned a number that makes sense. I guess what I was missing was a variable to keep a running count of the bonuses like you mentioned. Thank you so much.
No prob. Don't forget to mark as the answer. Good luck on the assignment.
0

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

1 Comment

Ah, this answer is similar to the one I replied to above. It helped me just with what I needed. Thank you so much. I agree that my code has a weirdness to it, I have played with JavaScript prior to this class and what this Professor is teaching is nothing like what I've seen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.