while doing a code clash on codingames, in which we had to replace a character in a string one by one to transform it into a target string, printing out every itteration of the new string, resulting in something like this:
"Turtle Ham"
"Hurtle Ham"
"Hartle Ham"
"Hamtle Ham"
"Ham le Ham"
"Ham Me Ham"
"Ham Ma Ham"
"Ham MakHam"
"Ham Makeam"
"Ham Makerm"
"Ham Maker!"
This is de code I came up with, in Javascript.
var source = "Turtle Ham";
var target = "Ham Maker!";
var safe = source;
String.prototype.replaceAt=function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
}
console.log(source);
for(var i=0; i < source.length; i++) {
source = source.replaceAt(i, target.charAt(i));
if(source == safe) {
} else {
console.log(source);
safe = source;
}
}
would there be any more effective way of doing this in Javascript?
1 Answer 1
As Tyler Flynn pointed out in a comment, you should flip the conditional from "equals" to "not equals" to avoid the (current empty)
if
branch:if(source != safe) { console.log(source); safe = source; }
I'd avoid extending the
String
prototype. The method you're adding is pretty generic, and could be reusable, so it's not a terrible thing to add, but still. Extending prototypes is generally considered bad form - not because it'll instantly break anything, but rather because if everybody did it, they might definedreplaceAt
in some other way, and break your code. Or your extension would break their code. So extending prototypes, while often tempting, should be considered carefully.
In this case, it'd be just as simple to have areplaceAt(string, index, character)
function that you call with the string, instead of calling it on the string.
Other than that, you code seems OK. There are a ton of other approaches you could take, of course, but without some external metric to judge by, yours is as good as any other.
However, there's one issue which irks me a little: source
is mutated. With your current code, you can't print the original string after the loop has run, because you've "lost" it - source just becomes equal to target. I'd keep source and target unaltered, and generate a third "result" string for each step instead.
This also points to an alternative approach: Instead of replacing characters, create a new string by glueing parts of the two others together.
var source = "Turtle Ham";
var target = "Ham Maker!";
var lastResult = null;
for(var i = 0; i <= source.length ; i++) {
var result = target.substr(0, i) + source.substr(i);
if(result != lastResult) {
console.log(result);
}
lastResult = result;
}
This code doesn't even need a separate function or prototype extension, and doesn't mutate the input (source and target) strings. It also doesn't need an extra console.log
to show the original; everything happens in the loop, which runs from 0 characters replaced to source.length
(inclusive) characters replaced.
source.length
, then fill each position with a random number from0
tosource.length - 1
, then enumerate that array instead and replace the char atarray[i]
position instead ofi
position. ;) \$\endgroup\$if(source == safe) {} else {...
Justif(source != safe) {...
. \$\endgroup\$