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: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Fizz Buzz
- // -----------------------------------------------
- function fizzBuzz(n) {
- let output="";
- if (n<=0) return output;
- for (i=1;i<=n;i++) {
- output+=fizzCheck(i);
- if (i!==n) {
- output+= ", ";
- }
- };
- return output;
- };
- function fizzCheck(num) {
- let word="";
- if (num<=0) {
- n=0;
- }
- else {
- if (num%3==0) {
- word+="fizz";
- }
- if (num%5==0) {
- word+="buzz";
- }
- }
- return num + word;
- };
- // -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Anagram Tester
- // -----------------------------------------------
- function areTheseAnagrams(a, b) {
- let aArr = a.toLowerCase().split("");
- let bArr = b.toLowerCase().split("");
- for(i=0;i<aArr.length;i++) {
- for (j=0;j<bArr.length;j++) {
- if (aArr[i]==bArr[j]) {
- aArr.splice(i,1);
- bArr.splice(j,1);
- i--;
- }
- }
- }
- return aArr.length==0 && bArr.length==0;
- }
- // -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Analyze Prices
- // -----------------------------------------------
- function analyzePrices(prices) {
- let output = {
- buyIndex:null,
- sellIndex:null
- };
- let maxDif=0;
- for (i=0;i<prices.length-1;i++) {
- for (j=i;j<prices.length;j++) {
- if (prices[j]-prices[i] > maxDif) {
- maxDif = prices[j]-prices[i];
- output.buyIndex=prices[i];
- output.sellIndex=prices[j];
- }
- }
- }
- return output;
- }
- // -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Sum Nested Arrays
- // -----------------------------------------------
- function sumNested(arr) {
- if (arr.length==0) return 0;
- let sum=0;
- for (i=0;i<arr.length;i++) {
- let test=arr[i];
- if (typeof test==="object") {
- sum+=sumNested(test);
- }
- else {
- sum+=test;
- }
- }
- return sum;
- }
- // -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Word Count
- // -----------------------------------------------
- function wordCount(sentence) {
- return sentence.split(" ").filter(word=>word!="").length;
- }
- // This one was fun!
- // -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Car
- // -----------------------------------------------
- function Car(speed) {
- this._speed = speed;
- this.getSpeed = function() {
- return this._speed;
- };
- this.setSpeed = function(speed) {
- if (speed>=0) this._speed = speed;
- };
- this.stop = function() {
- this.setSpeed(0);
- };
- }
- // -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Property Path Evaluation
- // -----------------------------------------------
- function propertyValueAt(obj, arr) {
- let output = 0;
- for (i=0;i<arr.length;i++) {
- output+=parseInt(rec(obj, arr[i]));
- }
- if (isNaN(output)) output = undefined;
- return output;
- }
- function rec(obj,index) {
- return obj[index];
- }
- // this only works for properties with a single value. I attempted to make this work for
- // nested properties, but ran out of time. It is shown below
- /*
- function propertyValueAt(obj, arr) {
- if (arr.length == 0) return 0;
- let output = 0;
- for (i=0;i<arr.length;i++) {
- if (typeof arr[i]==="object") {
- output+=propertyValue(obj[arr[i]],arr[i]);
- }
- output+=parseInt(obj[arr[i]]);
- }
- if (isNaN(output)) output = undefined;
- return output;
- }
- function rec(obj,index) {
- return obj[index];
- }
- let test = {
- a:1,
- b:{
- d:2,
- e:4
- }
- c:3
- };
- console.log(propertyValueAt(test,['a','b'));
- */
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">// -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Fizz Buzz
- // -----------------------------------------------
- function fizzBuzz(n) {
- let output="";
- if (n<=0) return output;
- for (i=1;i<=n;i++) {
- output+=fizzCheck(i);
- if (i!==n) {
- output+= ", ";
- }
- };
- return output;
- };
- function fizzCheck(num) {
- let word="";
- if (num<=0) {
- n=0;
- }
- else {
- if (num%3==0) {
- word+="fizz";
- }
- if (num%5==0) {
- word+="buzz";
- }
- }
- return num + word;
- };
- // -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Anagram Tester
- // -----------------------------------------------
- function areTheseAnagrams(a, b) {
- let aArr = a.toLowerCase().split("");
- let bArr = b.toLowerCase().split("");
- for(i=0;i<aArr.length;i++) {
- for (j=0;j<bArr.length;j++) {
- if (aArr[i]==bArr[j]) {
- aArr.splice(i,1);
- bArr.splice(j,1);
- i--;
- }
- }
- }
- return aArr.length==0 && bArr.length==0;
- }
- // -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Analyze Prices
- // -----------------------------------------------
- function analyzePrices(prices) {
- let output = {
- buyIndex:null,
- sellIndex:null
- };
- let maxDif=0;
- for (i=0;i<prices.length-1;i++) {
- for (j=i;j<prices.length;j++) {
- if (prices[j]-prices[i] > maxDif) {
- maxDif = prices[j]-prices[i];
- output.buyIndex=prices[i];
- output.sellIndex=prices[j];
- }
- }
- }
- return output;
- }
- // -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Sum Nested Arrays
- // -----------------------------------------------
- function sumNested(arr) {
- if (arr.length==0) return 0;
- let sum=0;
- for (i=0;i<arr.length;i++) {
- let test=arr[i];
- if (typeof test==="object") {
- sum+=sumNested(test);
- }
- else {
- sum+=test;
- }
- }
- return sum;
- }
- // -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Word Count
- // -----------------------------------------------
- function wordCount(sentence) {
- return sentence.split(" ").filter(word=>word!="").length;
- }
- // This one was fun!
- // -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Car
- // -----------------------------------------------
- function Car(speed) {
- this._speed = speed;
- this.getSpeed = function() {
- return this._speed;
- };
- this.setSpeed = function(speed) {
- if (speed>=0) this._speed = speed;
- };
- this.stop = function() {
- this.setSpeed(0);
- };
- }
- // -----------------------------------------------
- // Name: Daniel Copeland
- // -----------------------------------------------
- // -----------------------------------------------
- // Exercise: Property Path Evaluation
- // -----------------------------------------------
- function propertyValueAt(obj, arr) {
- let output = 0;
- for (i=0;i<arr.length;i++) {
- output+=parseInt(rec(obj, arr[i]));
- }
- if (isNaN(output)) output = undefined;
- return output;
- }
- function rec(obj,index) {
- return obj[index];
- }
- // this only works for properties with a single value. I attempted to make this work for
- // nested properties, but ran out of time. It is shown below
- /*
- function propertyValueAt(obj, arr) {
- if (arr.length == 0) return 0;
- let output = 0;
- for (i=0;i<arr.length;i++) {
- if (typeof arr[i]==="object") {
- output+=propertyValue(obj[arr[i]],arr[i]);
- }
- output+=parseInt(obj[arr[i]]);
- }
- if (isNaN(output)) output = undefined;
- return output;
- }
- function rec(obj,index) {
- return obj[index];
- }
- let test = {
- a:1,
- b:{
- d:2,
- e:4
- }
- c:3
- };
- console.log(propertyValueAt(test,['a','b'));
- */
- </script></body>
- </html>
Add Comment
Please, Sign In to add comment