-1

Hello everyone I need help with my web scripting homework: Function: Counter Parameter: An array of Numbers. Returns: The numbers of negative, zeros, and positive numbers in the array Note: you must use a switch statement in the function. This is what I have so far

 <!DOCTYPE html>
 <head>
<title>Exercise 4.11</title>
</head>
<body>
<script>
    var num = new Array();
         num[0] = parseInt(prompt("Please enter a number",""),10);
         num[1] = parseInt(prompt("Please enter a number",""),10);
         num[2] = parseInt(prompt("Please enter a number",""),10);
         num[3] = parseInt(prompt("Please enter a number",""),10);
         num[4] = parseInt(prompt("Please enter a number",""),10);

counter(num);

function counter(num) {

    var i,
      count = {
        negative: 0,
        zero: 0,
        positive: 0,
    };

for(i = 0; i < num.length; i++) {
      switch (true) {
        case (num[i] < 0):
            count.negative++;
            break;

        case (num[i] === 0):
            count.zero++;
            break;

        case (num[i] > 0):
            count.positive++;
            break;

    }

}
document.write(count);
};
</script>
  </body>
</html>

3 Answers3

2

There are several things wrong with this, but as you've not really asked a question I'll address them at random.

  • you've created a method called counter for the task of processing the array, but you've not called it
  • you're attempting to document.write after you've already exited the method by calling return first.
  • your prompts will return strings, which need to be parsed into numbers using parseInt

A quick nudge in the right direction:

Call your counter method after you've prompted for the numbers, and change the syntax in the method declaration so that you can call it before it's defined:

var num = new Array();

num[0] = prompt("Please enter a number","");
num[1] = prompt("Please enter a number","");
num[2] = prompt("Please enter a number","");
num[3] = prompt("Please enter a number","");
num[4] = prompt("Please enter a number","");

counter(num);

function counter(num){

  // your logic

}

Update

At the end of your method, you'll want to print the results. As you've discovered, JavaScript is trying to describe the object you've asked it to print by converting it to a string. The string it ends up giving you is pretty uninsightful. What would be better is to print each individual key in the count object:

document.write(count.negative);
document.write(count.zero);
document.write(count.positive);

I would also look into using something other than document.write, but the reasons go beyond the scope of this question.

shennan
  • 8,669
  • 5
  • 32
  • 56
  • ok I see and then to print it out do I just document.write(count); at the end? I'm just getting [Object][object] as my output. – user3765781 Jun 23 '14 at 01:20
  • @user3765781 have updated my answer to address this. – shennan Jun 23 '14 at 01:29
  • only a minor improvement, but use `var num = [];` instead. see http://stackoverflow.com/questions/931872/what-s-the-difference-between-array-and-while-declaring-a-javascript-ar – netzaffin Jun 23 '14 at 08:56
0

I'm going to guess that your problem is that the zero property of your counter object is always 0, because the prompt function returns a string not a number. The === operator only returns true if the two objects being compared are exactly the same, whereas the < and > operators are a bit more flexible. You should either call parseInt on the values returned by prompt, or change your case statement. Here are examples of the two options:

Option 1: ParseInt

num[0] = parseInt(prompt("Please enter a number",""), 10);

Option 2: Case Statement

case (num[i] == "0"):

These are two different approaches you could take. Do not combine them.

Austin Mullins
  • 6,869
  • 2
  • 31
  • 47
0

Hope I'm not late to the party. Here's my 2c:

jsBin demo

var count = {negative:0, zero:0, positive:0},
    i = 0 ;

(function ask(){
    var n = parseInt( prompt("Please enter a number",""), 10 );
    switch (true) {
      case (n<0):
        count.negative++;
        break;
      case (n===0):
        count.zero++;
        break;
      case (n>0):
        count.positive++;
        break;
      default: 
        alert( "Please use only Numbers!" );
        ask(); // Something went wrong... Ask again!
    }
    i++;
  if(i<5) ask();  // ASK 5 times

}()); // Start asking 

document.body.innerHTML = JSON.stringify(count, null, 4) ;

The above will immediately alert you if you did not used a number and repeat the process!
Also parseInt (10 is the radix param.) is needed to make sure to get a Number out of the prompt's String result.

Roko C. Buljan
  • 164,703
  • 32
  • 260
  • 278