It's an April fools prank that replaces some words with others, but it's not working!
This is the code:
function replace_words(words){ for(word in words){ document.innerHTML= document.innerHTML.replace(word, words[word]); } } replace_words({ "gangnam style" : "style", " style" : "gangnam style" "YouTube" : "<a href='http://www.scratch.mit.edu/ext/youtube/?v=mIQToVqDMb8'>YouTube</a>", "open" : "oppan", "Easter" : "CHOCOLATE!!!!!!!!!!!!!!!!!!!!!!", "poke" : "pok\u00E9mon", "April" : "April Fools!", "beautiful" : "pooey", "9" = "hykgfsd", // To prevent immediate replacement with 1 "8" = "9", "7" = "8", "6" = "7", "5" = "6", "4" = "5", "3" = "4", "2" = "3", "1" = "2", "0" = "1", "hykgfsd" = "0", "+" = "ksjdfhsdklthfdfkjug", //Prevents immediate replacement with + "-" = "+", "ksjdfhsdklthfdfkjug" = "+", "computer" : "toaster", )};
I I need it working before tomorrow.
Can someone help please?
Offline
Line 3: It should be document.body.innerHTML in both places.
Also, starting with "9"="…", you use the = sign. You need to use the : (colon) like you did above.
The last line should be }), not )}.
Finally, to replace all of the words, not just the first occurrence, you need to use:
.replace(new RegExp(word, "g"), words[word])
So "cat cat".replace("cat", dog) only does "dog cat" right now.
Last edited by Hardmath123 (2013-03-31 08:07:20)
Offline
OK, thanks, I did those thing but it's still not working. I looked in the console log and found this:
Uncaught SyntaxError: Unexpected string chrome-extension://nfannpdpemeggobgadhmiplhnhdbeoio/script.js:11
I'm assuming that means there's an error on line 11 which is :
"open" : "oppan",
I tried escaping the quote marks in the link on line 10 because i thought that might be the error, however I still get the same message
Any suggestions please?
Here's the updated code:
function replace_words(words){ for(word in words){ document.body.innerHTML= document.body.innerHTML.replace(new RegExp(word, "g"), words[word]); } } replace_words({ "gangnam style" : "style", " style" : "gangnam style" "YouTube" : "<a href=\'http://www.scratch.mit.edu/ext/youtube/?v=mIQToVqDMb8\'>YouTube</a>", "open" : "oppan", "Easter" : "CHOCOLATE!!!!!!!!!!!!!!!!!!!!!!", "poke" : "pok\u00E9mon", "April" : "April Fools!", "beautiful" : "pooey", "9" : "hykgfsd", // To prevent imediate replacement with 1 "8" : "9", "7" : "8", "6" : "7", "5" : "6", "4" : "5", "3" : "4", "2" : "3", "1" : "2", "0" : "1", "hykgfsd" : "0", "+" : "ksjdfhsdklthfdfkjug", //Prevents immediate replacement with + "-" : "+", "ksjdfhsdklthfdfkjug" : "+", "computer" : "toaster", });
Offline
function replace_words(words){ for(word in words){ document.body.innerHTML= document.body.innerHTML.replace(new RegExp(word, "g"), words[word]); } } replace_words({ "gangnam style" : "style", " style" : "gangnam style", "YouTube" : "<a href=\'http://www.scratch.mit.edu/ext/youtube/?v=mIQToVqDMb8\'>YouTube</a>", "open" : "oppan", "Easter" : "CHOCOLATE!!!!!!!!!!!!!!!!!!!!!!", "poke" : "pok\u00E9mon", "April" : "April Fools!", "beautiful" : "pooey", "9" : "hykgfsd", // To prevent imediate replacement with 1 "8" : "9", "7" : "8", "6" : "7", "5" : "6", "4" : "5", "3" : "4", "2" : "3", "1" : "2", "0" : "1", "hykgfsd" : "0", "+" : "ksjdfhsdklthfdfkjug", //Prevents immediate replacement with + "-" : "+", "ksjdfhsdklthfdfkjug" : "+", "computer" : "toaster", });
Offline
Yay! it works!!! Thanks, guys!
However, 4 little bugs:
- All numbers are turned into 0 instead of the intended purpose of making them all one higher (although this is still a good prank)
- + and - are not swapped
- Computer does not turn into toaster
- It is case sensitive.
Last edited by joefarebrother (2013-03-31 10:30:26)
Offline
For case-sensitivity, read up on RegExps.
For numbers turning to 0, think about your code for a minute. Say you're replacing 8. First, you replace it with 7. Then, you replace all 7s with 6s, so your 8 is now a 6. This goes on until you have 0 for all numbers.
+- is broken because you are converting + to gibberish and then the gibberish back to +. The "sdaipof":"+" line should be "sadlfjhk":"-".
Offline
Yay! It all works now!
Thanks!
Here's the whole code in case someone else wants to make a prank! Just change the words in the object!
function replace_words(words){ for(word in words){ document.body.innerHTML= document.body.innerHTML.replace(make_regexp(word), words[word]); } } function make_regexp(word){ var str = ""; for(i=0; i < word.length; ++i){ str += "[" + word.charAt(i).toLowerCase() + word.charAt(i).toUpperCase() + "]"; } return new RegExp(str, "g"); } replace_words({ "gangnam style" : "style", " style" : " gangnam style", "YouTube" : "<a href=\'http://www.scratch.mit.edu/ext/youtube/?v=mIQToVqDMb8\'>YouTube</a>", "open" : "oppan", "Easter" : "CHOCOLATE!!!!!!!!!!!!!!!!!!!!!!", "poke" : "pok\u00E9mon", "April" : "April Fools!", "beautiful" : "pooey", "9" : "hykgfsd", // To prevent imediate replacement with 1 "8" : "9", "7" : "8", "6" : "7", "5" : "6", "4" : "5", "3" : "4", "2" : "3", "1" : "2", "0" : "1", "hykgfsd" : "0", "+" : "ksjdfhsdklthfdfkjug", //Prevents immediate replacement with + "-" : "+", "ksjdfhsdklthfdfkjug" : "-", "computer" : "toaster", });
Reporting to be closed...
Last edited by joefarebrother (2013-03-31 12:40:43)
Offline
Closed by request of the topic owner.
Note: We've had a report that running this script can mess up your browser - so use at your own risk
Offline