Mastur long john
6.6.4: Who is in Line
coming soon
6.7.4: Remove From Line
coming soon
6.8.4: Simulating a Coin Flip
coming soon
6.4.6: Print Flight Itinerary
function start(){ var arr = ["ENTER CITY NAME", "ENTER CITY NAME", "ENTER CITY NAME", "ENTER CITY NAME"]; for(var i = 0; i < arr.length; i++){ var cur = arr[i]; print(cur); if (i < arr.length - 1 ){ print(" -> "); } } }
6.4.5: Product of List
function start(){ var arr = [1, 8, 3, 4, 2, 9]; var product = 1; for(var i = 0; i < arr.length; i++){ var cur = arr[i]; product*= cur; } println(product); }
6.4.8: Evens Only List
function start(){ var arr = [1,2,3,4,5,6]; var even = onlyEven(arr); println(even); } function onlyEvens(arr){ var result = []; for(var i = 0; i < arr.length; i++){ var cur = arr[i]; if(cur % 2 == 0){ result.push(arr[i]); } } return(result); }
6.4.9: Reverse List
function start(){ var arr = [1,2,3,4]; var reversed = reverseList(arr); println(reversed); } function reverseList(arr){ var reverse = []; for(var i = arr.length - 1; i >= 0; i--){ reverse.push(arr[i]); } return reverse; }
6.4.7: Double List
function start(){ var arr = [10,20,30,40]; var double = doubleList(arr); println(double); } function doubleList(arr){ var result = []; for(var i = 0; i < arr.length; i++){ result.push(arr[i]); result.push(arr[i]); } return(result); }
6.3.4: Karel Array (Push/Pop)
function start(){ var arr = []; arr.push("K"); arr.push("A"); arr.push("R"); arr.push("E"); arr.push("L"); println(arr[0]); println(arr[1]); println(arr[2]); println(arr[3]); println(arr[4]); println(arr[0]); println(arr[1]); println(arr[2]); var last = arr.pop(); var last = arr.pop(); println(arr[5]); println(arr[5]); }
6.2.4: Top Movies
function start(){ var favmovies = ["Ip man", "Wall e", "World War Z", "SuperMan"]; println(favmovies[0]); favmovies[0] = "Star Wars"; println(favmovies[2]); }
6.1.5: List of Prime Numbers
function start(){ var numList = ["2", "3", "5", "7", "11"]; println(numList[0]); println(numList[2]); println(numList[4]); }
6.1.4: List of Places to Travel
function start(){ var travelList = ["Japan", "Greenland", "United Kingdom", "Iceland"]; println(travelList[2]); }
6.5.8: Draw a Barcode
setSize(400, 100); var NUM_LINES = 400; function start() { drawBarcode(generateBarcode(NUM_LINES)); } // Draw the given barcode function drawBarcode(barcode){ var line_width = getWidth() / barcode.length; for (var i = 0; i < barcode.length; i++ ){ if(barcode[i]){ var x = i * line_width; var line = new Line(x, 0, x, getHeight()); line.setLineWidth(line_width); add(line); } } } /* Generates a bit array representing a barcode * The array contains booleans in each index. * true means there is a black line. * false means there is not. * * Returns the array. */ function generateBarcode(lines) { var barcode = []; for (var i = 0; i < lines; i++) { barcode[i] = Randomizer.nextBoolean(0.3); } return barcode; }
6.5.7: Changing Circles
var NUM_CIRCLES = 15; var MIN_RADIUS = 10; var MAX_RADIUS = 40; var DELAY = 500; var circles = []; function start(){ randomCircles(); setTimer(updateCircle, DELAY); } function drawCircle(){ var RADIUS = Randomizer.nextInt(MIN_RADIUS, MAX_RADIUS); var x = Randomizer.nextInt(RADIUS, getWidth() - RADIUS); var y = Randomizer.nextInt(getHeight() - RADIUS, RADIUS); var circle = new Circle(RADIUS); circle.setPosition(x,y); add(circle); return circle; } function updateCircle(circle){ var color = Randomizer.nextColor(); for(var i = 0; i < NUM_CIRCLES; i++){ circles[i].setColor(color); } } function randomCircles(num){ for(var i = 0; i < NUM_CIRCLES; i++){ circles.push(drawCircle()); } }
6.5.5: Coin Flip Fun: Number
var NUM_FLIPS = 100; function start(){ var flips = flipCoins(); printArray(flips); countHeadsAndTails(flips); } // This function should flip a coin NUM_FLIPS // times, and add the result to an array. We // return the result to the caller. function flipCoins(){ var flips = []; for(var i = 0; i < NUM_FLIPS; i++){ if(Randomizer.nextBoolean()){ flips.push("Heads"); }else{ flips.push("Tails"); } } return flips; } function printArray(arr){ for(var i = 0; i < arr.length; i++){ println(i + ": " + arr[i]); } } function countHeadsAndTails(flips){ var headsCount = 0; var tailsCount = 0; for(var i = 0; i < flips.length; i++){ if(flips[i] == "Heads"){ headsCount++; } else { tailsCount++; } } println("Number of heads: " + headsCount); println("Number of tails: " + tailsCount); }
6.5.6: Coin Flip Fun: Longest
var NUM_FLIPS = 100; var HEADS = 0; var TAILS = 0; function start(){ var flips = flipCoins(); printArray(flips); countHeadsAndTails(flips); longestHeadsStreak(flips); } // This function should flip a coin NUM_FLIPS // times, and add the result to an array. We // return the result to the caller. function flipCoins(){ var flips = []; for(var i = 0; i < NUM_FLIPS; i++){ if(Randomizer.nextBoolean()){ flips.push("Heads"); }else{ flips.push("Tails"); } } return flips; } // COUNTS NUM HEADS & TAILS function longestHeadsStreak(arr) { var storHeads = 0; // Temporarally Stores the longest streak then resets of Heads var larHeads = 0; // Holds the longest streak of heads var storTails = 0; var larTails = 0; for(var i = 0; i < NUM_FLIPS; i++) { var x = i + 1 if(arr[i] == "Heads") { storHeads++ } if(arr[x] != "Heads") { if(larHeads < storHeads) { var larHeads = 0; larHeads += storHeads; } var storHeads = 0; } if(arr[i] == "Tails") { storTails++ } if(arr[x] != "Tails") { if(larTails < storTails) { var larTails = 0; larTails += storTails; } var storTails = 0; } } println("Longest Streak of Heads: " + larHeads); println("Longest Streak of Tails: " + larTails); } function countHeadsAndTails(arr) { for(var i = 0; i < arr.length; i++){ if(arr[i] == "Heads") { HEADS += 1; }else if(arr[i] == "Tails") { TAILS += 1; } } println("Heads: " + HEADS + " , Tails: " + TAILS); } function printArray(arr){ for(var i = 0; i < arr.length; i++){ println((i+1) + ": " + arr[i]); } }