thescientist Posted September 24, 2010 Share Posted September 24, 2010 BTW, scientist, you fail. :)Yours creates an infinite loop of print dialog popups. It should look like this:for(var i = 1, x = 20, z = ""; i < x; i++){ document.write( (z += i.toString())+"<br />" );}; right, but an ending conditional was never provided in the "challenge" post. Here's a more easier question :)With 2 for loops generate this pattern11212312341234512345612345671234567812345678912345678910...so onAs far as the print statement, in my dev environment it just writes to the console. I forget to take into account the browser environment, but proof of concept seemed to suffice in this case although I do realize it was technically asked to be done with document.write. Link to comment Share on other sites More sharing options...
ShadowMage Posted September 24, 2010 Share Posted September 24, 2010 Shadow, your entry in the "Title" contest does seem to do the trick. Interesting how my version (not yet complete) takes a very different approach. A lot of your code fixes the hyphen problem, which I have not tackled yet.Hmm....I'd be interested to see what your version looks like. Guess I'll have to wait until next week! @scientistYea, I guess you're right on the ending conditional, but still..... Link to comment Share on other sites More sharing options...
jeffman Posted September 24, 2010 Share Posted September 24, 2010 Hint: massive global regexes and str.replace . I start with the big array same as you, but instead of looping through it, I join it into a string delimited by the regex | operator, which I thought was pretty clever. Benchmarking would say which technique would be faster after a million iterations. . . . Link to comment Share on other sites More sharing options...
ShadowMage Posted September 24, 2010 Share Posted September 24, 2010 Hint: massive global regexes and str.replace . I start with the big array same as you, but instead of looping through it, I join it into a string delimited by the regex | operator, which I thought was pretty clever. Benchmarking would say which technique would be faster after a million iterations. . . .That is rather clever. Link to comment Share on other sites More sharing options...
ShadowMage Posted September 24, 2010 Share Posted September 24, 2010 Benchmarking would say which technique would be faster after a million iterations. . . .FWIW, after 10,000 iterations mine benchmarks as follows:3.6923.723.7543.7213.683.6883.8633.7163.7543.705I ran the script ten times. Each of the above numbers is the number of seconds it took to run in FF 3.6.10 Link to comment Share on other sites More sharing options...
scout1idf Posted September 26, 2010 Share Posted September 26, 2010 Here's a more easier question :PWith 2 for loops generate this pattern11212312341234512345612345671234567812345678912345678910...so onLeast amount of statements win. using only PHP(echo) or Javascript(write). Well done That's exactly what I have except mine is PHP for ($i = 0; $i <= 900; $i++) { for ($j = 1; $j <= $i+1; $j++){ echo $j; } echo "<br />";}Lol 900.Just trying to be funny here....Taking the original challenge literally, the code should be....for ($i = 0; $i <= 9; $i++) { for ($j = 1; $j <= $i+1; $j++) { echo $j. "Â "; } echo "<br />";} echo "....so on"; :) :) Link to comment Share on other sites More sharing options...
jeffman Posted September 27, 2010 Share Posted September 27, 2010 Okay. Time for the "Title" contest has expired, with only ONE entry. OK. We'll try something simpler next time.I did not thoroughly test Shadow's code, but it does seem to work. Here is my own version, which also works.In order to clarify the number of actual statements, I have departed from normal coding practices in a few places.And I'm using an annoying codebox so the big array of words not to be uppercased doesn't fill up the whole screen.I keep thinking all the calls to string.replace are somehow unnecessary, but I didn't want to waste time puzzling itout any more than I did. Anyway: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <title></title> <script type="text/javascript"> function myUpperCase () { var val = arguments[0].toUpperCase(); return val; } function myLowerCase () { var val = arguments[0].toLowerCase(); return val; } function convertToTitle(str) { var lowerCaseWords = ["a","an","the","and","or","but","yet","for","nor","aboard","about","above","absent","across","after","against","along","alongside","amid","amidst","among","amongst","around","as","aside","astride","at","athwart","atop","barring","before","behind","below","beneath","beside","besides","between","betwixt","beyond","but","by","circa","despite","down","during","except","for","from","given","in","inside","into","like","mid","midst","minus","near","next","notwithstanding","of","off","on","onto","out","outside","over","pace","per","plus","pro","qua","round","save","since","than","through","throughout","till","to","toward","towards","under","underneath","unlike","until","up","upon","versus","via","with","within","without"]; var val = lowerCaseWords.join("|"); var lowerCaseWordsRE = new RegExp("\\b(" + val + ")\\b", "gi"); str = str.replace(/\s+/g, " "); str = str.replace(/^ | $/g, ""); str = str.toLowerCase(); str = str.replace(/\b./gi, myUpperCase); str = str.replace(lowerCaseWordsRE, myLowerCase); str = str.replace(/^.|\: ./gi, myUpperCase); str = str.replace(/( )([\w\d])([\w\d]*$)/i, function () { var val = arguments[2].toUpperCase(); val = arguments[1] + val + arguments[3]; return val; } ); return str; } </script> </head> <body> <textarea id="txt" cols="80"> into the mind of a do-nothing: the life and times of wiLLiam st. nor"</textarea> <input type="button" onclick="document.getElementById('output').innerHTML=convertToTitle(document.getElementById('txt').value)" value="Convert" style="display:block"> <h3 id="output" style="font-family:Verdana, sans-serif">Your Title</h3> </body></html> Link to comment Share on other sites More sharing options...
ShadowMage Posted September 27, 2010 Share Posted September 27, 2010 Mine is almost double the number of statements! 30 vs 18I knew I didn't stand a chance though. Not against an expert like you! Link to comment Share on other sites More sharing options...
jeffman Posted September 27, 2010 Share Posted September 27, 2010 Regular expressions and the ability to pass callback functions to string.replace helped a lot. And I'm still a regex beginner. The real experts write stuff I can't begin to understand. Link to comment Share on other sites More sharing options...
ShadowMage Posted September 27, 2010 Share Posted September 27, 2010 ...the ability to pass callback functions to string.replace helped a lot.I didn't even know that was possible....I mean I've worked with callback functions and stuff but I didn't know you could use one with string.replace like you did.And I'm still a regex beginner. The real experts write stuff I can't begin to understand. Yeah, regex's are quite confounding. Not so long ago I didn't even know what a regex was. It looked like a cat walked across the keyboard.....I've worked with them a lot lately and I've become somewhat skilled in their usage, though I would still classify myself as a beginner too. Link to comment Share on other sites More sharing options...
Synook Posted September 29, 2010 Share Posted September 29, 2010 Posts for the Sieve of Erastosthenes contest have been split to a new thread: http://w3schools.invisionzone.com/index.php?showtopic=33718. Link to comment Share on other sites More sharing options...
ShadowMage Posted September 29, 2010 Share Posted September 29, 2010 Nested calls is exactly what I'm talking about. Each call is a statement.If we didn't do that, we would have to resort to some serious benchmarking. I personally don't run PHP on my desktop, so I am coding all this in JavaScript. (PHP would simplfiy a few things, so maybe I'll migrate.) So now we have to benchmark on 3-4 platforms, 4-5 browsers, and an untold number of processors. And even if we did use server-side environments, we still have the matter of which environment, which platform and which hardware being used.Counting statements makes for a much friendlier (and more manageable) contest, I think.Well, when you put it that way, yeah it does make for a friendlier, more manageable contest. So, rules change! Least amount of statements wins.EDIT: Well, Synook, can you move this one too. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.