<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users616</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/616" rel="self"/>
  <title>Jermaine</title>
  <updated>Mon Apr 19 14:17:08 -0700 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1268</id>
    <published>2010-04-19T14:17:08-07:00</published>
    <updated>2010-04-23T12:49:17-07:00</updated>
    <title>[JavaScript] Working jQuery but a bit cluttered...</title>
    <content type="html">&lt;p&gt;Hi Everyone, &lt;/p&gt;

&lt;p&gt;I'm changing the time by 1 hour in the second selectbox, based on what's selected in the first selectbox. &lt;/p&gt;

&lt;p&gt;The code is working perfectly fine, you can paste it in your texteditor and fire it up in your webbrowser to see how it works. &lt;/p&gt;

&lt;p&gt;The code is just a bit cluttered. So any (better) suggestions are welcome...&lt;/p&gt;

&lt;pre&gt;[html]

&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;script src=&amp;quot;http://code.jquery.com/jquery-latest.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;select id=&amp;quot;mySelect&amp;quot;&amp;gt;
  &amp;lt;option value=&amp;quot;8&amp;quot;&amp;gt;08:00&amp;lt;/option&amp;gt; 
  &amp;lt;option value=&amp;quot;9&amp;quot;&amp;gt;09:00&amp;lt;/option&amp;gt; 
  &amp;lt;option value=&amp;quot;10&amp;quot;&amp;gt;10:00&amp;lt;/option&amp;gt;
&amp;lt;/select&amp;gt; 


&amp;lt;select id=&amp;quot;mySelect_2&amp;quot;&amp;gt;
  &amp;lt;option value=&amp;quot;8&amp;quot;&amp;gt;08:00&amp;lt;/option&amp;gt; 
  &amp;lt;option value=&amp;quot;9&amp;quot;&amp;gt;09:00&amp;lt;/option&amp;gt; 
  &amp;lt;option value=&amp;quot;10&amp;quot;&amp;gt;10:00&amp;lt;/option&amp;gt;
&amp;lt;/select&amp;gt; 


&amp;lt;script&amp;gt;

