0

I am trying to create a function that builds a string made up of the characters of two given strings.

The function has 3 arguments:

  1. searchString - a String that is scanned character by character to identify the position of newCharacter
  2. originalString - a string the same length as searchString
  3. newCharacter - a 1 character string.

The function should return a new string that contains newCharacter in the same positions as in searchString otherwise the characters at the corresponding positions of the originalString.

Example:

searchString = data, 
originalString = bcde 
newCharacter = a

The function would return "bada".

5
  • 1
    Can you provide sample input and output of what you're trying to accomplish? Commented Sep 18, 2011 at 7:45
  • 1
    Your description is very confusing. For start, what is targetString? Another argument? Commented Sep 18, 2011 at 7:47
  • Sure. Let me give it a go and I'll try to explain. Commented Sep 18, 2011 at 7:49
  • Could you provide a sample with inputs an an expected result? Commented Sep 18, 2011 at 7:49
  • Okay - updated the question with some examples and tried to clarify in more depth what I'm struggling with. Thanks very much. Commented Sep 18, 2011 at 7:59

2 Answers 2

1

try this:

var rossFn = function (searchString, originalString, newCharacter) {
    var initialValue = "";
    for (var i = 0; i < searchString.length; i++) {
        if (searchString.charAt(i) === newCharacter) {
            initialValue += newCharacter;
        } else {
            initialValue += originalString.charAt(i);
        }
    }
    return initialValue;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for this. I'm sorry that it doesn't make much sense. I have elaborated a little on the initial question. You guys clearly have superior brains :)
Ah, thanks a lot! You're a genius. it works perfectly. Haha. I'm amazed that you were able to figure that out from my gibberish.
0

Another way to solve this:

function manipulateString(searchString, originalString, newCharacter)
{
    //convert strings to array
    searchString = searchString.split(''); 
    originalString = originalString.split(''); 
    //find indices in the search string where new character occurs
    var matchingIndicesInSearchString = searchString.reduce(function(a, e, i) {
        if (e === newCharacter)
            a.push(i);
        return a;
    }, []);  

    matchingIndicesInSearchString.forEach(function (value, index, array) 
    {  //replace characters in the original string with the new character at the right indices
       originalString.splice(value,1, newCharacter);
    });
    //convert array back to string 
    return originalString.toString().replace(/,/g,'');  
}

It works. Please use like so:

var searchString   = 'data', 
    originalString = 'bcde', 
    newCharacter   = 'a';

manipulateString(searchString, originalString, newCharacter); //outputs 'bada'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.