Saturday
March 25, 2017

Post a New Question

Posted by on .

I'm working on a javascript exercise.
I have written function called count where the count function take in one parameter and the parameter is a array of number. The problem I having is how do i return the numbers of negative elements, zeros, and values greater than zeros in the given array. I don't understand how the switch statement are suppose to implement it.

  • JavaScript - ,

    You are probably best to post the method so far. However, switch statements are shorthands for a group of if statements that all relate to the same quantity as:

    if (x == 0) doSomething0();
    else if (x == 1) doSomething1();
    else if (x == 2) doSomething2();
    else if (x == 3) doSomething3();
    else if (x == 4) doSomething4();
    else doSomethingElse();

    So in your case you need to read the value from the array called numbers,

    {
    for (int counter=0, counter<numbers.length, n++)
    {
    x=numbers[counter];

    // now use the if statements to count how many of each type there are in the array. For clarity you will need to declare three variables for number of negatives, positives and zeros. For example

    int negatives=0;
    int positives=0;
    int zeros=0;

    then use the switch statements to count each of the tree types.

Answer This Question

First Name:
School Subject:
Answer:

Related Questions

More Related Questions

Post a New Question