JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 14th, 2013, 09:20 AM
ZWEI01 ZWEI01 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 10 ZWEI01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 54 m 13 sec
Reputation Power: 0
Recursive Function Help (newbie)

Well, I'm currently in the process of learning JS, and I started to write a unscramble-word game last night. Problem is the function I wrote to scramble a word keeps returning undefined and after much frustration I need some help.

Code:
	function scramble (toScramble, recursions) {
		//Condition. scrambles word 5 times.
		if (recursions == 5) {
			return toScramble;
		} else {
			recursions++;
			var size = toScramble.length - 1;
			//Get random index to select random split letter
			//from string.
			var index = Math.floor(Math.random() * size);
			var splitArray = toScramble.split(toScramble[index]);
			//Add split letter back to new array to maintain word.
			splitArray + toScramble[index];
			
			var size = splitArray.length - 1;
			newString = "";	
			for (var i = size; i >= 0; i--) {
				//Cocacenate word backwards with for loop.
				newString += splitArray[i];
				if (i == 0) {
					scramble(newString, recursions);
				}
			}
		}
}

Reply With Quote
  #2  
Old January 14th, 2013, 10:10 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2012
Location: Germany
Posts: 2,014 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 6 Days 11 h 12 m 17 sec
Reputation Power: 812
Hi,

you're missing a "return" for the recursive function call. Without that, the whole recursion doesn't work, because the final value doesn't get passed on. The "original" function call simply returns nothing, so you end up with "undefined".

There are several other logical errors and Javascript mistakes:
Code:
splitArray + toScramble[index];

This line by itself doesn't do anything, because it's an expression like "1 + 1". Without an assignment, it's just discarded. The value of the expression also isn't correct, because you add a string to an array. This yields a nonsense string like "ab,def". I guess you meant something like this:
Code:
splitArray.push(toScramble[index]);

However, there's a logical error: When the split character occurs multiple times and you only add it once at the end, all the characters except one get lost.

It might be a good idea to start with a simpler recursive function to understand the concept. A classical example would be the factorial function: fac(n) = 1 * 2 * ... *n
Code:
function fac(n) {
	if (n == 0)
		return 1;					// base case
	else
		return n * fac(n - 1);		// recursive call
} 

You can also try to recursively revert an error.
Comments on this post
ZWEI01 agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Recursive Function Help (newbie)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap