Maths and Computing
Home
Books
Mathematics
Binary
Computing
Coding
Information
Maths and Computing JavaScript Examples
This tool will run small JavaScript programs. It is essentially intended for use with the book "An Introduction to Mathematics for Computing and IT Practitioners". Click an entry on the right to select one of the book's code examples.
1.3.2: Working out the triangular numbers
2.3.1: Storing a vector in an array
2.3.2: Storing a matrix in an array
3.2.3: Multiplying two 2 by 2 matrices
5.3.2: Solving a simultaneous equation
6.4.2: Finding the minimum spanning tree
7.5.1: Calculating the nth term of a given sequence V1
7.5.2: Calculating the nth term of a given sequence V2
8.2.2: Quick Sort
9.6.3: Simulation Scenario 1
9.6.4: Simulation Scenario 2
11.1.5: Converting a decimal number to binary
11.5.3: Converting binary numbers to Gray code
13.4.3: Converting an 8-bit character to MIME Base 64
15.1.2: Calculating a sequence of prime numbers
15.2.3: Euclids Algorithm
15.3.2: Calculating the square root of a binary number
15.5.2: Working out the day of the week
<html>
<body>
<script type ="text/javascript"> <title>
Converting an 8-bit character to MIME Base 64
</title>
</head>
<body>
<script type ="text/javascript">
// The array of MIME values M=new Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"); // Function to convert the string txt to MIME function toMime(txt) { p=0; remainder=0; mime_string=""; // Loop until there are no more characters left. for(i=0;i
0) current_value=(remainder<<8) + txt.charCodeAt(i++); else current_value=remainder; // Extra the bits to the right of current_value remainder=current_value & (Math.pow(2,p)-1); // Extra the six bits to the left of current_value six_bit_value=current_value >> p; // Look up the mime_value mime_string=mime_string.concat(M[six_bit_value]); } // Make adjustments to the end of the MIME string if(p==2) { mime_string=mime_string.concat(M[remainder << 4],"=="); } if(p==4) { mime_string=mime_string.concat(M[remainder << 2],"="); } if(p==6) { mime_string=mime_string.concat(M[remainder]); } return(mime_string); } document.writeln(toMime("Hello World") + "
");
</script>
</body>
</html>