Jump to content

Can you see issue with function


Caitlin-havener

Recommended Posts

Hey guys! I am having a problem with my convertPig function can you see the issue? It is just supposed to split the string into an array, identify words that are less or more than five letters and add -a or -oink to them, then put the array back into one string and return the variable. Do you see it?The function:

function convertPig(stringToPig){	var pigArray=stringToPig.split(" ");		for (var i in pigArray)	{	  if (pigArray[i].length>=5)	  {		  pigArray[i]=pigArray[i] + "-a";	  }	  else if (pigArray[i].length<5)	  {		  pigArray[i]=pigArray[i] + "-oink";  	  }	}		var pigConverted=pigArray.toString();	return pigConverted;	console.log("String converted.");	}

Whole code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Assignment 1-Caitlin Havener</title><script type="text/javascript">//FUNCTIONS/*Pig Language Spec‐ Pig Language is similar to Pig Latin, but they are not the same!    * If a word is five or more letters long, add “-a” to the end of the word    * If a word is less than five letters long, add “-oink” to the end of the word    * For example: “The quick brown fox” becomes “The-oink quick-a brown-a fox-oink*/function convertPig(stringToPig){	var pigArray=stringToPig.split(" ");		for (var i in pigArray)	{	  if (pigArray[i].length>=5)	  {		  pigArray[i]=pigArray[i] + "-a";	  }	  else if (pigArray[i].length<5)	  {		  pigArray[i]=pigArray[i] + "-oink";  	  }	}		var pigConverted=pigArray.toString();	return pigConverted;	console.log("String converted.");	}//MAIN PROGRAM//   The program should start with the page displaying a message that prompt()s the user to enter either 1, 2, 3, or “Exit”.var keepPrompting=1;do{	var input=prompt("Main menu: Please enter 1, 2, 3, or Exit.");		/*Program 1 – (User enters 1 in the Main Menu to get here)  ‐ The program should prompt() the user to enter a String.  ‐ If the user doesnʼt enter anything, the program should write “You need to enter something” to the Firebug console window and should prompt() them again to enter a 					  String.  ‐ If the user enters a String, the program should return to “Program – Main Menu”.*/	if (parseInt(input)==1)	{  		do		{			var stringToPig=prompt("Please enter the text that you want to be translated into Pig.");				if(!stringToPig)				{					stringToPig=prompt("You need to enter something");				}		}		while(!stringToPig);      	}		/*Program 2 – (User enters 2 in the Main Menu to get here)‐ If the user has already entered a String in step “Program 1”, the program should convert the String into “Pig Language”, write “String converted” to the Firebug console window, and return to “Program – Main Menu”.‐ If the user hasnʼt already entered a String in step “Program 1”, the program should write “You need to first enter a String” to the Firebug console window and return to “Program – Main Menu”.*/	else if (parseInt(input)==2)	{		if(stringToPig)		{			convertPig(stringToPig);		}		else		{			console.log("You need to first enter a string.");		}	}		/*Program 3 – (User enters 3 in the Main Menu to get here)‐ If a String has already been converted in step “Program 2”, the “Pig Language” version of the String should be written to the Firebug console window and the program should clear all Strings and return to “Program – Main Menu”.‐ If a String hasnʼt already been converted in step “Program 2”, the program should write “You need to first convert your String” to the Firebug console window and return to “Program – Main Menu”.*/	else if (parseInt(input)==3)	{		if(pigConverted)		{			console.log("The string converted to pig is: " + pigConverted);			delete input, stringToPig, pigConverted;			//input=undefined; ?? which one		}		else		{			console.log("You need to first convert your string.");		}	}		/*Program Exit – (User enters “Exit” in the Main Menu to get here)‐ If the user enters “Exit” in the Main Menu, the program should write “Thanks for using the Pig Language Converter!” to the Firebug console window and stop executing.*/	else if (input.toUpperCase()=="EXIT")	{		console.log("Thanks for using the Pig Language Converter!");		keepPrompting=0;		}	else	{			}}while (keepPrompting==1);//program will repeat until the user indicates that they want to exit./*General‐ Code should be indented to show parent/child relationships.‐ The server path for this assignment should be ~/public_html/dig3480c/assignment1‐ Your XHTML page should be located in the root directory for this assignment and should be named page_index.html‐ All directories and filenames should contain no spaces or uppercase letters.Functional Spec‐ Global variables may be used in this assignment, but use them only when necessary.‐ All user interaction with the script should happen through prompt() and the Firebug console window.‐ The program should run continuously until the user types “Exit” in “Program Main Menu”.Presentation‐ There are no presentation requirements for this assignment because all interaction will happen through prompt() and the Firebug console window.Content ‐ The <body> of the XHTML page shouldnʼt contain anything.Rubric/20 Implementation JavaScript specs followed/80 Functional Specs followed-20 “Program Main Menu” doesnʼt work according to spec-20 “Program 1” doesnʼt work according to spec-20 “Program 2” doesnʼt work according to spec-20 “Program 3” doesnʼt work according to spec-100 if your assignment path and assignment file name are not exactly as requested in the “delivery method” section above.ʻExactly as requestedʼ means your assignment folder and assignment file name must be named as requested, not contain spaces, and not contain capital letters.-100 if any JavaScript libraries are used /100 TOTAL*/</script></head><body></body></html>

Link to comment
Share on other sites

Can you describe what the results of your script are and what results you expected? The thing I see that might be causing trouble is the use of for...in rather than a normal for loop to transverse an array.

Link to comment
Share on other sites

Can you describe what the results of your script are and what results you expected? The thing I see that might be causing trouble is the use of for...in rather than a normal for loop to transverse an array.
Well I tried a normal for loop as well. for(i=0; i<pigArray.length;i++) didnt work as well. My program has a main menu that prompts you for a number- you are expected to follow the 1-4 sequence (I don't know my teacher wrote the criteria). So you press 1 and it asks for a string. It then goes back to the main menu, then you type 2. It should then take your string and run it through the convertPig function. The parameter for that function should be the string. It is supposed to split the string into an array of each word in the string. Then it evaluates each word to see if it is longer than or five characters it should add "-a" to the end of the word and if it is less than 5 characters it adds "-oink" to it. At the end of the function it is supposed to take the array and make it one string again and then pass that string back out of the function (pigConverted).
Link to comment
Share on other sites

can you show us an example starting string and what the outcome is for you implementation when using the phrase "The quick brown fox"? I would seconds Ingolme's recommendation of just using a straight for loop.I think you should be adding a lot of logging. for example:

function convertPig(stringToPig){		console.log('Starting String => ' + stringToPig);	var pigArray=stringToPig.split(" ");	var convertedString = "";	console.log(pigArray);	for (var i  = 0; i < pigArray.length; i++){		  var phrase = pigArray[i];	  console.log('Current Phrase => ' + phrase);	  if (phrase.length >=5){			 phrase += "-a";	  }else if (phrase.length<5){			 phrase += "-oink";  	  };	  convertedString += (phrase + " ");	  //shorter version for the above if/else lines	  //convertedString += (phrase.length >= 5) ? phrase += "-a" : phrase += "-oink";	  console.log('ConvertedString in Progress => ' + convertedString);	};	console.log('Final Converted String => ' + convertedString);	return convertedString;};

Link to comment
Share on other sites

good deal. It didn't seem like there was much wrong with with it to begin with, just some basic refactoring and confirmation to tell you where the problem could be coming from.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...