Skip to content

Instantly share code, notes, and snippets.

View bogoreh's full-sized avatar
🎯
Focusing

bogoreh

🎯
Focusing
View GitHub Profile
//swapping the values that are passed to this function
var swap = function(array, firstIndex, secondIndex) {
var temp = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = temp;
};
//declaring testArray and assigning it numbers
var testArray = [7, 9, 4];
var indexOfMinimum = function(array, startIndex) {
// Set initial values for minValue and minIndex,
// based on the leftmost entry in the subarray:
var minValue = array[startIndex];
var minIndex = startIndex;
// Loop over items starting with startIndex,
// updating minValue and minIndex as needed:
for (var i=minIndex+1; i<array.length; i++){
//this function swaps the two values
var swap = function(array, firstIndex, secondIndex) {
var temp = array[firstIndex];
array[firstIndex] = array[secondIndex];
array[secondIndex] = temp;
};
//this function checks to see if there is smaller value in the array
var indexOfMinimum = function(array, startIndex) {
var insert = function(array, rightIndex, value) {
var i;
for (i = rightIndex; i >= 0 && array[i] > value; i--){
array[i+1] = array[i];
}
array[i+1] = value;
};
var array = [3, 5, 7, 11, 13, 2, 9, 6];
var factorial = function(n) {
var result = 1;
for (var i = 1; i <= n; i++){
result = result * i;
}
return result;
};
println("The value of 5! should be " + 5*4*3*2*1);
var factorial = function(n) {
// base case:
if (n === 0){
return 1;
}
// recursive case:
return n* factorial(n-1);
};
println("The value of 0! is " + factorial(0) + ".");
// Returns the first character of the string str
var firstCharacter = function(str) {
return str.slice(0, 1);
};
// Returns the last character of a string str
var lastCharacter = function(str) {
return str.slice(-1);
};
var drawShape = function(x, y, radius) {
//randomized variable to decide what shape
var r = random(1);
//randomizing all three colour variables
var red = floor(random(0, 255));
var blue = floor(random(0, 255));
var green = floor(random(0, 255));
//creating random colours
fill(red-radius, green+radius, blue+radius);
// This library exposes 3 functions:
// hanoi.moveDisk(fromPeg, toPeg); This moves the top disk from the fromPeg to the toPeg
// hanoi.getSparePeg(fromPeg, toPeg); This returns the remaining peg that isn't the fromPeg or the toPeg
// hanoi.isSolved(toPeg); This returns true if all disks are on toPeg and no invalid moves have been used
var hanoi = (function() {
var sprites = function() {
"use strict";
// A minimalist sprite library for KA visualizations.
// Devin Balkcom, June 2014.