Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Ujjwal Sharma
    @ryzokuken
    hint
    CamperBot
    @camperbot

    hint [1/9]

    ## :construction: After this are possible spoiler hints.

    Make sure you've tried to hard to solve it yourself before proceeding. :construction:

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    Ujjwal Sharma
    @ryzokuken
    hint
    CamperBot
    @camperbot

    hint [2/9]

    # Problem Explanation:

    • You will create a program that takes a sentence, then search for a word in it and replaces it for a new one while preserving the uppercase if there is one.

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    Ujjwal Sharma
    @ryzokuken
    hiny
    hint
    CamperBot
    @camperbot

    hint [3/9]

    Hint: 1

    • Find the index where before is in the string.

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    Ujjwal Sharma
    @ryzokuken
    hint
    CamperBot
    @camperbot

    hint [4/9]

    Hint: 2

    • Check first letter case.

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    Ujjwal Sharma
    @ryzokuken
    hint
    CamperBot
    @camperbot

    hint [5/9]

    Hint: 3

    • Strings are immutable, you will need to save the edits on another variable, even if you must reuse the same one just to make it look like the changes where done using just that one variable.

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    David
    @DavidPhan1992
    hitn
    hint
    CamperBot
    @camperbot

    hint [1/9]

    ## :construction: After this are possible spoiler hints.

    Make sure you've tried to hard to solve it yourself before proceeding. :construction:

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    David
    @DavidPhan1992
    hint
    CamperBot
    @camperbot

    hint [2/9]

    # Problem Explanation:

    • You will create a program that takes a sentence, then search for a word in it and replaces it for a new one while preserving the uppercase if there is one.

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    David
    @DavidPhan1992
    hint
    CamperBot
    @camperbot

    hint [3/9]

    Hint: 1

    • Find the index where before is in the string.

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    David
    @DavidPhan1992
    hint
    CamperBot
    @camperbot

    hint [4/9]

    Hint: 2

    • Check first letter case.

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    David
    @DavidPhan1992
    hint
    CamperBot
    @camperbot

    hint [5/9]

    Hint: 3

    • Strings are immutable, you will need to save the edits on another variable, even if you must reuse the same one just to make it look like the changes where done using just that one variable.

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    David
    @DavidPhan1992
    hint
    CamperBot
    @camperbot

    hint [6/9]

    Spoiler Alert!

    687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

    Solution ahead!

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    David
    @DavidPhan1992
    hint
    CamperBot
    @camperbot

    hint [7/9]

    Code Solution #1:

    function myReplace(str, before, after) {
      // Find index where before is on string
      var index = str.indexOf(before);
      // Check to see if the first letter is uppercase or not
      if (str[index] === str[index].toUpperCase()) {
        // Change the after word to be capitalized before we use it.
        after = after.charAt(0).toUpperCase() + after.slice(1);
      }
      // Now replace the original str with the edited one.
      str = str.replace(before, after);
    
      return str;
    }

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    David
    @DavidPhan1992
    hint
    CamperBot
    @camperbot

    hint [8/9]

    Code Solution #2:

    function myReplace(str, before, after) {
    //Create a regular expression object
      var re = new RegExp(before,"gi");
    //Check whether the first letter is uppercase or not
      if(/[A-Z]/.test(before[0])){
      //Change the word to be capitalized
        after = after.charAt(0).toUpperCase()+after.slice(1);
         }
         //Replace the original word with new one
      var  newStr =  str.replace(re,after);
    
     return newStr;
    
    }

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    David
    @DavidPhan1992
    hint
    CamperBot
    @camperbot

    hint [9/9]

    Code Solution #3:

    function myReplace(str, before, after) {
    
        // create a function that will change the casing of any number of letter in parameter "target" 
        // matching parameter "source"
        function applyCasing(source, target) {
            // split the source and target strings to array of letters
            var targetArr = target.split("");
            var sourceArr = source.split("");
            // iterate through all the items of sourceArr and targetArr arrays till loop hits the end of shortes array
            for (var i = 0; i < Math.min(targetArr.length, sourceArr.length); i++){
                // find out the casing of every letter from sourceArr using regular expression
                // if sourceArr[i] is upper case then convert targetArr[i] to upper case 
                if (/[A-Z]/.test(sourceArr[i])) {
                    targetArr[i] = targetArr[i].toUpperCase();
                }
                // if sourceArr[i] is not upper case then convert targetArr[i] to lower case
                else targetArr[i] = targetArr[i].toLowerCase();
            } 
            // join modified targetArr to string and return
            return (targetArr.join(""));
        }
    
    
        // replace "before" with "after" with "before"-casing 
        return str.replace(before, applyCasing(before, after));
    }

    Code Explanation:

    • Read comments on program.

    Credits:

    If you found this page useful, you can give thanks by copying and pasting this on the main chat: thanks @Rafase282 @coded9 @aganita for your help with Bonfire: Search and Replace

    NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    David
    @DavidPhan1992
    hint
    CamperBot
    @camperbot

    hint [1/1]

    ## :construction: After this are possible spoiler hints.

    Make sure you've tried to hard to solve it yourself before proceeding. :construction:

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    David
    @DavidPhan1992
    hint
    CamperBot
    @camperbot
    These hints depend on people like you! Please add to this :point_right: Bonfire's Wiki Hints Page
    David
    @DavidPhan1992
    hint
    CamperBot
    @camperbot

    hint [1/1]

    ## :construction: After this are possible spoiler hints.

    Make sure you've tried to hard to solve it yourself before proceeding. :construction:

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    David
    @DavidPhan1992
    hint
    CamperBot
    @camperbot
    These hints depend on people like you! Please add to this :point_right: Bonfire's Wiki Hints Page
    Ujjwal Sharma
    @ryzokuken
    hint
    CamperBot
    @camperbot

    hint [1/1]

    ## :construction: After this are possible spoiler hints.

    Make sure you've tried to hard to solve it yourself before proceeding. :construction:

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    Ujjwal Sharma
    @ryzokuken
    hint
    CamperBot
    @camperbot
    These hints depend on people like you! Please add to this :point_right: Bonfire's Wiki Hints Page
    Ujjwal Sharma
    @ryzokuken
    hint
    CamperBot
    @camperbot

    hint [1/1]

    ## :construction: After this are possible spoiler hints.

    Make sure you've tried to hard to solve it yourself before proceeding. :construction:

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    Ujjwal Sharma
    @ryzokuken
    hint
    CamperBot
    @camperbot
    These hints depend on people like you! Please add to this :point_right: Bonfire's Wiki Hints Page
    Ujjwal Sharma
    @ryzokuken
    hint
    CamperBot
    @camperbot

    hint [1/1]

    ## :construction: After this are possible spoiler hints.

    Make sure you've tried to hard to solve it yourself before proceeding. :construction:

    type hint for next hint :pencil: [Contribute at the FCC Wiki]

    Faiz Ahmad Faiz
    @Faizahmadfaiz
    bf spoiler
    CamperBot
    @camperbot
    These hints depend on people like you! Please add to this :point_right: Bonfire's Wiki Hints Page