Replace word using dictionary Program

Replace word using dictionary Program 



to replace word using dictionary javascript Here is an example of a program in JavaScript that replaces a word in a string using a dictionary:

 const dictionary = { "{NANE}": "Jhon", "{DESIGNATION}": "Software Engineer", };

 const replaceWord = (sentence, dictionary) =>
 { 

 let newSentence = sentence;

 for (let key in dictionary) { 

     newSentence = newSentence.replace(key, dictionary[key]);

 }

 return newSentence;

 }; 


const sentence = "My name is {NAME} and I am {DESIGNATION}";


 console.log(replaceWord(sentence, dictionary));

 // Output: "My name is John and I am SoftwareEngineer. "



 In this example, the dictionary object contains key-value pairs of words to be replaced and their replacements. The replaceWord function takes a sentence and the dictionary as input, and loops through the keys in the dictionary. For each key, it replaces all occurrences of that key in the sentence with its corresponding value in the dictionary. The modified sentence is returned as the output.

Comments