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">
- // -----------------------
- // Geoffrey Gibson
- // -----------------------
- // -----------------------
- // Exercise: Fizz Buzz
- // -----------------------
- function fizzBuzz(n)
- {
- // loop over the number value given
- for (var i = 0; i <= n; ++i)
- {
- // checks if 'i' is divisble by 3
- if ((i % 3) === 0)
- {
- console.log(i + 'fizz');
- }
- // checks if 'i' is divisble by 5
- else if ((i % 5) === 0) {
- console.log(i + 'buzz');
- }
- // checks if 'i' is divisble by 15
- else if ((i % 15) === 0) {
- console.log(i + 'fizzbuzz');
- }
- // else print the number as is
- else {
- console.log(i);
- }
- }
- }
- console.log(fizzBuzz(15));
- // -----------------------
- // Exercise: Word Count
- // -----------------------
- // come back
- function wordCount(sentence)
- {
- var count = 0;
- var str = sentence.substring(0, sentence.length;
- for (var i = 0; i < sentence.length; ++i)
- {
- if (!str.includes(' '))
- {
- console.log(++count);
- }
- }
- }
- console.log(wordCount('Hey There'));
- // --------------------------------
- // Exercise: Object-oriented - Car
- // --------------------------------
- function Car()
- {
- // if this was not set it would return null/undefined we want this defined for the output
- this.carSpeed = 0;
- this.getSpeed = function()
- {
- // returning the value of the current speed
- return this.carSpeed;
- }
- this.setSpeed = function(speed)
- {
- // using the setter, return 'this' carSpeed the global variable and set it equal to value of speed
- this.carSpeed = speed;
- return this.carSpeed; // return the value after it has been reassigned to speed
- }
- this.stop = function() {
- // no need for a parameter to set the value to zero, just a reassignment
- this.carSpeed = 0;
- return this.carSpeed;
- }
- }
- var car = new Car();
- console.log(car.getSpeed());
- car.setSpeed(10);
- console.log(car.getSpeed());
- car.stop();
- console.log(car.getSpeed());
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">// -----------------------
- // Geoffrey Gibson
- // -----------------------
- // -----------------------
- // Exercise: Fizz Buzz
- // -----------------------
- function fizzBuzz(n)
- {
- // loop over the number value given
- for (var i = 0; i <= n; ++i)
- {
- // checks if 'i' is divisble by 3
- if ((i % 3) === 0)
- {
- console.log(i + 'fizz');
- }
- // checks if 'i' is divisble by 5
- else if ((i % 5) === 0) {
- console.log(i + 'buzz');
- }
- // checks if 'i' is divisble by 15
- else if ((i % 15) === 0) {
- console.log(i + 'fizzbuzz');
- }
- // else print the number as is
- else {
- console.log(i);
- }
- }
- }
- console.log(fizzBuzz(15));
- // -----------------------
- // Exercise: Word Count
- // -----------------------
- // come back
- function wordCount(sentence)
- {
- var count = 0;
- var str = sentence.substring(0, sentence.length;
- for (var i = 0; i < sentence.length; ++i)
- {
- if (!str.includes(' '))
- {
- console.log(++count);
- }
- }
- }
- console.log(wordCount('Hey There'));
- // --------------------------------
- // Exercise: Object-oriented - Car
- // --------------------------------
- function Car()
- {
- // if this was not set it would return null/undefined we want this defined for the output
- this.carSpeed = 0;
- this.getSpeed = function()
- {
- // returning the value of the current speed
- return this.carSpeed;
- }
- this.setSpeed = function(speed)
- {
- // using the setter, return 'this' carSpeed the global variable and set it equal to value of speed
- this.carSpeed = speed;
- return this.carSpeed; // return the value after it has been reassigned to speed
- }
- this.stop = function() {
- // no need for a parameter to set the value to zero, just a reassignment
- this.carSpeed = 0;
- return this.carSpeed;
- }
- }
- var car = new Car();
- console.log(car.getSpeed());
- car.setSpeed(10);
- console.log(car.getSpeed());
- car.stop();
- console.log(car.getSpeed());
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement