Javascript switch statement

malfunctioning default section

Page 1 of 1

11 Replies - 2824 Views - Last Post: 15 November 2010 - 08:57 PM

#1 niteMaresInKoad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Javascript switch statement

Posted 11 November 2010 - 07:24 PM

I have a fully(almost) functiong javascript program that has a switch statement with a malfunctioning "default" section.

switch (vowel) {
       case vowel: document.write('The leftmost vowel is "' + text.charAt(vowel) + '" in position ' + vowel );
                 
       break;
  
       default:	document.write('Characters entered contained no vowels');
        }



So when a non vowel containing string is entered then"The leftmost vowel is -1" appears and this is logically correct, but my default statement of "Characters entered contained no vowels" in my default section does not appear.
Any suggestions.

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript switch statement

#2 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Re: Javascript switch statement

Posted 11 November 2010 - 07:37 PM

The problem that you are having is that it will always be true, so the default statement will never be executed. You are putting the variable "vowel" through the switch statement, and then your first case is also the variable "vowel", therefore, your first case will be executed. What you might want to do is instead, use an if statement, like so:
var pos = text.charAt(vowel);
if(pos!=-1)
     document.write('The leftmost vowel is "' + pos + '" in position ' + vowel);
else
     document.write('Characters entered contained no vowels');


Was This Post Helpful? 0
  • +
  • -

#3 niteMaresInKoad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Re: Javascript switch statement

Posted 11 November 2010 - 07:51 PM

View Postpsyking, on 11 November 2010 - 06:37 PM, said:

The problem that you are having is that it will always be true, so the default statement will never be executed. You are putting the variable "vowel" through the switch statement, and then your first case is also the variable "vowel", therefore, your first case will be executed. What you might want to do is instead, use an if statement, like so:
var pos = text.charAt(vowel);
if(pos!=-1)
     document.write('The leftmost vowel is "' + pos + '" in position ' + vowel);
else
     document.write('Characters entered contained no vowels');



still same answer sir, get "The leftmost vowel is -1" and no default statement...I want to give you all of my code, but competition is fierce these days, let me know otherwise...if you need function definition and other sections.
Was This Post Helpful? 0
  • +
  • -

#4 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Re: Javascript switch statement

Posted 12 November 2010 - 11:24 AM

View PostniteMaresInKoad, on 12 November 2010 - 01:51 AM, said:

View Postpsyking, on 11 November 2010 - 06:37 PM, said:

The problem that you are having is that it will always be true, so the default statement will never be executed. You are putting the variable "vowel" through the switch statement, and then your first case is also the variable "vowel", therefore, your first case will be executed. What you might want to do is instead, use an if statement, like so:
var pos = text.charAt(vowel);
if(pos!=-1)
     document.write('The leftmost vowel is "' + pos + '" in position ' + vowel);
else
     document.write('Characters entered contained no vowels');



still same answer sir, get "The leftmost vowel is -1" and no default statement...I want to give you all of my code, but competition is fierce these days, let me know otherwise...if you need function definition and other sections.


All I need is the answer to one question, for the variable "vowel", are you using it as an integer/number or are you using it as a character/word/letter?
Was This Post Helpful? 0
  • +
  • -

#5 niteMaresInKoad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Re: Javascript switch statement

Posted 12 November 2010 - 04:47 PM

View Postpsyking, on 12 November 2010 - 10:24 AM, said:

View PostniteMaresInKoad, on 12 November 2010 - 01:51 AM, said:

View Postpsyking, on 11 November 2010 - 06:37 PM, said:

The problem that you are having is that it will always be true, so the default statement will never be executed. You are putting the variable "vowel" through the switch statement, and then your first case is also the variable "vowel", therefore, your first case will be executed. What you might want to do is instead, use an if statement, like so:
var pos = text.charAt(vowel);
if(pos!=-1)
     document.write('The leftmost vowel is "' + pos + '" in position ' + vowel);
else
     document.write('Characters entered contained no vowels');



still same answer sir, get "The leftmost vowel is -1" and no default statement...I want to give you all of my code, but competition is fierce these days, let me know otherwise...if you need function definition and other sections.


All I need is the answer to one question, for the variable "vowel", are you using it as an integer/number or are you using it as a character/word/letter?



There are no int variables used, only 2 vars, but a charAt(vowel) is used. Hate to be so stingy on my code, but here are some snippets
function first_vowel (letter)
   { var vowel;
     vowel = letter.search(/[aeiou]/i);
     return vowel;
   }

and

vowel = first_vowel(text);

and

var pos = text.charAt(vowel);



Was This Post Helpful? 0
  • +
  • -

#6 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Re: Javascript switch statement

Posted 12 November 2010 - 07:31 PM

View PostniteMaresInKoad, on 12 November 2010 - 10:47 PM, said:

There are no int variables used, only 2 vars, but a charAt(vowel) is used. Hate to be so stingy on my code, but here are some snippets
function first_vowel (letter)
   { var vowel;
     vowel = letter.search(/[aeiou]/i);
     return vowel;
   }

and

vowel = first_vowel(text);

and

var pos = text.charAt(vowel);





