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);
- Create a variable that is 24 times 55
- Create a const and set it to be equal to your name
- With javascript
console.log
the first character in your name - Create an array with 3 strings, three numbers and three booleans
console.log
the 4. element in the array made above- Optional with javascript
console.log
the last character in your name.
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];
- Create a special new folder called "pizza-exercise"
- Inside the folder create a new html file called "index.html"
- Also inside the folder create a new JavaScript file called "pizza.js"
- Remember to Include the pizza.js script in the html file
- Write a log statement, so you know that your javascript code is running:
console.log("I love pizza");
- Create a variable to store the name of your favourite pizza
- Create a variable to store the price of the pizza
- 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>
Now we will modify the program so that you can order multiple pizzas and also decide if the pizzas should be family size
-
Create a new variable to store the amount of pizzas you would like to order
-
Create a new variable to store whether or not you would to order a family size pizza.
-
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.
-
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>
-
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.
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;
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.
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();
Create a for loop that logs out the numbers from 74 - 98
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
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);
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
}
*/
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: [],
}
*/
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?
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);
}