$(&amp;quot;#mySelect&amp;quot;).change(function(){
    var selected = $(this).attr(&amp;quot;selectedIndex&amp;quot;);
    var max_select_box = $(&amp;quot;#mySelect option&amp;quot;).length -1;
  
      if (selected == max_select_box) { 
           $(&amp;quot;#mySelect2&amp;quot;).attr(&amp;quot;selectedIndex&amp;quot;, selected);
      } else {
           $(&amp;quot;#mySelect2&amp;quot;).attr(&amp;quot;selectedIndex&amp;quot;, selected + 1);
      }
  
      $(&amp;quot;#mySelect_2&amp;quot;).change(function(){
      var selected = $(this).attr(&amp;quot;selectedIndex&amp;quot;);
      var currently_selected = $(&amp;quot;#mySelect&amp;quot;).attr(&amp;quot;selectedIndex&amp;quot;);

  
      if (selected &amp;lt; currently_selected) { 
           $(&amp;quot;#mySelect&amp;quot;).attr(&amp;quot;selectedIndex&amp;quot;, selected);
      }
  });
});

&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Jermaine</name>
      <email>jermaine@dualminded.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1268-working-jquery-but-a-bit-cluttered" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1017</id>
    <published>2009-09-01T11:02:53-07:00</published>
    <updated>2009-09-01T15:17:25-07:00</updated>
    <title>[JavaScript] Port Prototype Code to jQuery?</title>
    <content type="html">&lt;p&gt;Hi All, &lt;/p&gt;

&lt;p&gt;I want to refactor (port) the following PrototypeJS code to jQuery. I'm in the process of learning jQuery and currently stuck with porting this piece of code... Any idea's? &lt;/p&gt;

&lt;pre&gt;## PrototypeJS javascript code [javascript]

var total = 0;

// For each fieldpair that has a child with a checked input
// we get the sibling with a text input and add that value
// to the total. 
$$(&amp;quot;.fieldpair&amp;quot;).each(function(fieldpair) {
   if (fieldpair.down(&amp;quot;input:checked&amp;quot;)) {
     var input = fieldpair.down('input:text');
     total += parseInt(input.value);		 
  }
});&lt;/pre&gt;</content>
    <author>
      <name>Jermaine</name>
      <email>jermaine@dualminded.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1017-port-prototype-code-to-jquery" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor278263</id>
    <published>2009-08-26T18:48:34-07:00</published>
    <title>[JavaScript] On Better way in Prototype.js?</title>
    <content type="html">&lt;p&gt;Hi North, &lt;/p&gt;

&lt;p&gt;JQuery would be fine as well....&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Jermaine</name>
      <email>jermaine@dualminded.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/943-better-way-in-prototype-js/refactors/278263" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code943</id>
    <published>2009-07-02T20:59:17-07:00</published>
    <updated>2009-10-23T01:05:33-07:00</updated>
    <title>[JavaScript] Better way in Prototype.js?</title>
    <content type="html">&lt;p&gt;Hi Everyone, &lt;/p&gt;

&lt;p&gt;I've got this small piece of code that basically toggles a element but changes the hyperlink's name as well. &lt;/p&gt;

&lt;p&gt;In this case if I would click on the &amp;quot;Show&amp;quot; button, it will display the content div, but it will also change the text from &amp;quot;Show&amp;quot; to &amp;quot;Hide&amp;quot;. &lt;/p&gt;

&lt;p&gt;Now it all works fine, but I think it looks ugly. I'm not really proficient with javascript, but I'm looking for a better (Unobtrusive) way to do this in prototype.
&lt;br /&gt;Any ideas?&lt;/p&gt;

&lt;p&gt;Many thanks in advance.&lt;/p&gt;

&lt;p&gt;Greets, &lt;/p&gt;

&lt;p&gt;- Jermaine&lt;/p&gt;

&lt;pre&gt;&amp;lt;script language=&amp;quot;javascript&amp;quot;&amp;gt;
function toggleLinkAndElement (link, elementID, text) {

 Element.toggle(elementID); // toggle the element
      if (link.innerHTML != text )
	  {
	    // change link text
	    link.oldInnerHTML = link.innerHTML;
	    link.innerHTML = text;    
	  }
	  else
	  {
	   // change it back
	    link.innerHTML = link.oldInnerHTML;
	  }
}
&amp;lt;/script&amp;gt;
&amp;lt;div id=&amp;quot;content&amp;quot; style=&amp;quot;display:none&amp;quot;&amp;gt;
        &amp;lt;p&amp;gt;HELLO WORLD!&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;a href=&amp;quot;#&amp;quot; onclick=&amp;quot;toggleLinkAndElement(this, 'content', 'Hide'); return false;&amp;quot;&amp;gt;Show&amp;lt;/a&amp;gt;
&lt;/pre&gt;</content>
    <author>
      <name>Jermaine</name>
      <email>jermaine@dualminded.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/943-better-way-in-prototype-js" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code776</id>
    <published>2009-03-04T18:22:35-08:00</published>
    <updated>2009-03-07T00:26:03-08:00</updated>
    <title>[Ruby] Working ruby code, but messy ....</title>
    <content type="html">&lt;p&gt;Hi Everyone, &lt;/p&gt;

&lt;p&gt;I've created a ruby script that checks several (fake/local) websites whether it's up-to-date or not by just doing a comparison on the date listed on the site.&lt;/p&gt;

&lt;p&gt;The condition is: each site must not be older then 1 day.&lt;/p&gt;

&lt;p&gt;If however it's a &amp;quot;special&amp;quot; site (by simply looking if the text &amp;quot;special&amp;quot; is present on the site) then it shouldn't be older then 7 days.&lt;/p&gt;

&lt;p&gt;Now the thing is, I've created a ruby script to check all of this, and it works perfectly fine though. However I really don't like the way the code looks. Also I think it's quite a bit slow, I believe it can be much faster if the code is well optimized.&lt;/p&gt;

&lt;p&gt;As you can see in my script below it does the following things: &lt;/p&gt;

&lt;p&gt;1. It does two regular expressions, one for the date that is on the site, and one to see wheater it's a &amp;quot;special&amp;quot; site of not.
&lt;br /&gt;2. It compares the date on the site with the current date (Date.today)
&lt;br /&gt;3. And some If and else statements to see if a site is up-to-date.&lt;/p&gt;

&lt;p&gt;Any idea how to optimize this? Any suggestions will be very appreciated.&lt;/p&gt;

&lt;p&gt;Many thanks in advance....&lt;/p&gt;

&lt;pre&gt;#!/usr/bin/ruby
puts &amp;quot;Content-Type: text/html\n\n&amp;quot;

require 'net/http'
require 'uri'
require 'open-uri'
require 'date'

@@begin_time = Time.now

def check_urls(url, iterate)
    
    puts &amp;quot;&amp;lt;h3&amp;gt;Checking URLS&amp;lt;/h3&amp;gt;&amp;quot;
    puts &amp;quot;&amp;lt;ul&amp;gt;&amp;quot;
    iterate.times do |i|
    uri_output = Net::HTTP.get(URI.parse(url[i])).scan(/\d{2}\.\d{2}\.\d{4}/)[0]
    site_kind = Net::HTTP.get(URI.parse(url[i])).include? &amp;quot;SPECIAL&amp;quot;

    if uri_output == nil
      puts &amp;quot;&amp;lt;li&amp;gt;[NOTHING FOUND] &amp;lt;a href='&amp;quot; + url[i] + &amp;quot;' target='_blank'&amp;gt;&amp;quot; + url[i] + &amp;quot;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&amp;quot;
    else

    site_date = Date.strptime(uri_output, &amp;quot;%d.%m.%Y&amp;quot;)
    	 current_date = Date.today
    	 difference = current_date - chart_date

    if site_kind == true &amp;amp;&amp;amp; difference &amp;gt; 7
  	  puts &amp;quot;&amp;lt;li&amp;gt;[NOT UPDATED] #{site_date} &amp;lt;a href='&amp;quot; + url[i] + &amp;quot;' target='_blank'&amp;gt;&amp;quot; + url[i] + &amp;quot;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&amp;quot;
    else
    if site_kind == false &amp;amp;&amp;amp; difference &amp;gt; 1
  	  puts &amp;quot;&amp;lt;li&amp;gt;[NOT UPDATED] #{site_date} &amp;lt;a href='&amp;quot; + url[i] + &amp;quot;' target='_blank'&amp;gt;&amp;quot; + url[i] + &amp;quot;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&amp;quot;
    else
  	  puts &amp;quot;&amp;lt;li&amp;gt;[OK] #{site_date} &amp;lt;a href='&amp;quot; + url[i] + &amp;quot;' target='_blank'&amp;gt;&amp;quot; + url[i] + &amp;quot;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;&amp;quot;
    end
  end
  end
  end
    puts &amp;quot;&amp;lt;/ul&amp;gt;&amp;quot;
    
    puts &amp;quot;&amp;lt;/ul&amp;gt; &amp;lt;strong&amp;gt;ALL DONE&amp;lt;/strong&amp;gt;&amp;quot;
    executed_time = Time.now - @@begin_time
    to_minutes(executed_time)
end

def to_minutes(seconds)
  min = (seconds/60).floor
  sec = (seconds - (min * 60)).round
 
  if min &amp;lt; 10
   min = &amp;quot;0#{min}&amp;quot;
  end
  
  if sec &amp;lt; 10
   sec = &amp;quot;0#{sec}&amp;quot;
   end
   
  puts &amp;quot;in &amp;lt;strong&amp;gt;#{min}:#{sec}&amp;lt;/strong&amp;gt;&amp;quot;
end

websites = IO.read('websites.txt').split
number_of_lines = websites.length
check_urls(websites, number_of_lines)

&lt;/pre&gt;</content>
    <author>
      <name>Jermaine</name>
      <email>jermaine@dualminded.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/776-working-ruby-code-but-messy" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13704</id>
    <published>2008-07-24T06:50:59-07:00</published>
    <title>[JavaScript] On countdown timer (minutes &amp; seconds) javascript</title>
    <content type="html">&lt;p&gt;Guys, this helped me alot. Thanks so much!&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Jermaine</name>
      <email>jermaine@dualminded.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/302-countdown-timer-minutes-seconds-javascript/refactors/13704" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code302</id>
    <published>2008-05-27T09:57:58-07:00</published>
    <updated>2012-02-07T07:05:31-08:00</updated>
    <title>[JavaScript] countdown timer (minutes &amp; seconds) javascript</title>
    <content type="html">&lt;p&gt;Hello Everyone.
&lt;br /&gt;I'm trying to create a countdown timer in javascript but it's a bit cluttered. Ideally I would like it to be unobtrusive in Prototype.
&lt;/p&gt;

&lt;pre&gt;&amp;lt;div id=&amp;quot;countdown&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div id=&amp;quot;notifier&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;

&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;

  var start = 20;
  Number.prototype.toMinutesAndSeconds = function() {
	var nbr = Math.floor(this / 60);
	return (nbr+&amp;quot;:&amp;quot;)+(((nbr=(this-(nbr*60)))&amp;lt;10)?&amp;quot;0&amp;quot;+nbr:nbr);
}

function display(seconds, output) {
	output.innerHTML = (--seconds).toMinutesAndSeconds();
	if(seconds &amp;gt; 0) {
		window.setTimeout(function(){display(seconds, output)}, 1000);
	}
	
	if(seconds &amp;lt; 11) {
		document.getElementById(&amp;quot;notifier&amp;quot;).innerHTML = &amp;quot;Just 10 seconds to go&amp;quot;;
	}
	
	if (seconds == 0) {
		document.getElementById(&amp;quot;notifier&amp;quot;).innerHTML = &amp;quot;Time is up baby&amp;quot;;
	}
}
display(start, document.getElementById(&amp;quot;countdown&amp;quot;));

&amp;lt;/script&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Jermaine</name>
      <email>jermaine@dualminded.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/302-countdown-timer-minutes-seconds-javascript" rel="alternate"/>
  </entry>
</feed>

