0

I making one application which contains 2 input box and one input button.My qestion is when user enter some input eg. "I used to going somewhere" then my result should be "I UD GNG somewhere." For that i am using this code http://pastebin.com/vHkhASdZ

Please,anyone have idea how to solve then pls reply me asap. I solved my issue in php but in case of javascript i don't have any idea. Here is my output link in php http://codepad.viper-7.com/SQvO6Y I want my result in javascript.Pls anyone know then give reply.

2 Answers 2

3

Use this function

var replaceText = function () {
    var inputval = document.getElementById('first_text').value;
    var arr = {
        "going": "GNG",
        "used to": "UD",
        "as soon as possible": "ASAP",
        "to do fast": "tdf"
    }

    for(var key in arr) {
        if (typeof (arr[key]) !== "undefined") inputval = inputval.replace(key, arr[key])
    }
    document.getElementById("second_text").value = inputval;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Dear sir,jsfiddle.net/sandeeppandey350/Q6ttQ i tried this but not getting any solution. pls suggest me where i doing wrong.
Why the undefined test? You're not going to get an undefined in a for..in loop (unless you've explicitly assigned a key to undefined).
@nnnnnn Sorry my bad, it should be if(arr.hasOwnProperty(key)). This ables to determine whether an object has the specified property as a direct property of arr object
0

Do something like this. Define word replace as map.

var wordmap = {
  going: 'GNG',
  'used to': 'UD',
  'as soon as possible': 'asap',
  'to do fast': 'tdf',
  ..

}

and define function that iterates every word in the input string.

function replace( text ) {
    var input = text.split(' ');
    var outputval = []
    for (var i in input) {
       outputval.push( wordmap[input[i]] || input[i] )
    }
    return outputval.join(' ');
}

And use it like this

var output = replace( document.getElementById('first_text').value );

1 Comment

Dear sir, jsfiddle.net/sandeeppandey350/M5aNm i tried this but not getting any solution.

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.