Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- // --------------------------------------------------------------------------
- // Name: Christopher Cobb
- // --------------------------------------------------------------------------
- // --------------------------------------------------------------------------
- // Exercise: Property Path Evaluation
- // --------------------------------------------------------------------------
- //Function propertyValueAt
- //Input: object whose value to find.
- // propertys: an array of a property to find.
- //Output: object's property based on input array, or undefined if not found.
- function propertyValueAt(inputObject,propertys){
- //We need to find the property, which could be another object of properties.
- var propertyValue = inputObject;
- //Let's loop through propertys to go down the list.
- for(var i = 0; i < propertys.length; i++ ){
- propertyValue = propertyValue[propertys[i]];
- }
- return propertyValue;
- }
- /*
- var object = {
- a: 1,
- b: {
- c: 2,
- d: 3
- }
- };
- console.log(propertyValueAt(object,['b','f']));
- */
- // --------------------------------------------------------------------------
- // Name: Christopher Cobb
- // --------------------------------------------------------------------------
- // --------------------------------------------------------------------------
- // Exercise: Sum Nested Arrays
- // --------------------------------------------------------------------------
- // Function sumNested
- // Input: addArray - an array of numbers and arrays of numbers to be added.
- // Return Output: The sum of all numbers (current code version has to have numbers)
- //var maxLoop = 0; //Inf. loop protector cuz JSBin doesn't have a stop option.
- function sumNested(addArray){
- //Let's go for this approach:
- //for each element, we need to recurse on each element of the array./*
- /*maxLoop++;
- if( maxLoop > 1000 ){
- console.log("max hit");
- return 0;
- }*/
- var sum = 0;
- //So if we're just a number at a array element, add it up!
- // else dive into the array until we're at the elements then add them up.
- for( var i = 0; i < addArray.length; i++){
- if(!Array.isArray(addArray[i])){
- console.log('Adding ' + addArray[i]);
- sum = sum + addArray[i]; // Single num found! Add it up!
- } else {
- console.log('recursive ' + addArray[i]);
- sum = sum + sumNested(addArray[i]); //Add up everything inside the array!
- }
- console.log("Sum:" + sum);
- }
- return sum;
- }
- //console.log(sumNested([])); //23
- // --------------------------------------------------------------------------
- // Name: Christopher Cobb
- // --------------------------------------------------------------------------
- // --------------------------------------------------------------------------
- // Exercise: Word Count
- // --------------------------------------------------------------------------
- //Function wordCount(sentence)
- // Input: A string of characters to count the # of words of.
- // Output: The number of words. Words are space seperated, and can consist
- // of any type of symbol that isn't a space or newline.
- function wordCount(sentence){
- //A simple problem, simply check for spaces in a small loop
- var numWords = 0;
- var atSpace = true;
- //Here's the logic. We start assuming we're at a space.
- //Check a letter, if we're at a space and the next letter is a real letter
- // we're at a new word!
- //If we're not at a space, then we end the current word at a space, then
- // check for the next word as above.
- for( var i = 0; i < sentence.length; i++){
- if(atSpace){
- if(sentence[i] !== ' ' && sentence[i] !== '\n'){
- atSpace = false;
- numWords++;
- }
- } else {
- atSpace = sentence[i] === ' ' || sentence[i] === '\n';
- }
- }
- return numWords;
- }
- //console.log(wordCount('Its a newline\ntest! \n Hi!'));
- //Returns 5, "Its" "a" "newline" "test!" "Hi!"
- // --------------------------------------------------------------------------
- // Name: Christopher Cobb
- // --------------------------------------------------------------------------
- // --------------------------------------------------------------------------
- // Exercise: Anagram Tester
- // --------------------------------------------------------------------------
- //function isNumberofEqual(testChar, stringA, stringB)
- //input: a character to test for the number of occurances
- // string 1 to check.
- // string 2 to check.
- //output: If the number of testCha occurances in the strings is the same
- //function areTheseAnagrams(stringA,stringB)
- //input: stringa and stringb to check for anagrams.
- //output: if they are anagrams.
- function isNumberOfEqual(testChar, stringA ,stringB){
- var numA = 0;
- var numB = 0;
- //Let's count all the characters!
- for( var i = 0; i < stringA.length; i++){
- if(stringA.charAt(i) === testChar){
- numA++;
- }
- if(stringB.charAt(i) === testChar ){
- numB++;
- }
- }
- return numA === numB;
- }
- function areTheseAnagrams(stringA,stringB){
- //Ok! We got two string! First, if they're not equal length they're not anas
- if( stringA.length != stringB.length)
- {
- return false;
- }
- //Now from here on they are equal length. My quick idea is a markoff method.
- //is to check if the character at i ocurs the same amount of times in both
- // strings, using isNumberofEqual.
- for(var i = 0; i < stringA.length; i++){
- //if(stringB.indexOf(stringA.charAt(i)) === -1){
- if( isNumberOfEqual(stringA.charAt(i),stringA,stringB) === false ){
- return false;
- }
- }
- return true;
- }
- //console.log(areTheseAnagrams("abc","cde"));
- // --------------------------------------------------------------------------
- // Name: Christopher Cobb
- // --------------------------------------------------------------------------
- // --------------------------------------------------------------------------
- // Exercise: Analyze Prices
- // --------------------------------------------------------------------------
- //function analyzePrices(priceArray)
- //input: An array of prices, indexed by time.
- //output: an object of buyIndex: the index to buy the stock. sellIndex: the index to sell
- //Nulls if it's not profitable at all.
- function analyzePrices(priceArray){
- var priceIndex = new Object;
- priceIndex.buyIndex = 0;
- priceIndex.sellIndex = 0;
- var maxProfit = 0;
- var heldStock = 0;
- for( var i = 0; i < priceArray.length; i++){
- for( var j = 1; j < priceArray.length; j++){
- //find the profit for buying at i, selling at j
- if(i < j){ //can't sell before buy
- var profit = priceArray[j]-priceArray[i];
- if(profit > maxProfit){
- //record!
- priceIndex.buyIndex = i;
- priceIndex.sellIndex = j;
- maxProfit = profit;
- }
- }
- }
- }
- if( maxProfit === 0){
- priceIndex.buyIndex = null;
- priceIndex.sellIndex = null;
- }
- return priceIndex;
- }
- //console.log(analyzePrices([1,1,1,1,1]));
- // --------------------------------------------------------------------------
- // Name: Christopher Cobb
- // --------------------------------------------------------------------------
- // --------------------------------------------------------------------------
- // Exercise: Fizz Buzz
- // --------------------------------------------------------------------------
- function fizzBuzz(n){
- var returnString = "";
- if( n <= 0 ){
- return returnString;
- }
- for( var i = 1; i <= n; i++){
- returnString += 1;
- if(i%3 === 0){
- returnString += 'fizz';
- }
- if(i%5 === 0){
- returnString += 'buzz';
- }
- returnString += ","
- }
- return returnString;
- }
- //Really? Fizzbuzz?
- console.log(fizzBuzz(15));
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">// --------------------------------------------------------------------------
- // Name: Christopher Cobb
- // --------------------------------------------------------------------------
- // --------------------------------------------------------------------------
- // Exercise: Property Path Evaluation
- // --------------------------------------------------------------------------
- //Function propertyValueAt
- //Input: object whose value to find.
- // propertys: an array of a property to find.
- //Output: object's property based on input array, or undefined if not found.
- function propertyValueAt(inputObject,propertys){
- //We need to find the property, which could be another object of properties.
- var propertyValue = inputObject;
- //Let's loop through propertys to go down the list.
- for(var i = 0; i < propertys.length; i++ ){
- propertyValue = propertyValue[propertys[i]];
- }
- return propertyValue;
- }
- /*
- var object = {
- a: 1,
- b: {
- c: 2,
- d: 3
- }
- };
- console.log(propertyValueAt(object,['b','f']));
- */
- // --------------------------------------------------------------------------
- // Name: Christopher Cobb
- // --------------------------------------------------------------------------
- // --------------------------------------------------------------------------
- // Exercise: Sum Nested Arrays
- // --------------------------------------------------------------------------
- // Function sumNested
- // Input: addArray - an array of numbers and arrays of numbers to be added.
- // Return Output: The sum of all numbers (current code version has to have numbers)
- //var maxLoop = 0; //Inf. loop protector cuz JSBin doesn't have a stop option.
- function sumNested(addArray){
- //Let's go for this approach:
- //for each element, we need to recurse on each element of the array./*
- /*maxLoop++;
- if( maxLoop > 1000 ){
- console.log("max hit");
- return 0;
- }*/
- var sum = 0;
- //So if we're just a number at a array element, add it up!
- // else dive into the array until we're at the elements then add them up.
- for( var i = 0; i < addArray.length; i++){
- if(!Array.isArray(addArray[i])){
- console.log('Adding ' + addArray[i]);
- sum = sum + addArray[i]; // Single num found! Add it up!
- } else {
- console.log('recursive ' + addArray[i]);
- sum = sum + sumNested(addArray[i]); //Add up everything inside the array!
- }
- console.log("Sum:" + sum);
- }
- return sum;
- }
- //console.log(sumNested([])); //23
- // --------------------------------------------------------------------------
- // Name: Christopher Cobb
- // --------------------------------------------------------------------------
- // --------------------------------------------------------------------------
- // Exercise: Word Count
- // --------------------------------------------------------------------------
- //Function wordCount(sentence)
- // Input: A string of characters to count the # of words of.
- // Output: The number of words. Words are space seperated, and can consist
- // of any type of symbol that isn't a space or newline.
- function wordCount(sentence){
- //A simple problem, simply check for spaces in a small loop
- var numWords = 0;
- var atSpace = true;
- //Here's the logic. We start assuming we're at a space.
- //Check a letter, if we're at a space and the next letter is a real letter
- // we're at a new word!
- //If we're not at a space, then we end the current word at a space, then
- // check for the next word as above.
- for( var i = 0; i < sentence.length; i++){
- if(atSpace){
- if(sentence[i] !== ' ' && sentence[i] !== '\n'){
- atSpace = false;
- numWords++;
- }
- } else {
- atSpace = sentence[i] === ' ' || sentence[i] === '\n';
- }
- }
- return numWords;
- }
- //console.log(wordCount('Its a newline\ntest! \n Hi!'));
- //Returns 5, "Its" "a" "newline" "test!" "Hi!"
- // --------------------------------------------------------------------------
- // Name: Christopher Cobb
- // --------------------------------------------------------------------------
- // --------------------------------------------------------------------------
- // Exercise: Anagram Tester
- // --------------------------------------------------------------------------
- //function isNumberofEqual(testChar, stringA, stringB)
- //input: a character to test for the number of occurances
- // string 1 to check.
- // string 2 to check.
- //output: If the number of testCha occurances in the strings is the same
- //function areTheseAnagrams(stringA,stringB)
- //input: stringa and stringb to check for anagrams.
- //output: if they are anagrams.
- function isNumberOfEqual(testChar, stringA ,stringB){
- var numA = 0;
- var numB = 0;
- //Let's count all the characters!
- for( var i = 0; i < stringA.length; i++){
- if(stringA.charAt(i) === testChar){
- numA++;
- }
- if(stringB.charAt(i) === testChar ){
- numB++;
- }
- }
- return numA === numB;
- }
- function areTheseAnagrams(stringA,stringB){
- //Ok! We got two string! First, if they're not equal length they're not anas
- if( stringA.length != stringB.length)
- {
- return false;
- }
- //Now from here on they are equal length. My quick idea is a markoff method.
- //is to check if the character at i ocurs the same amount of times in both
- // strings, using isNumberofEqual.
- for(var i = 0; i < stringA.length; i++){
- //if(stringB.indexOf(stringA.charAt(i)) === -1){
- if( isNumberOfEqual(stringA.charAt(i),stringA,stringB) === false ){
- return false;
- }
- }
- return true;
- }
- //console.log(areTheseAnagrams("abc","cde"));
- // --------------------------------------------------------------------------
- // Name: Christopher Cobb
- // --------------------------------------------------------------------------
- // --------------------------------------------------------------------------
- // Exercise: Analyze Prices
- // --------------------------------------------------------------------------
- //function analyzePrices(priceArray)
- //input: An array of prices, indexed by time.
- //output: an object of buyIndex: the index to buy the stock. sellIndex: the index to sell
- //Nulls if it's not profitable at all.
- function analyzePrices(priceArray){
- var priceIndex = new Object;
- priceIndex.buyIndex = 0;
- priceIndex.sellIndex = 0;
- var maxProfit = 0;
- var heldStock = 0;
- for( var i = 0; i < priceArray.length; i++){
- for( var j = 1; j < priceArray.length; j++){
- //find the profit for buying at i, selling at j
- if(i < j){ //can't sell before buy
- var profit = priceArray[j]-priceArray[i];
- if(profit > maxProfit){
- //record!
- priceIndex.buyIndex = i;
- priceIndex.sellIndex = j;
- maxProfit = profit;
- }
- }
- }
- }
- if( maxProfit === 0){
- priceIndex.buyIndex = null;
- priceIndex.sellIndex = null;
- }
- return priceIndex;
- }
- //console.log(analyzePrices([1,1,1,1,1]));
- // --------------------------------------------------------------------------
- // Name: Christopher Cobb
- // --------------------------------------------------------------------------
- // --------------------------------------------------------------------------
- // Exercise: Fizz Buzz
- // --------------------------------------------------------------------------
- function fizzBuzz(n){
- var returnString = "";
- if( n <= 0 ){
- return returnString;
- }
- for( var i = 1; i <= n; i++){
- returnString += 1;
- if(i%3 === 0){
- returnString += 'fizz';
- }
- if(i%5 === 0){
- returnString += 'buzz';
- }
- returnString += ","
- }
- return returnString;
- }
- //Really? Fizzbuzz?
- console.log(fizzBuzz(15));</script></body>
- </html>
Add Comment
Please, Sign In to add comment