This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by 杰里米
almost 12 years

2.6 lost beyond imagination

Ok this recursive loop goes upwards so I assume the base case is (index===length)

output should be like this: question tion on n values needed: [0,8] [4,4] [6,2] [7,1]

Answer 4fed6521d3f606000300b645

8 votes
Best answer

Permalink

Have you tried the recursive call like this?

help(Math.ceil( (length + index) / 2 ));

I think this is simpler than:

help(Math.ceil(length-(length-index)/2));
points
almost 12 years

Answer 4fd20a708c367d000301f8b9

6 votes

Permalink

 // Function to recursively print the right half of a string
var right = function(str){
var length = str.length;

// Helper function
var help = function(index){
    
    // Recursive Case: Print right half
    if(index<length){
        // Prints characters from index until the end of the array
        console.log(str.substring(index,length));
        // Recursive Call: call help on right half
        help(length-Math.floor((length-index)/2));
    }
    // Base Case: Do Nothing
};

help(0);
};

 // Testing
 var word = "question";
 right(word);
points
Submitted by 杰里米
almost 12 years

Answer 4fbd45e609732d00030067c2

4 votes

Permalink

FINALLY SOLVED line 13 help(length-Math.floor((length-index)/2));

So this is much different than what the author had said to do but I got the correct output and credit for the exercise. So the question remains what is the REAL answer. Despite my frustrations I really do love these exercises.

points
Submitted by 杰里米
almost 12 years

Answer 5037d0b8955fa2000201ac7e

4 votes

Permalink

I wrote my if condition like this:

if(index < length ){
    // Prints characters from index until the end of the array
    console.log(str.substring(index, length));
    
    // Recursive Call: call help on right half
    help(Math.ceil( index + (length - index)/ 2));           
} 

…then I came here and noticed what Ruben wrote and, though my math skills are weak, I had to figure out how:

(L + I)/2 was equal to I + (L-I)/2

Here’s that process:

  1. I + (L-I)/2 is equal to I + L/2 - I/2
  2. I + L/2 - I/2 is equal to 2I/2 + L/2 - I/2
  3. 2I/2 + L/2 - I/2 simplifies to I/2 + L/2
  4. I/2 + L/2 simplifies again to (I + L)/2

I wish I that had occured to me on my own! Thanks, Ruben.

points
Submitted by Ross Studtman
over 11 years

Answer 4fd1cb1755a4e80003010091

0 votes

Permalink

Well it is not much different, my line was: help(Math.ceil(length-(length-index)/2)); But yeah, the assignment is far from easy :)

points
Submitted by Andrej Bergant
almost 12 years

Answer 4fd2076ceb42bf000301fc6a

0 votes

Permalink

That’s all well and good, but what about line 9? I’m not sure what to put in there. My current line 9 is as follows: if(index > 0){ But I think it should be something more like: if(index = 0; index < length; index+=2){ What is line 9 and why?

points
Submitted by Stacy Bjorgaard
almost 12 years

Answer 4fd21368a8df0b0003001369

0 votes

Permalink

Thanks, x13420x. I over-complicated the problem.

points
Submitted by Stacy Bjorgaard
almost 12 years

Answer 50857abba476e40200000307

0 votes

Permalink

That’s my code - it’s much simpler but it does the job of printing the right half of a string. However, it’s not accepted as correct. Is it because i reduce the initial string? Any ideas?

var right = function(str){ var length = str.length;
var help = function(index){ if(length>1){ index=length/2; console.log(str.substring(index, length)); str=str.substring(index,length); length=str.length; help(Math.ceil(str)); }

    else {}
};

help(0);

};

var word = “question”; right(word);

points
Submitted by neda02
over 11 years