Room 51

Get Programming with JavaScript - Listing 19.06

Listing 19.06 - Using a while loop with replace

var template = "Using a while loop with replace by . Follow "; var filled = template; console.log("Starting replacement..."); while (filled.indexOf("") !== -1) { filled = filled.replace("", "Oskar"); console.log(filled); } console.log("...replacement finished.");

Further Adventures

Listing 19.06 - Using a while loop with replace - Task 2

function replace(template, placeholder, data) { var filled = template; while (filled.indexOf(placeholder) !== -1) { filled = filled.replace(placeholder, data); } } // test the function var template = "Using a while loop with replace by . Follow "; console.log("Starting replacement..."); console.log(replace(template, "", "Oskar")); console.log("...replacement finished.");