Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8493461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:01:01+00:00 2026-06-10T23:01:01+00:00

I’m working on a codecademy exercise where we’re building a hangman game with jQuery/JavaScript.

  • 0

I’m working on a codecademy exercise where we’re building a hangman game with jQuery/JavaScript. The player starts with a string of underline characters ‘‘ and if they guess a letter in the word, the appropriate underline should be replaced with that letter. For example, if the word is ‘tree’ and the player guesses ‘e’, the function should return ‘_ee’

I wrote the function using gsub. N is the position where the string is supposed to be alterned, and c is the character to insert at that position on the original string. This passes the test at codecademy.

function alterAt ( n, c, originalString ) { 
   var k = originalString.gsub(n, c ); 
   return k; 

}

However, codecademy told me to use the function substr() and concatenation to return a new string with the letter replaced. I can’t figure out how to do it this way, and I’m not even sure if it’s the better way to do it. What’s wrong with using gsub? Can you assist?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:01:03+00:00Added an answer on June 10, 2026 at 11:01 pm

    Here’s a version using substr():

    function alterAt ( n, c, originalString ) { 
        return(originalString.substr(0, n) + c + originalString.substr(n + 1));
    }
    

    Working demo: http://jsfiddle.net/jfriend00/YX94Q/

    If, in the context of your hangman game, you had two strings, one the original and one the underscores plus guessed letters and you wanted to apply a guess to the underscores, you could do that like this:

    function alterAt ( n, c, originalString ) { 
        return(originalString.substr(0, n) + c + originalString.substr(n + 1));
    }
    
    function replaceMatchingUnderscores(guessChar, displayStr, original) {
        var index = 0;
        while ((index = original.indexOf(guessChar, index)) !== -1) {
            displayStr = alterAt(index, guessChar, displayStr);
            index++;
        }
        return(displayStr);
    }
    

    Working demo: http://jsfiddle.net/jfriend00/EwfxW/.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.