Ah, OK. When you call the function "first_vowel", it is returning an integer. That might be why you are getting an error.
Try this:
var vowel = first_vowel(text);
if(vowel!=-1)
     document.write("The leftmost vowel is "' + text.charAt(vowel) + '" in position ' + vowel);
else
     document.write("Characters entered contained no vowels");


Try that and see if it works.
Was This Post Helpful? 0
  • +
  • -

#7 niteMaresInKoad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Re: Javascript switch statement

Posted 12 November 2010 - 08:51 PM

View Postpsyking, on 12 November 2010 - 06:31 PM, said:

View PostniteMaresInKoad, on 12 November 2010 - 10:47 PM, said:

There are no int variables used, only 2 vars, but a charAt(vowel) is used. Hate to be so stingy on my code, but here are some snippets
function first_vowel (letter)
   { var vowel;
     vowel = letter.search(/[aeiou]/i);
     return vowel;
   }

and

vowel = first_vowel(text);

and

var pos = text.charAt(vowel);





Ah, OK. When you call the function "first_vowel", it is returning an integer. That might be why you are getting an error.
Try this:
var vowel = first_vowel(text);
if(vowel!=-1)
     document.write("The leftmost vowel is "' + text.charAt(vowel) + '" in position ' + vowel);
else
     document.write("Characters entered contained no vowels");


Try that and see if it works.



Not just yet, most of the program works, I'll have to put it down for a while and then come back to it. I do have another question about another problem if you would be so kind.

Have to create program that returns "Valid" or "Invalid" when a name is input. I know I need to use the search function and maybe a quantifier like * or + or ? Need much more help on this one 'cause I am borderline clueless on this one, here is what I have so far:

<script language="Javascript">//
var input = window.prompt("Please input a name.","");
input = str.search(/\d/g);
document.write(matches, "<br />");


<!--if result = 0
then return Valid
else
if return Invalid-->
</script>



Input data is name that has to fit required format of last name, first name, middle initial, havin' a lot of problems with this one.

Suggestions Mr. Guru?

This post has been edited by niteMaresInKoad: 12 November 2010 - 08:53 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,582
  • Joined: 08-June 10

Re: Javascript switch statement

Posted 13 November 2010 - 01:46 AM

View PostniteMaresInKoad, on 13 November 2010 - 03:51 AM, said:


<script language="Javascript">//
var input = window.prompt("Please input a name.","");
input = str.search(/\d/g);
document.write(matches, "<br />");


<!--if result = 0
then return Valid
else
if return Invalid-->
</script>



Input data is name that has to fit required format of last name, first name, middle initial, havin' a lot of problems with this one.

Suggestions Mr. Guru?


- undefined variables (str, matches)
- invalid characters (<!-- --> is a HTML comment and causes an error in JS) - though I assume that should be pseudo code
Was This Post Helpful? 0
  • +
  • -

#9 niteMaresInKoad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Re: Javascript switch statement

Posted 13 November 2010 - 07:16 AM

View PostDormilich, on 13 November 2010 - 12:46 AM, said:

View PostniteMaresInKoad, on 13 November 2010 - 03:51 AM, said:


<script language="Javascript">//
var input = window.prompt("Please input a name.","");
input = str.search(/\d/g);
document.write(matches, "<br />");


<!--if result = 0
then return Valid
else
if return Invalid-->
</script>



Input data is name that has to fit required format of last name, first name, middle initial, havin' a lot of problems with this one.

Suggestions Mr. Guru?


- undefined variables (str, matches)
- invalid characters (<!-- --> is a HTML comment and causes an error in JS) - though I assume that should be pseudo code



yes, I am aware of the comment section of that snippet, it's actually unfinished pseudocode, that last part should be:
else
  result != 0
  return "Invalid"


let me tweak this some more and I shall return, thanks again.
Was This Post Helpful? 0
  • +
  • -

#10 psyking  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 165
  • Joined: 17-January 10

Re: Javascript switch statement

Posted 15 November 2010 - 03:57 PM

What would you define a "Valid" an "Invalid" name to be?
Was This Post Helpful? 0
  • +
  • -

#11 niteMaresInKoad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Re: Javascript switch statement

Posted 15 November 2010 - 07:33 PM

View Postpsyking, on 15 November 2010 - 02:57 PM, said:

What would you define a "Valid" an "Invalid" name to be?


Can't get this to work for the life of me. Trying to input name in format "Last name, first name, middle initial(one character for initial). Output is supposed to say "Valid" or "Invalid". Cannot figure out the RegExp for this or get my If statement to work.
<script language="Javascript">//
var str = window.prompt("Please input a name.","");
var patt = /\w\s\w\s\w/gi;
//var patt = /\w/;
document.write(str.match(patt));


if (patt == 0)
{
 document.write(" is Valid");
 }
 else
{
 document.write(" is Invalid");
}
</script>


Need help. Will give awesome kudos.
Was This Post Helpful? 0
  • +
  • -

#12 niteMaresInKoad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Re: Javascript switch statement

Posted 15 November 2010 - 08:57 PM

View PostniteMaresInKoad, on 15 November 2010 - 06:33 PM, said:

View Postpsyking, on 15 November 2010 - 02:57 PM, said:

What would you define a "Valid" an "Invalid" name to be?


Can't get this to work for the life of me. Trying to input name in format "Last name, first name, middle initial(one character for initial). Output is supposed to say "Valid" or "Invalid". Cannot figure out the RegExp for this or get my If statement to work.
<script language="Javascript">//
var str = window.prompt("Please input a name.","");
var patt = /\w\s\w\s\w/gi;
//var patt = /\w/;
document.write(str.match(patt));


if (patt == 0)
{
 document.write(" is Valid");
 }
 else
{
 document.write(" is Invalid");
}
</script>


Need help. Will give awesome kudos.


Booyah!
[.search(/^[a-zA-Z]{2,}\s+[a-zA-Z]{2,}\s+[a-zA-Z]+\s*$/))==-1 ? "Invalid" : "Valid" );


This post has been edited by niteMaresInKoad: 15 November 2010 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1