0

Ok so I'm trying to write some code to determine whether or not values in a static array are positive, negative or equal to zero.

So the array is populated and I'd use a switch statement to go through the values and output text depending on if it is above, below or equal to zero.

Here's some of the code I've been doing so far with this.

Please keep answers which pertain to the use of switches! Thanks in advance.

Note: I'm teaching myself JS, so I'm new to this. Here's my code so far:

// JavaScript Document

var numbers=new Array();
numbers[0]="1";       
numbers[1]="2";
numbers[2]="3";
numbers[3]="-1";       
numbers[4]="-2";
numbers[5]="-3";
numbers[6]="0";



switch (numbers) {
    case "positive":
        if (numbers>0) 
        {alert("DERP")};
    break;
    case "negative":
        if (numbers<0) 
        {alert("NO DERP")};
    break;
   case "zero":
        if (numbers==0) 
        {alert("STILL DERP")};
    break;
}
4
  • 2
    Hint: looking at each element requires you loop over the array. Commented Jun 28, 2012 at 19:32
  • pardon me if this is incorrect but this looks a lot like homework. :P In any case try looking up how switch/cases are structured in js and then try with just one value. then try making a loop for it. Commented Jun 28, 2012 at 19:34
  • Why do you need to use a switch condition? A loop is the normal way to check this. Commented Jun 28, 2012 at 19:38
  • 1
    I agree with @j08691. In fact, you can't really use a switch/case in this situation easily because js switch/cases only alow you to test for equality and won't let you test against an expression. You should probably use an if/else if/else structure imo. Commented Jun 28, 2012 at 19:39

2 Answers 2

3

You need to loop through the array, and check each element. A switch is not the right tool here, it will not do what you want. switches may be a way of doing if/else, but they only check for equality, not less than/greater than.

var str = 'a'

switch(str){
    case 'a':
        alert(1);
        break;
    case 'b':
        alert(2);
        break;
    default:
        alert(0);
        break;
}

This alerts 1.

If the value of str matches one of the case statements (you can't use < or > in the case), the code will run. Otherwise defualt will run. They are not "labels", so checking the value inside the cases makes no sense.

You need to loop, then just use an if/else.

for(var i=0, len=numbers.length; i<len; i++){
    var num = numbers[i];

    if(num > 0) alert('DERP');
    else if(num < 0) alert("NO DERP");
    else alert("STILL DERP");
}
Sign up to request clarification or add additional context in comments.

Comments

1

You compare string and numbers. The numbers in your array are surrounded by "" : this make string. You should remove these "" in your array or add "" in your switch.

1 Comment

Whoops, yea I just removed those.. Still won't work. Looking at the other comments from others now. Yea, I know an array should be used for this. I was just wondering if it is possible for a switch to do it.

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.