Problem of the Day

SCROLL TO BOTTOM

Get out a piece of paper and answer the following question.


Wednesday, 2/4

  1. What happens if I call a + 3 without declaring "a" as a variable first

  2. Solution: Reference Error: a is not Defined.
  3. What does the last line of the following code print to the console?
            var a;
            a = 3;
            var b;
            b = "this is a string";
            a + b + a; //what does this line print?
           

    Solution: "3this is a string3";

Thursday, 2/5

  1. What does the last line of the following code print to the console?
            var a;
            a = "I'm sick of this snow.";
            var b;
            b = "this snow.";
            a - b; //what does this line print?
           

  2. Solution: NaN
  3. How can I concatenate a and b in the code below to get 44?
            var a;
            a = 4;
            var b;
            b = 4;
            
            //if we add a+b, we get 8. How can we concatenate them?
            
           

  4. Solution: 4 + "" + 4; (Alternatively, skip concatenation and just do a*10+b;)

Monday, 2/9

  1. What does the following code print?
    
             var a;
             a = 4;
             a + "b" + 4 //what does this return?
           

  2. Solution: "4b4";
  3. What are the two errors in the code below?
    
             var a;
             a = a + 3;
             a + b;
           

  4. Solution: Neither a nor b is assigned a value before being used.

Tuesday, 2/10

  1. What will be the value of a at the end of the loop?
    
             var a = 0;
             while (a < 10) {
             a = a + 1;
             }
           

  2. Solution: 10.

Wednesday, 2/11

  1. What do each of the following mean? (Fill in the blank)
    1. a < b     a is less than b
    2. a >= b     a is greater than or equal to b
    3. a == b     a is equal to b
    4. a != b     a is not equal to b b
    5. a >= b     a is greater than or equal to b

Thursday, 2/12

  1. 1. Is the last line True or False?
    
             var a = 15;
             var b = 2;
             var c = b - a; (-13) 
             var d = b + c; (-11)
             
             d > -12
             
           
    Solution True. c equals -13, therefore d equals -11. -11 is greater Than -12, because when dealing with negative numbers
    the number closer to zero is always greater.
  1. 2. For each of the following variables, write "valid" or "invalid" depending on whether it is a valid variable name.
    
             
             1. var Frosted_Flakes;
             2. var fr00tl00ps;
             3. var else;
             4. var 5pecial_K;
             5. var while;
             
           
    Solution 1. valid 2. valid 3. invalid 4. invalid 5. invalid

Friday, 2/13

  1. Say whether each line returns true or false.
    
             a. 1 <= 2
             b. 1 > 1
             c. 1 === 1
             d. 1 === "1"
             e. "foo" === "bar"
             f. !true
             g. true && false
             h. true || false
           
    Solution a. true b. false c. true d. false e. false f. false g. false h. true
  1. What is the value of x at the end of the loop?
    
             var x;
             x = 10;
             while(x > 0) {
               x = x - 3;
            }
            x
           
    Solution X equals -2. Each time the loop runs, it reduces X by 3. So 10 - 3 = 7, 7 - 3 = 4, 4 - 3 = 1, and 1 - 3 = -2.
    At this point, x is now less than zero, so the loop cannot run again.

Friday, 2/13

  1. Write the output of this code
    
                var n = 0;
                while (n < 10) {
                  console.log(n);
                  n = n + 3;
                }
              

    Solution 0
    3
    6
    9
  2. Write code to print the odd numbers from 1 to 11. Use a while loop.
    Solution:
          var a = 1;
          while(a <= 11) {
          console.log(a);
          a = a + 2;
          }
          

Tuesday, 2/24

  1. Write a while loop using correct syntax.
    Solution:
    
            var a = 0;
            while ( a < 10 ) {
              a = a + 1;
            }
          
  2. Write an if statement using correct syntax.
    Solution:
           if ( 3 < 5 ) {
            console.log("true");
           }
         

Wednesday, 2/25

  1. What does the following code do?
    
           var a = "hello there!";
           console.log(a.length);
         

    Solution: It prints "12"
  2. Fill in the blanks. What will this program print?
    
          var a = 4;
          if (  _____ ) {
            console.log ("a is less than four");
          } else if (_______ ){ 
            console.log ("a is equal to four");
          } else {
            console.log("a is greater than four");
          }
          

    Solution: if (a < 4) ... else if (a == 4) ... it will print "a is equal to four".
    Note the "==" in the else if. Remember that "=" is for assignment (var a = 4) and "==" is for checking equality (if (a == 4))

Thursday, 2/26

  1. What does the following code output?
    
           
           var a = 0;
           var b = 5;
           while (a < 10) {
           
            if (b <= a) {
              console.log(a);
            }
           
           a = a + 1;
           
           }
         
  2. Solution: 5 6 7 8 9

    Link to Today's Lab.


    Friday, 2/27

    TEST REVIEW

  1. Write the output of the following code.
    
           var x = 3;
           var y = x + x;
           
           a. x * y
           b. x !== y
           c. if (x < y) {
                console.log("x is smaller than y");
              } else {
                console.log("x is not smaller than y");
              }
         
    Solution: a. 18 b. true c. x is smaller than y
  2. Writes yes if it's a valid variable name, no if it's invalid.
             a. var x;
             b. var School_For_Human_Rights;
             c. var 123xyz;
             d. var xyz123;
           
    a. yes b. yes c. no d. yes

Tuesday, 3/3

  1. Write a while loop that outputs the following:
    
          0
          1
          2
          4
          5
          6
         
  2. Solution:
    
        var i = 0;
        while(i < 7) {
         
          if (i !== 3) {
            console.log(i);
          }
          i = i + 1;
        }
         

Wednesday, 3/4

  1. Say what the following code outputs:
    
            var anArray = ["hello", 777, true];
            
            a. anArray[0]
            b. anArray[1]
            c. anArray[1] + 1
            d. anArray[2]
            e. !anArray[2]
          
  2. Write code that creates an array with three elements: one string, one number, and one boolean.
  3. Solution:
    
          
          a."hello"
          b.777
          c.778
          d.true
          f.false
          
          var myarray = ["string",123,true];
          
        

Thursday, 3/5

  1. Write a while loop that outputs the following:
    
           
           no
           no
           no
           no
           yes
           no
    
          
    Answer
    
            var a = 1;
            while (a <= 6) {
              if (a == 5) {
                console.log("yes");
              } else {
                console.log("no");
              }
              
              a = a + 1;
            }
          
    Link to today's lab

Friday, 3/6

  1. Write a while loop that outputs the following:
    
            1
            2
            3
            5
            6
            7
          
    Answer
    
            var n = 1;
            while (n <= 7) {
              if (n !== 4) {
                console.log(n);
              }
              
              n = n + 1;
            }
          

Monday, 3/9

  1. What do the following boolean expressions return?
    
            a. (3 < 5) && (9 < 7);
            b. var n = 3;
               (n < 10) || (n > 50);
            c. var x = 42;
               (n <= 100) && (n >= 0);
          
  2. Solution:
    a. False
    b. True
    c. True
  3. Write code to express the following.
    
            a. n is a number equal to 10
            b. n is bigger than 5
            c. n is smaller than or the same size as 8
            d. n is between 9 and 11
          
  4. Solution:
    a. n == 10;
    b. n > 5;
    c. n <= 8;
    d. n >= 9 && n <= 11;

Tues, 3/10

  1. Write a while loop that outputs the following:
    
            soup
            soup
            sandwhich
            soup
          

    Solution:
    
            var i = 0;
            while (i < 4) {
              if (i == 3) {
                console.log("sandwhich");
              } else {
                console.log("soup");
              }
            }
          
  2. What is the value of the variable "bar" after this code executes?
    
            
            var foo = function(n) {
              return n;
            };
            
            var bar = foo(10);
            
          

    Solution:10

Wed, 3/11

  1. (no problem of the day)

Thurs, 3/12

  1. Write a while loop that outputs the following:
    
            3
            4
            5
            6
          

    Solution:
    
            var i = 3;
            while (i < 7) {
              console.log(i);
              i = i + 1;
            }
          

Tues, 3/17

  1. How many times will this loop run?
    
            var i = 17;
            while ( i <= 26) {
              console.log('This is loop number ' + i);
              i = i + 1;
            }
          

    Solution:10 (the loop prints 17, 18, ... 25, 26)
  2. How many times will this loop run?
    
            var i = 17;
            var counter = 1;
            
            while ( i <= 26) {
              console.log('This is loop number ' + counter);
              counter = counter + 1;
              i = i + 2;
            }
          

    Solution:5 (the loop prints 1, 2, 3, 4, 5; but i is 17, 19, 21, 23, 25 and stops when it hits 27

Weds, 3/18

  1. For the following, write the minimum and maximum number that the loop will print
    1. 
              var i = 12;
              while (i < 100) {
                  console.log(i);
                  i = i + 1;
              }
            

      Answer:Min: 12, Max: 99
    2. 
              var i = 7;
              while (i < 100) {
                  i = i + 1;
                  console.log(i);
              }
            
      Answer:Min: 8, Max: 100
      Notice here that we increment i by 1 before we print it!
    3. 
              var i = 8;
              while (i <= 77) {
                  i = i + 3;
                  console.log(i);
              }
            

      Answer:Min: 11, Max: 77
      Notice here that we increment i by 3 before we print it!
    4. Test Practice Problem

    Write a while loop that prints "yes" for odd numbers and "no" for even numbers, for all numbers between 1 and 100. The first five outputs should look like:
    yes
    no
    yes
    no
    yes
          
    Answer
    
            var i = 1;
            while (i <= 100){
              if (i % 2 == 0) {
                console.log("no");
              } else {
                console.log("yes");
              }
            }
          

Thurs, 3/19

  1. Write a while loop that outputs the following:
    
            3
            4
            6
          
    Answer
    
            var a = 3;
            while (a < 7) {
              if (a !== 5) {
                console.log(a);
              }
              a = a + 1;
            }
          

Fri, 3/20 -- TEST REVIEW

  1. Write a while loop that outputs the following:
    
              2
              4
              8
              10
            
    Answer
    
              var n = 2;
              while (n <= 10) {
                if (n !== 6) {
                  console.log(n);
                }
              }
            

Mon, 3/23

  1. Write what the following boolean expressions mean in English.
    
              a. x !== 5
              b. x >= 10 && x <= 20
            
  2. Solution
    
            a. x is not equal to five.
            b. x is a number between 10 and 20.
          
  3. Write JavaScript code that means the same thing as these English statements.
    
              a. n is greater than 8
              b. x is between 30 and 40
              c. if y is greater than 13 print "big",
                 if y is smaller print "small",
                 if y is the same size print "unlucky"
            
  4. Answer
    
            a. n > 8
            b. x >= 30 && x =< 40
            c. if (y > 13) {
                   console.log('big');
               }
               if (y < 13) {
                   console.log('small');
               }
               if (y == 13) {
                   console.log('unlucky');
               }
          

Tues, 3/24

  1. What will the following code print?
    
             
              var i = 1;
              var count = 1;
              
              while (i <= 10) {
              
                  console.log(count);
              
                  count = count + 1;
              
                  i = i + 2;
              
              }
             
            

Wed, 3/25

  1. Given the following function:
    
              var f = function(n) {
                if (n < 18) {
                  console.log("minor");
                } else if (n > 18) {
                  console.log("major");
                } else {
                  console.log("on the line");
                }
            
    What gets printed out by the following?
    
              a. f(5)
              b. f(25)
              c. f(18)
              d. f(17)
            
  2. Solution
    
            a. minor
            b. major
            c. on the line
            d. minor
          

3-26

  1. What do you need to do to get the following function to print out "Hello, Barry White"
    
            var greetMe = function(name) {
            
                console.log("Hello, " + name);
            
            }
          

3-26

Doughtnuts!


Mon 3-30

  1. Write, in english, what this function does.
    
              var radiusToDiameter = function(n) {
                return n*2;
              }
            
  2. Solution:This function converts the radius of a circle to its diameter.
  3. Write a function that converts the diameter of a circle to a radius.
  4. Solution:
    
            var diameterToRadius = function(n) {
                return n/2;
            }
          

Tues 3-31

  1. Create a function that can convert minutes to seconds.

Weds April 1

  1. What does the following program print?
    
              function divide(a, b) {
                return a / b;
              }
              
              console.log( divide( 4, 2 ) );
              console.log( divide( 2, 4 ) );
              console.log( divide( 1, 0 ) );
            
  2. Solution
    
            1. 2
            2. 0.5
            3. Infinity
          
  3. Which of the following two functions would produce "April Fools!" when called like
    myFunction("April ", " Fools!")
           #1
              var myFunction = function(a, b) {
                return b + a;
              }
           #2
              var myFunction = function(a, b) {
                return a + b;
              }
            
  4. Solution #2
Exit Problem: write a function which does the following

        
      myFunction(4, 5) -> returns 10
      myFunction(9, 0) -> returns 10
      myFunction(1, 4) -> returns 6
      myFunction("hi", "there") -> returns "hi1there"
        
      
Solution

        var myFunction = function(a,b) {
        
          return a + 1 + b;
        
        }
      

Weds April 2

  1. Write a function that can square a number.
              myFunction(2) --> returns 4
              myFunction(3) --> returns 9
              myFunction(10) --> returns 100
              myFunction(-8) --> returns 64
            

Monday April 13

Translate the following math formulas into JavaScript functions.
    
  1. 2x^2 + 3x + 5
  2. 4/3 * pi * r^3
  3. bh/2

Monday April 13

  1. Write any valid function and explain what your function does.
  2. l

    Tuesday, April 15

    The gravity formula is given by the expression

    Where G is a constant equal to 6.67 × 10-11 N-m2/kg2

    Written in Javascript, this looks like:

                function Force (m1, m2, r) {
                  return (G * m1 * m2) / (r * r)
                }
              
    l
  3. What arguments (parameters) does this function take in?
  4. What type of thing (string, list, etc) does this function return?
  5. What type of thing (string, list, etc) are the parameters to this function?

Thurs, April 16

What will each block of code print to the console?
  1.               var a = 0;
                  a = 1;
                  console.log(a);
                
  2.               var b = "mama.";
                  b = "papa.";
                  b = "david bowie."
                  console.log(b);
                
  3.               var a = "Luther Vandross";
                  var b = "Barry White";
                  a = "Teddy Pendergrass"
                  console.log(b);
                
  4.               var b = function(num) {
                            return num + 1;
                          };
                  console.log(b);
                
  5.               var b = function(num) {
                            return num + 1;
                          };
                  console.log(b(2));
                

Friday, April 17

What will this code print out?
  1. 
                    var f = function(x, y) {
                      return 4*x + y - 1;
                    }
                    console.log(f(10, 3));
                  
  2. 
                    var whatDoWeWant = function(s) {
                      return s + " now!"
                    }
                    console.log(whatDoWeWant("15"));
                  
  3. Write a function to triple a number and add one to it.

Class discussion: Program or be Programmed

Read the first five paragraphs, starting with "When human beings acquired language" and ending with "But so far, anyway, too many of us are finding our digital networks responding unpredictably or even opposed to our intentions."

Reflection questions -- email nabil.hassein@gmail.com

  • What do you want to do after you graduate? What do computers have to do with it?
  • What do you think the future will look like?
  • How is reading and writing code the same/different from reading and writing English?


  • Wednesday, April 22

    Write a function that returns concatenates its input with itself. Example:
    
                f("foo") -> "foofoo"
                f("2") -> "22"
              

    Friday, April 24

    1. How are the following related? Write at least three sentences. Give your best guess if you don't know.

    Monday, April 27

    Create a new page on your neocities site and replace all of the code on that page with the code from This Page.

    Wednesday, April 29

    Using the cheatsheet provided, add a string of text to the graphics page that you created yesterday. See https://teals-test.neocities.org/graphics.html for an example (mine says "Hello World").

    Monday, May the 4th be with you

    Choose a topic in Computer Science that interests you. (Examples: programming in SNAP, web development/design, HTML, Javascript, graphics programming, games programming, physics simulation, hardware, etc.). Why does it interest you? What more would you like to learn about it?

    For full credit, please write at least two full, well-constructed paragraphs.

    Wednesday, April 22

    The way to work with dates in Javascript is to use the "Date" Object. It looks like this:
    
                var d = new Date('3/4/2015');
              
    When you create two date variables, you can add or subtract them.

    But the result is given in milliseconds.
    Given this information, write some Javascript to determine how many days are left in the school year.
    (you don't actually have to calculate the answer, just show how the function would work.)

    Wednesday, May 6th

    Write a function that expresses the following math formula:
     f(x) = x^2 + 5x + 7 

    Thursday, May 7th

    Look up the Javascript "setTimeout" function, and use it to write a function that logs "Cool story, bro" to the console after 5 seconds.

    Monday, May 11th

    Take your best guess at what this code prints out:
    
            var arr = ["hello", "goodbye", 7, true];
            
            for (var i = 0; i < arr.length; i++) {
              console.log(arr[i]);
            }
          

    Tuesday, May 12th

    Write a function that uses setInterval to print "tick" to the console every 3 seconds.



    Links for today.
    Lesson 1 Lesson 2 Lesson 3 Lesson 4

    Monday, May 18

    You are a robot and you want to shuffle a deck of cards. You have a couple of rules as a robot:

    1. All the cards start in the left stack
    2. You can only hold one card at a time
    3. You only have enough desk space for two stacks of cards
    4. You can pick a card from anywhere in a stack, but you can only put a card down on top of a stack

    Work with your team to write instructions that will allow you to shuffle the deck of cards.

    Wednesday, May 27

    1. Create a new page on your neocities site and open it in the editor
    2. Delete everything on that page
    3. Copy everything from https://gist.githubusercontent.com/ertyseidohl/fe01f581b2071c8dfb85/raw/c024487eae3bb4188c1adb5ac8266c956c8878b9/gistfile1.txt and paste it into the now empty page
    4. Save that page and run it. It should ask you for a number and then perform various operations on that number.

    Wednesday, June 3

    1. If n1 is n, and n2 is n * n, what is n3?
    2. What is n4?
    3. What is n1/2? (extra credit)
    4. What is n0? (extra credit)
    5. What is the difference between 5 and 10? 52 and 102? 53 and 103?