Skip to content Skip to sidebar Skip to footer

Java Script Problem With Initial Display Of Str.replaceall

I was given this code for a moving window self-paced reading experiment where the initial display is a sentence string covered by dashes (separated by word) and the display of regi

Solution 1:

Wow, TLDR the comments. Here's how I'd do it:

functionreplaceWithDash(input,partNumber) {
    //First, lets check if partNumber was omitted, since we can use this case//to replace a single part as wellif(typeof partNumber!="number")
        //Just remove the commas and replace every letter with the dashreturn input.replace(/,/g,"").replace(/[^\s]/g,"-");

    //If partNumber was specified (the function didn't end on the previous return), lets split the sentence
    parts=input.split(",");
    //remove the partNumber-th element and replace it with its dashed version
    parts.splice(partNumber,1,replaceWithdash(parts[partNumber]));
    //then join everything again and fix repeating spaces (it's quicker to do this once here than trimming each element)return parts.join(" ").replace(/[\s]+/g," ");
}

Note that I fixed the function name.

Post a Comment for "Java Script Problem With Initial Display Of Str.replaceall"