Advertisement
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: Payton Dudley
- // Exercise: Anagram Tester
- function areTheseAnagrams(word1, word2) {
- var changeWord1 = word1.replace(/[^A-Za-z]+/g, '').toLowerCase();
- var changeWord2 = word2.replace(/[^A-Za-z]+/g, '').toLowerCase();
- var counts = [];
- var word1Length = changeWord1.length;
- var word2Length = changeWord2.length
- if (word1Length !== word2Length) {
- return false;
- }
- for (var i = 0; i < word1Length; i++) {
- var index = changeWord1.charCodeAt(i)-97;
- counts[index] = (counts[index] || 0) + 1;
- }
- for (var i = 0; i < word2Length; i++) {
- var index = changeWord2.charCodeAt(i)-97;
- if (!counts[index]) {
- return false;
- } else {
- counts[index]--;
- }
- }
- return true;
- }
- console.log(areTheseAnagrams("abc","bca"));
- console.log(areTheseAnagrams("abc","cde"));
- // Name: Payton Dudley
- // Exercise: Sum Nested Arrays
- function sumNested(i) {
- var sum=0;
- for(var a=0;a<i.length;a++){
- if(typeof i[a]=="number"){
- sum+=i[a];
- }else if(i[a] instanceof Array){
- sum+=arraySum(i[a]);
- }
- }
- return sum;
- }
- // Name: Payton Dudley
- // Exercise: Word Count
- function wordCount(str) {
- var count = 0;
- for (var i = 1; i <= str.length; i++) {
- if (str.charAt(i) == " ") {
- count ++;
- }
- }
- return count + 1;
- }
- console.log(wordCount("This is the sentence I want to test my word count on"));
- // Name: Payton Dudley
- // Exercise: Property Path Evaluation
- /*function propertyValueAt(key, obj) {
- return key.split('.').reduce(function(a,b){
- return a && a[b];
- }, obj);
- }
- propertyValueAt('foo.bar', obj);
- var key = key.split('.');
- console.log(obj[keys[0]][keys[1]]);
- var object = {
- a: 1,
- b: {
- c: 2,
- d: 3
- }
- };
- propertyValueAt(object,['a']);*/
- // Name: Payton Dudley
- // Exercise: Object Oriented Programming - Car
- function Car() {
- this.speed = 0;
- this.getSpeed = function () {
- return this.speed;
- }
- this.setSpeed = function (speed) {
- if (speed >= 0) {
- this.speed = speed;}
- }
- this.stop = function () {
- this.speed = 0;
- }
- }
- var myCar = new Car();
- console.log(myCar.getSpeed());
- myCar.setSpeed(25);
- console.log(myCar.getSpeed());
- myCar.stop();
- console.log(myCar.getSpeed());
- // Name: Payton Dudley
- // Exercise: Analyze Prices
- var prices = [1,2,3,4,5,6,7,8,9,0];
- var min = 0;
- var buyIndex;
- var sellIndex;
- var obj = {buyIndex: Number.POSITIVE_INFINITY, sellIndex: Number.NEGATIVE_INFINITY};
- function analyzePrices(prices) {
- for (var i=prices.length-1; i>=0; i--) {
- tmp = prices[i];
- if (tmp < buyIndex) buyIndex = tmp;
- if (tmp > sellIndex) sellIndex = tmp;
- }
- var obj ={}
- obj[buyIndex];
- return obj{buyIndex, sellIndex};
- }
- analyzePrices(prices);
- // Name: Payton Dudley
- // Exercise: Fizz Buzz
- var n = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
- function fizzbuzz (n) {
- for(var i = 1; i < n.length; i++){
- if( i % 3 === 0 && i % 5 === 0){
- console.log(i + "fizzbuzz");
- }else if( i % 3 === 0){
- console.log(i + "fizz");
- }else if( i % 5 === 0){
- console.log(i + "buzz");
- } else {
- console.log(i);
- }
- }
- }
- console.log(fizzbuzz(n));
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">// Name: Payton Dudley
- // Exercise: Anagram Tester
- function areTheseAnagrams(word1, word2) {
- var changeWord1 = word1.replace(/[^A-Za-z]+/g, '').toLowerCase();
- var changeWord2 = word2.replace(/[^A-Za-z]+/g, '').toLowerCase();
- var counts = [];
- var word1Length = changeWord1.length;
- var word2Length = changeWord2.length
- if (word1Length !== word2Length) {
- return false;
- }
- for (var i = 0; i < word1Length; i++) {
- var index = changeWord1.charCodeAt(i)-97;
- counts[index] = (counts[index] || 0) + 1;
- }
- for (var i = 0; i < word2Length; i++) {
- var index = changeWord2.charCodeAt(i)-97;
- if (!counts[index]) {
- return false;
- } else {
- counts[index]--;
- }
- }
- return true;
- }
- console.log(areTheseAnagrams("abc","bca"));
- console.log(areTheseAnagrams("abc","cde"));
- // Name: Payton Dudley
- // Exercise: Sum Nested Arrays
- function sumNested(i) {
- var sum=0;
- for(var a=0;a<i.length;a++){
- if(typeof i[a]=="number"){
- sum+=i[a];
- }else if(i[a] instanceof Array){
- sum+=arraySum(i[a]);
- }
- }
- return sum;
- }
- // Name: Payton Dudley
- // Exercise: Word Count
- function wordCount(str) {
- var count = 0;
- for (var i = 1; i <= str.length; i++) {
- if (str.charAt(i) == " ") {
- count ++;
- }
- }
- return count + 1;
- }
- console.log(wordCount("This is the sentence I want to test my word count on"));
- // Name: Payton Dudley
- // Exercise: Property Path Evaluation
- /*function propertyValueAt(key, obj) {
- return key.split('.').reduce(function(a,b){
- return a && a[b];
- }, obj);
- }
- propertyValueAt('foo.bar', obj);
- var key = key.split('.');
- console.log(obj[keys[0]][keys[1]]);
- var object = {
- a: 1,
- b: {
- c: 2,
- d: 3
- }
- };
- propertyValueAt(object,['a']);*/
- // Name: Payton Dudley
- // Exercise: Object Oriented Programming - Car
- function Car() {
- this.speed = 0;
- this.getSpeed = function () {
- return this.speed;
- }
- this.setSpeed = function (speed) {
- if (speed >= 0) {
- this.speed = speed;}
- }
- this.stop = function () {
- this.speed = 0;
- }
- }
- var myCar = new Car();
- console.log(myCar.getSpeed());
- myCar.setSpeed(25);
- console.log(myCar.getSpeed());
- myCar.stop();
- console.log(myCar.getSpeed());
- // Name: Payton Dudley
- // Exercise: Analyze Prices
- var prices = [1,2,3,4,5,6,7,8,9,0];
- var min = 0;
- var buyIndex;
- var sellIndex;
- var obj = {buyIndex: Number.POSITIVE_INFINITY, sellIndex: Number.NEGATIVE_INFINITY};
- function analyzePrices(prices) {
- for (var i=prices.length-1; i>=0; i--) {
- tmp = prices[i];
- if (tmp < buyIndex) buyIndex = tmp;
- if (tmp > sellIndex) sellIndex = tmp;
- }
- var obj ={}
- obj[buyIndex];
- return obj{buyIndex, sellIndex};
- }
- analyzePrices(prices);
- // Name: Payton Dudley
- // Exercise: Fizz Buzz
- var n = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
- function fizzbuzz (n) {
- for(var i = 1; i < n.length; i++){
- if( i % 3 === 0 && i % 5 === 0){
- console.log(i + "fizzbuzz");
- }else if( i % 3 === 0){
- console.log(i + "fizz");
- }else if( i % 5 === 0){
- console.log(i + "buzz");
- } else {
- console.log(i);
- }
- }
- }
- console.log(fizzbuzz(n));
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement