寝る前にもうひとつ

20分くらいかかったかな・・・

クラス化を試してみた。
こんな感じであってるのかな。

元ネタtopcoderの道1 | プログラミングに自信があるやつこい!!

function CCipher()
{
	this.alphabet = ["A","B","C","D","E","F"
	            ,"G","H","I","J","K","L"
	            ,"L","M","N","O","P","Q"
	            ,"R","S","T","U","V","W"
	            ,"X","Y","Z"
	            ];
}

CCipher.prototype.decode = function(str, num){
	var result = "";
	for(var i =0; i < str.length; i++)
	{
		var index = this.getAlphabetIndex(str.charAt(i));
		
		result += this.alphabet[this.getShiftIndex(index, num)];
	}
	
	return result;
};

CCipher.prototype.getAlphabetIndex = function(char)
{
	for(var i = 0; i < this.alphabet.length; i++)
	{
		if(char == this.alphabet[i])
		{
			return i;
		}
	}
};

CCipher.prototype.getShiftIndex = function(index, num)
{
	var tmp = index - num;
	
	if(tmp < 0)
	{
		return this.alphabet.length - Math.abs(tmp);
	}
	else
	{
		return tmp;
	}
};