This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Returns either the index of the location in the array, | |
or -1 if the array did not contain the targetValue */ | |
var doSearch = function(array, targetValue) { | |
var min = 0; | |
var max = array.length - 1; | |
var guess; | |
var i = 1; | |
while (max >= min){ | |
guess = Math.floor((max + min)/2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) + "."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
OlderNewer