Programming Challenge ( JavaScript )

Kirk Cerny kirksemail at gmail.com
Fri Sep 28 09:36:59 MDT 2007


Nobody had submitted Javascript so here goes.

<html>
	<head>
		<title>JavaScript Seconds</title>
		<script type="text/javascript" >

			String.prototype.f = function ()
			{
				if (this.length === 0) {
					return 0;
				} else if (this.toString() === "00") {
					return "00"
				} else {
				return this.replace(/^0+/,"");
				}
			};


			var parseTime = function ()
			{
				var timeStringId = "timeStringId";
				var timeStringInput = document.getElementById(timeStringId);
				var timeString = timeStringInput.value;
				var timeArray = [];

				var regex = /(\d+)?:?(\d+)?:?(\d+)(\.\d+)?\s*([\+\-\*\\])?\s*/g;

				var mul1 = '3600';
				var mul2 = '60';
				timeString = timeString.replace(regex, 'if("$3".length === 1)
{mul1 = 60; mul2 = 10;} timeArray.push(String.concat(Number("$1".f() *
mul1), "+", Number("$2".f() * mul2), "+", Number("$3".f()), "+",
Number("$4".f()), "$5" ));');

				//alert(timeString);

				eval(timeString);
				timeString = timeArray.join(' ');
				alert(timeString);

			};


		</script>
	</head>
	<body>

		<form>
		<input id="timeStringId" type="" name="" value="1:00:00" />
		<input id="timeStringButtonId" type="button" value="Go"
onclick="parseTime();" />
		</form>

	</body>
</html>



More information about the PLUG mailing list