Skip to content

Instantly share code, notes, and snippets.

@jennli
Last active January 22, 2016 01:26
Show Gist options
  • Save jennli/4a94f6be96d44f020de1 to your computer and use it in GitHub Desktop.
Save jennli/4a94f6be96d44f020de1 to your computer and use it in GitHub Desktop.
// //lab 1
// Write a function called printMulti that takes an array of arrays, such as:
// var my_array = [[2,3,4], ["Hello CodeCore", 1, true]];
// and prints every element to the console.
var my_array = [[2,3,4], ["Hello CodeCore", 1, true]];
function printArray(arr){
if(arr.length == 0){
return;
}
else if (arr.constructor !== Array){
console.log(arr);
return arr;
}
else{
printArray(arr[0]);
}
printArray(arr.slice(1,arr.length));
}
printArray(my_array);
// //lab2
// Write a JavaScript function to merge two arrays and removes all duplicates elements. For example:
// var array1 = [1, 2, 3];
// var array2 = [3,4,5];
// console.log(merge(array1, array2));
// [1,2,3,4,5]
function addValueToObject(arr, obj){
for(var a = 0; a < arr.length; ++a){
var value = arr[a];
if(obj[value] === undefined){
obj[value] = 0;
}
obj[value] += 1;
}
return obj;
}
function mergeArray (arr1, arr2){
var result = {};
var mergedArray = [];
// for(var a = 0; a < arr1.length; ++a){
// var value = arr1[a];
// if(result[value] === undefined){
// result[value] = 0;
// }
// result[value] += 1;
// }
// for(var a = 0; a < arr2.length; ++a){
// var value = arr2[a];
// if(result[value] === undefined){
// result[value] = 0;
// }
// result[value] += 1;
// }
addValueToObject(arr1, result);
addValueToObject(arr2, result);
for(var c in result){
mergedArray.push(c);
}
return mergedArray;
}
mergeArray([1,2,3,4,5,6,7], [2,3,4,5,6,7,8]);
// lab 3. Write a function called largestNumber that takes from an array of numbers the largest number.
function getLargestNum(arr){
var largest = arr[0];
for(var i = 1; i<arr.length; ++i){
if(arr[i] > largest){
largest = arr[i];
}
}
return largest;
}
getLargestNum([7,6,5,4,8,8,1]);
// Assignment: [lab] Card Shuffling Next Module
// Create a javascript object that stores deck of cards.
// - Add a print method on it that will print the cards in order.
// - Add a method shuffle that will shuffle the cards
// - Add a randomCard method that give your a random card from your collection
var deck = {};
function initializeDeck(deck) {
for(var i = 1; i < 14; i++){
deck[i] = i+'Spade';
deck[i + 13] = i + 'Heart';
deck[i + 13*2] = i + 'Club';
deck[i + 13*3] = i + 'Diamond';
}
}
function printDeck(deck){
for (var d in deck){
console.log(deck[d]);
}
}
function shuffle(deck){
var resultDeck = {};
for(var x in deck){
resultDeck[x] = deck[x];
}
// -- To shuffle an array a of n elements (indices 0..n-1):
// for i from n−1 downto 1 do
// j ← random integer such that 0 ≤ j ≤ i
// exchange a[j] and a[i]
for(var i = 52; i > 1; --i){
var j = Math.floor(Math.random()*i + 1);
// console.log("i is " + i + "; j is "+ j );
var temp = resultDeck[i];
resultDeck[i] = resultDeck[j];
resultDeck[j] = temp;
}
return resultDeck;
}
function randomCard(deck){
var randy = Math.floor(Math.random()*52 + 1);
var card = deck[randy];
console.log("your random card is " + card);
return card;
}
var deck = {};
initializeDeck(deck);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment