Skip to editor
File
New File Ctrl+N
New Folder

Save Ctrl+S
Upload File…
Download as ZIP

Delete File

Reset Files…
View
Toggle Sidebar Ctrl+B
Toggle Terminal Ctrl+`

Word Wrap Alt+Z

Exercise Info

Theme: Dark
Font Size: 14px
Reading Mode: Default
Run
Run Code Ctrl+Enter
Save & Run F5

Clear Output
Help
Keyboard Shortcuts ?
CS173: Intro to Computer Science - Recursion (3 pts)
Explorer
GitHub

Connect to GitHub to push and pull your project files to a repository.

On the page that opens, create a token with repo scope, copy it, then paste it below.

Paste the personal access token you created on GitHub with repo scope.

Click Refresh to load commit history
Exercise Info

Goals

  1. To implement a recursive function call

Instructions

Fill in the reverseString method below to recursively compute the string in reverse.

In this exercise [1], the main() method runs the code on “hello” and “stressed,” and they should give “olleh” and “desserts,” respectively (remember that when you’re feeling stressed during finals week!).

Hints

  • Since s.length()-1 is the last index of a string s, the last character of s can be extracted with s.charAt(s.length()-1)
  • Recall from chapter 2 that for a string s, s.substring(a, b) gives the substring of a string from index a to index b, not including b. So, for instance, if s = “stressed”, then s.substring(0, 7) would yield the string “stresse”. In this case, index 7 is the last index, but we don’t include that index.
  • If you get an error like too much recursion, this is the same as the stack overflow error we saw in the video. This probably means that the string you’re calling recursively isn’t actually decreasing in length, so you never reach the stopping condition of an empty string, and your code goes on forever. Check to make sure you’re taking the right substring in your recursive call.
  1. Developed by Prof. Chris Tralie ↩


The Ursinus-WebIDE by Chris Tralie (opens in new tab) and Bill Mongan (opens in new tab)

Your browser does not support WebGL. A graphical rendering canvas would appear here.


          
No suggestions. Code quality feedback will appear here.
Run your code to inspect variables.
Click Step Run above to record execution steps. Available for Python.

Run your code to see the function-call tape.

For Python, calls are recorded automatically (use Step Run on the Inspector toolbar).

For JavaScript or Java (Processing), you have to record the markers yourself. These helpers are labels — they don't call any function, they only annotate this tape.

Option 1 — sibling log (simple). Call webideTrace.tap('name', arg1, arg2) once per event. Each tap shows up as a flat sibling. Use this when you just want to know "did this code run?" or "in what order did these run?".

Option 2 — scope tracking (for nested / recursive calls). Put webideTrace.call('name', args…) as the first line inside the function body, and webideTrace.return(value) immediately before every return statement (or once at the end if the function is void). Then call the function normally from outside.

// JavaScript example
function fact(n) {
    webideTrace.call('fact', n);          // at the top
    if (n <= 1) {
        return webideTrace.return(1);     // wrap every return
    }
    let r = n * fact(n - 1);
    return webideTrace.return(r);
}
fact(4);   // now the tape shows fact(4) → fact(3) → fact(2) → fact(1)

Every call must be matched by a return. If you put three calls in a row without returns, each one ends up nested inside the previous (which is what the IDE will warn about with "no .return"). For that case use tap instead.

Not logged in java
Ln 1, Col 1
Keyboard Shortcuts

Editing & Files

Save current fileCtrlS
Run codeCtrlEnter
Save and RunF5
Toggle word wrapAltZ

View

Toggle sidebarCtrlB
Toggle terminal panelCtrl`
Focus ExplorerCtrlShiftE
Focus Terminal tabCtrlShiftT
Focus Output tabCtrlShiftO
Focus Suggestions tabCtrlShiftP

Editor Font

Increase font sizeCtrl=
Decrease font sizeCtrl-

Help

Show this dialog?
Close any dialogEsc

Tip: Within an input field, "?" types a literal question mark. Press Esc first to leave the field, then press ? to open this dialog.