Archive for the ‘ Katas ’ Category

The Cube

You just watched the movie ’Cube’ before going to bed. Bad idea – you find yourself in a nightmare where you are also put in a huge cube which has 6 doors. But fortunately you remember an idea from the film about how each of the doors has a numeric code. If decoded correctly it tells you if there is a deadly trap behind it or not.

You read the first code 28733 13412 96476 – and remember that one had to add the digits of each group together (e.g. 2+8+7+3+3 = 23) and test if at least one of the 3 check sums is a prime number. If it is, there is a deadly trap!

Coding Kata

<!DOCTYPE html>
<html>
<head>
	<meta charset=utf-8 />
	<title></title>
	<link rel="stylesheet" type="text/css" media="screen" href="master.css" />
	<script type="text/javascript" src="jquery.min.js"></script>
	<!--[if IE]>
		<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
	<![endif]-->
	<script type="text/javascript">
		function isPrime(text){
			var e = text.split(',');
			for (var i=0; i<text.length; i++){
				var num = 0;
				for (pos = 0; pos < text[i].length; pos++) {
					num = num + parseInt(text[i].charAt (pos))
				}

				console.info("Testing %i, the sum of digits %s.", num, text[i]);

				if (num == 1 || num == 2) {
					console.info("%i, the sum of digits %s, is prime!.", num, text[i]);
					return true;
				}
				for (var divisor = 3; divisor < num; divisor++) {
					if (num % divisor == 0) {
					console.info("%i, the sum of digits %s, is prime!.", num, text[i]);
						return true;
					}
					else {
						console.info(num + ", the sum of digits " + text[i] + ", is not prime.  It is divisible by " + divisor + ".");
						break;
					}
				}
			}
			return false;
		}

		function isTrap(text){
			if(isPrime(text)){
				trap.play();
				return "It's a Trap!";
			}
		    return  "It's safe!";
		}
	</script>
</head>
<body>
	<form>
		<div class="box">
			<h1>Add and Prime</h1>
            <label>
            	<span>Please enter first number</span>
               	<input type="text" class="input_text" id="number1"/>
			</label>
			<label>
               	<span>Tada</span>
               	<input type="text" class="input_text" id="result1"/>
            <label>
				<input type="button" value="Calculate" onclick="$('#result1').val(isTrap($('#number1').val()))">
            </label>
 		</div>
		<audio id="trap" src="itsatrap.mp3">
			Your browser does not support the audio element.
		</audio>
</body>
</html>

Caesar plays Lottery

Write a method to convert a number between 1 and 50 to its Roman numeral equivalent.
(for example: 2 = ’II’, 10 = ’X’, 25 = ’XXV’, 50 = ’L’)

Coding Kata

<html>
<body>
<script type="text/javascript">
	function deromanize( roman ) {
	  var roman = roman.toUpperCase(),
		  lookup = {I:1,V:5,X:10,L:50,C:100,D:500,M:1000},
		  arabic = 0,
		  i = roman.length;
	  while (i--) {
		if ( lookup[roman[i]] < lookup[roman[i+1]] )
		  arabic -= lookup[roman[i]];
		else
		  arabic += lookup[roman[i]];
	  }
	  return arabic;
	}

	document.write(deromanize("VIII"));
</script>
</script></body>
</html>

NASA Countdown

The NASA wants to shoot yet another rocket to the orbit. As this is your first day as a wannabe scientist at the ground control, you are very excited. So excited that when the guy responsible for the countdown asks you to bring him some coffee, you accidentally spill it all over his lap and the computer – both are not capable of doing their job now.

But the liftoff is only seconds away. Somebody has to do the countdown – or the rocket can’t start and millions of dollars are lost! In a moment of great bravery you remember you learnt how to count (a long time ago in High School). So you take the chance!

The ground control chief will tell you where to start – then count to 0.
(example for ’11’: 11 10 9 8 7 6 5 4 3 2 1 0)

Coding Kata

<html>
<body>
<script type="text/javascript">
	function countdown(start){
		for (i=start; i>=0; i++){
			document.write("T-MINUS " + i + "<br />");
		}
	}

	countdown(20);
</script>
</body>
</html>

Tea Party

Your task is to welcome your guests properly: Some are female and some were knighted by the queen. So greet them correctly – or this will be your last hosting.

For example:
-Jane Austen is a women, so say Hello Ms. Austen
-George Orwell is a man, so say Hello Mr. Orwell
-Isaac Newton was knighted, so say Hello Sir Newton

Good luck with your party!
Coding Kata

<html>
<body>
<script type="text/javascript">
	function welcome(lastName, isFemale, isSir){
		if (isSir)
			return "Hello Sir " + lastName + "<br />";
		else if (isFemale)
			return "Hello Ms. " + lastName + "<br />";
		else
			return "Hello Mr. " + lastName + "<br />";
	}

	document.write(welcome("Austen", true, false));
	document.write(welcome("Orwell", false, false));
	document.write(welcome("Newton", false, true));
</script>
</body>
</html>

Fibonacci Killer

Calculate the missing Fibonacci numbers in order to predict where that murderer is gonna strike next!

Coding Kata

<script type="text/javascript">
   var runlimit = 12;
   var num1=0, num2=1, ans;
   function getNextFib(){
      ans = (num1 + num2);
      num1 = num2;
      num2 = ans;
      return ans;
   }
   function isPrime(num){
      for(x = num-1; x > 1; x--){
         if(ans % i == 0)
            return false;
         }
         return true;
      }
      for(i = 0; i < runlimit; i++){
         var x = getNextFib();
         document.write(x);
         if(isPrime(x))
            document.write(" is prime!");
            document.write("<br>");
        }
   }
</script>

Fuzz Buzz

Any number divisible by three is replaced by the word fizz and any divisible by five by the word buzz. Numbers divisible by both become fizzbuzz. A player who makes a mistake has to take a drink.
Einstein will choose a random number to start with – for example: 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz…

Coding Kata

<script type="text/javascript">
   var i=0;
   for (i=0;i< =100;i++) {
      if (i%3 == 0 && i%5 == 0) {
         document.write("The number is Fuzz Buzz");
         document.write("<br />");
      } else
      if (i%3 == 0) {
         document.write("The number is Fuzz");
         document.write("<br />");
      } else if (i%5 == 0) {
         document.write("The number is Buzz");
         document.write("<br />");
      } else {
         document.write("The number is " + i);
         document.write("<br />");
      }
   }
</script>