6 Replies - 892 Views - Last Post: 16 February 2011 - 10:31 AM

#1 TheBearKing   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 11-February 11

Question regarding switch statements

Posted 16 February 2011 - 10:08 AM

So I have an assignment regarding switch statements in Javascript. He gave us the code, and wants us to add different values for k (which I'm pretty sure I know how to do), but every time I run the program, it gives me the same result. It's however obvious that of the four different results, there should be four different numbers. Any clue on what I'm doing wrong?

<script type="text/javascript">
		<!--
		var k = 3; // k's default value
		var choice; // user's choice
		
		choice = window.prompt( "Select an integer,\n" +
								"1 through 4" );
								
		switch ( k )
		{
			case 1:
				alert(k);
				break;
			case 2:
			case 3:
				alert(++k);
				break;
			case 4:
				alert(--k);
				break;
			default:
				alert(k *= 3);
		} // end switch
		
		x = k;
		//-->
	</script>


I'm not asking for help with homework. Just even a clue as to what I'm doing wrong so that I can get onto the right track with this.

Thanks for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: Question regarding switch statements

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4212
  • View blog
  • Posts: 13,303
  • Joined: 08-June 10

Re: Question regarding switch statements

Posted 16 February 2011 - 10:13 AM

you never change k.
Was This Post Helpful? 1
  • +
  • -

#3 TheBearKing   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 11-February 11

Re: Question regarding switch statements

Posted 16 February 2011 - 10:18 AM

I removed the value from the variable k, and now instead of getting a readout of 4 every time, I'm getting the good ol' NaN.
Was This Post Helpful? 0
  • +
  • -

#4 techcord   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 29-January 10

Re: Question regarding switch statements

Posted 16 February 2011 - 10:20 AM

View PostTheBearKing, on 16 February 2011 - 10:08 AM, said:

So I have an assignment regarding switch statements in Javascript. He gave us the code, and wants us to add different values for k (which I'm pretty sure I know how to do), but every time I run the program, it gives me the same result. It's however obvious that of the four different results, there should be four different numbers. Any clue on what I'm doing wrong?

<script type="text/javascript">
		<!--
		var k = 3; // k's default value
		var choice; // user's choice
		
		choice = window.prompt( "Select an integer,\n" +
								"1 through 4" );
								
		switch ( k )
		{
			case 1:
				alert(k);
				break;
			case 2:
			case 3:
				alert(++k);
				break;
			case 4:
				alert(--k);
				break;
			default:
				alert(k *= 3);
		} // end switch
		
		x = k;
		//-->
	</script>


I'm not asking for help with homework. Just even a clue as to what I'm doing wrong so that I can get onto the right track with this.

Thanks for your time.


Your would want to send the switch Choice and not K

switch ( k )

switch (choice)

Let me know if that helps,

Landon
Was This Post Helpful? 0
  • +
  • -

#5 TheBearKing   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 11-February 11

Re: Question regarding switch statements

Posted 16 February 2011 - 10:25 AM

Changing the k to choice in the switch statement caused the NaN to go away, but now every choice leads to a readout of 3. Also, in cases, does it have to be case "1":, or can I leave the quotations out?

Ok, I got it to work. I changed the k to "choice", and put the case 1 in quotes (case "1", as well as 2 3 and 4). I'm all set now. Thank you guys so much for the help.
Was This Post Helpful? 0
  • +
  • -

#6 techcord   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 27
  • Joined: 29-January 10

Re: Question regarding switch statements

Posted 16 February 2011 - 10:26 AM

View PostTheBearKing, on 16 February 2011 - 10:23 AM, said:

Changing the k to choice in the switch statement caused the NaN to go away, but now every choice leads to a readout of 3. Also, in cases, does it have to be case "1":, or can I leave the quotations out?


I would think since its a charecter you would put '1'

Try that and I will try it also,

Landon
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4212
  • View blog
  • Posts: 13,303
  • Joined: 08-June 10

Re: Question regarding switch statements

Posted 16 February 2011 - 10:31 AM

View PostTheBearKing, on 16 February 2011 - 06:25 PM, said:

Changing the k to choice in the switch statement caused the NaN to go away, but now every choice leads to a readout of 3.

switch is type-save (1 != "1"). stated in ECMAScript 262 3rd Edition, section 12.11, step 9

This post has been edited by Dormilich: 16 February 2011 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1