These exercises can be done in your browser. If you feel courageous enough, you can start with Part 2 and get tooling to work and then return to Part 1 to solve exercises in your text editor.
You don't need to do the exercieses in any particular order or do all of them. Try reading all of them before starting. You can also take a look at some entries under 'JavaScript Fundamentals' to get the general idea here: https://javascript.info/ , but definitely check or skim-read these first:
- https://javascript.info/variables
- https://javascript.info/operators
- https://javascript.info/logical-operators
- https://javascript.info/function-basics
- https://javascript.info/object
Also, feel free to message us on Discord.
Most exercises were taken from this source, feel free to practice with more of these if you feel like it: https://www.w3resource.com/javascript-exercises/javascript-basic-exercises.php
Write a program that takes a random integer between 1 and 10, and the user is then prompted to input a guess number. The
program displays (console.log()
or alert()
) a message "Good Work"
if the input matches the guess
number otherwise "Not matched"
.
Resources:
prompt()
andalert()
https://javascript.info/alert-prompt-confirm- random https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
Solution in the source: https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-8.php
Write a function to check a pair of numbers and return true if one of the numbers is 50 or if their sum is 50.
(advanced) What happens if one or both of the numbers is provided as string? What is the possible solution to this?
In an array of names const names = ["Anna", "Johannes", "Paula", "Daisy"]
, how would you:
- find the index for the name "Paula"?
- check if the name "Paula" is in the list?
- check if the name exists in the list if you could only search it as: "PAULA" or "paula"?
Check these:
Write a JavaScript function to create a string from a given string. This is done by taking the last 3 characters and adding them at both the front and back. The string length must be 3 or more.
For example:
addThree(umbrella) // -> llaumbrellalla
addThree(cap) // -> capcapcap
One solution: https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-26.php Maybe also check: https://javascript.info/ifelse
Write a program that returns an array of strings (const) consisting of
every name in names
(from Exercise 3) and their respective length
multiplied by two: ["Anna 8", "Johannes 16" ...]
Take a careful look at this!: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Create the following object:
let zooAnimals = {
"giraffe": { "weight": 910, "origin": "Tanzania" },
"lion": { "weight": 200, "origin": "Tanzania" },
"elephant": { "weight": 5000, "origin": "India" },
"penguin": { "weight": 30, "origin": "Argentina" },
"koala": { "weight": 10, "origin": "Australia" },
};
- How would you check if an animal exists in the object?
- How would you check if an animal with a specific weight or a specific origin exists in the object?
- How would you add a new animal?
- (advanced) Create an object method named "about" which generates
text about specified animal, e.g.
zooAnimals.about("giraffe") // -> "giraffe weights 910 kg and comes from Tanzania"
. If the animal is not in the zoo, return "we don't have this animal".
Tips: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics
-
Download and install NodeJS (LTS version).
-
Install Yarn dependency manager (you might have to use ’sudo’ for this to work). Run this in your Terminal:
corepack enable
-
Create Vite starter project:
yarn create vite
- specify the arbitrary but meaningful name
- select Vanilla framework
- select JavaScript variant
- follow the further instructions
-
You will see the link to the development instance, i.e. http://localhost:5173/. Open it. You should see the “Hello Vite!” webpage.
-
Study the
.js
,.css
and.html
files generated by Vite. Make sure you understand what’s going on there. Some questions to help you understand the files:- How are the files connected together?
- What method is used in the script files to retrieve and alter the HTML element?
-
Add XState to your project:
yarn add xstate
-
Follow this documentation: https://stately.ai/docs/xstate and move the program logic in
counter.js
to a state chart:- Information about the counter is stored in the context of your state chart.
- Event
INC
updates the counter. Clicking on the button should emit this event. - Log the counter following the example: https://stately.ai/docs/xstate#create-a-simple-machine
- Then, adjust you code so that the page contents are updated
according to the state. The variable
counter
should be removed from thesetupCounter(element)
function.
Resources:
Create an interface in HTML for Exercise 1. Replace alert()
(or
console.log()
) and prompt()
with appropriate HTML elements. You
should have a input box, a button to submit the input in the
box.
Edit the HTML so that you page looks better: write a title, write a short message that tells the user how to use your mini program, choose a background color and put your objects in the center of the page. Try checking how CSS can help you with this.
Check: