Skip to content

Instantly share code, notes, and snippets.

@zkwsk
Last active November 7, 2019 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zkwsk/8369e31d22410ef153f887034ddfc40f to your computer and use it in GitHub Desktop.
Save zkwsk/8369e31d22410ef153f887034ddfc40f to your computer and use it in GitHub Desktop.

Variable types

With pen and paper write down what is logged out:

// Types
console.log(typeof 3);
console.log(typeof -33);
console.log(typeof '3');
const threeConst = 3;
console.log(threeConst);
let threeLet = 3;
console.log(threeLet);
console.log(typeof 'console.log("console.log(console.log(""))")');
console.log(typeof typeof 45);
const names = ['benjamin', 'Christopher'];
console.log(typeof names[0]);
console.log(typeof [3][0]);
console.log(typeof true);

Follow up exercises

  1. Create a variable that is 24 times 55
  2. Create a const and set it to be equal to your name
  3. With javascript console.log the first character in your name
  4. Create an array with 3 strings, three numbers and three booleans
  5. console.log the 4. element in the array made above
  6. Optional with javascript console.log the last character in your name.

Fix the errors

Fix the errors in this script:

const name = 'benjamin';
name = 'benjamin-better';

const pizzaPrice = 78;
const pizzaPriceDiscounted = pizzaprice - 10;

const users = ['peter', 'Johnny', 'Børge'];

const lastUser = users[3];

Pizza project

Part 1

  1. Create a special new folder called "pizza-exercise"
  2. Inside the folder create a new html file called "index.html"
  3. Also inside the folder create a new JavaScript file called "pizza.js"
  4. Remember to Include the pizza.js script in the html file
  5. Write a log statement, so you know that your javascript code is running: console.log("I love pizza");
  6. Create a variable to store the name of your favourite pizza
  7. Create a variable to store the price of the pizza
  8. Now log at statement to the console that will show the pizza man the entire pizza order in a language he understands, eg. like this: New pizza order: <name of pizza>. The price of the pizza is: <price of pizza>

Part 2

Now we will modify the program so that you can order multiple pizzas and also decide if the pizzas should be family size

  1. Create a new variable to store the amount of pizzas you would like to order

  2. Create a new variable to store whether or not you would to order a family size pizza.

  3. Now write a formula to calculate the total price of your pizza order, and save it in a variable called totalPrice (if the pizza is family size the prize of the pizza is doubled.

  4. Modify the log statement for the pizza man so it includes wether or not the pizza is family size, and now show the total price of the order New pizza order: <amount of pizzas> <family or not?> <name of pizza>. Total cost for the order is: <total price>

  5. Try to change the price of the pizza and if the pizza should be family size, and then check if the total price is calculated correctly.

If sentences

Create an if sentence that will give a user a message based on his bank account balance. Use the balance variable and change that.

  • If a user has less that 0 log out 'Please earn some money!'
  • If a user has between 0 and 1000 log out 'Your balance is looking okay'
  • If a user has between 1000 and 3000 log out 'Your balance is looking good'
  • If a user has between 3000 and 10000 log out 'Your balance is fantastic'
  • If a user has more than 10000 log out 'Your balance is AMAZING!'
const balance = 1000;

Function

Create a function called getCircleArea. It should have the radius of the circle as parameter and return the circle area. What happens if we dont return anything in the function?

Create a function called celciusToFahreneit it should have a parameter called celcius. It should return the temperature in fahrenheit.

Try call the function and check with google if the function returns the right value.

Scope

With pen and paper write what is logged out.

const global = 'global';
function scopeTest() {
    console.log(functionScope);
    console.log(global);
    const functionScope = 'functionScope';

    function tester() {
        console.log(global);

        const testerVariable = 'testerVariable';
    }

    tester();
    console.log(testerVariable);
}

scopeTest();

For loop

Simple for loop

Create a for loop that logs out the numbers from 74 - 98

For loop in a function

Create a function that has two parameters: stringToLog and numberOfTimesToLog

When calling the function it should log out the stringToLog the amount of times specified in numberOfTimesToLog. Use a for loop.

logString('hello', 3);
// hello
// hello
// hello

Fizz buzz

This is a classic programming task.

Create a function that prints the numbers from 1 to 100. But for multiples of three print Fizz instead of the number and for the multiples of five print Buzz. For numbers which are multiples of both three and five print FizzBuzz.

When that works. Make the two number for multiples into parameters. So it can be called like this:

fizzBuzz(4, 12);

Character frequencies

Write a function that counts the frequency of characters in a string:

console.log(getCharacterFrequencies('happy'));
/*
{
  characters: [
    {
      character: 'a',
      count: 1
    },
    {
      character: 'h',
      count: 1
    },
    {
      character: 'p',
      count: 2
    },
    {
      character: 'y',
      count: 1
    }
  ], length: 5
}
*/

Build a sentiment analyzer

A sentiment analyzer is some functionality that figures out how positive/negative a sentence is.

Fx the sentence `I am mega super awesome happy" Should have a high score The sentence "I hate doing boring stuff" should have a low score.

Create a function that takes a string as a parameter. calling the function will return an object with score, positiveWords and negativeWords. You decide how the score should be implemented and what words are negative and positive.

Here is an example of using the function:

const sentimentScoreObject = getSentimentScore('I am mega super awesome happy');

console.log(sentimentScoreObject); 
/*
{
  score: 3,
  positiveWords: ['happy', 'awesome', 'super'],
  negativeWords: [],
}
*/

Credit card number formatter

This is a very real world example of a problem i got at my previous work. I was tasked to implement one of the smart credit card input fields, where the credit card numbers are seperated with a space. Fx inputting 123456789 would show 1234 5678 9.

Create a function that takes a number as parameter. The function should return the following object:

const formattedCreditCardObject = formatCreditCardNumber(123456789);
console.log(formattedCreditCardObject);
/*
{
  original: 123456789,
  formatted: "1234 5678 9",
}
*/

Thins to consider:

  • What should happen if the function is called with an argument that is not a number?

Send emails

Imagine we work at a company. Peter from the HR department wants us to send out a couple of emails to some recepients. The only problem is that he sent us the email in a weird format: benjamin@gmail.com|peter@gmail.com|hans@gmail.com|ahmad@gmail.com|sana@gmail.com|virgeen@gmail.com|mohammed@gmail.com

Use the sendEmailTo function to send an email to all the recepients that we got from Peter.

Hint use the .split method and look up iterating an array js for loop on google.

// This function emulates sending emails to receipients
function sendEmailTo(recepient) {
    // But really it only logs out a string
    console.log('email sent to ' + recepient);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment