Arrays 01
-
Create 3 empty arrays
e1
,e2
,e3
, in 3 different ways. -
Create 2 arrays
a1
anda2
, containing a single element1
, in 2 different ways.
To see the solution, enter a valid code or purchase one:
This collection of 73 exercises is about the basics of the JavaScript language. It is of great value for beginners and intermediate programmers who want to be quite good at JavaScript.
Bite-sized, tested and vetted exercises only: no storytelling, no riddle, minimal use of math (basic arithmetic).
e1
,
e2
, e3
,
in 3 different ways.
a1
and
a2
,
containing a single element 1
, in 2 different ways.
To see the solution, enter a valid code or purchase one:
const a = [1, 2, 3, 4, 5];
Create 3 arrays a1
, a2
and a3
in 3 different ways so that they contain the same elements as a
and so that they are independent from one another. Mutating the array a
should not mutate any of the 3 other arrays.
To see the solution, enter a valid code or purchase one:
let letters = [...'Elephant'];
Iterate through the letters
array to display each letter to the screen. Do it in 5 different ways.
To see the solution, enter a valid code or purchase one:
const numbers_1 = [1, 2, 3];
const numbers_2 = [4, 5, 6];
Concatenate the 2 arrays numbers_1
and numbers_2
and assign the result to a new variable called numbers
.
To see the solution, enter a valid code or purchase one:
const numbers = [0, 1, 2, 3, 4, 5];
Create 3 new arrays doubled_1
, doubled_2
,
doubled_3
that have doubled values compared to numbers
. Use 4 different ways to do so.
To see the solution, enter a valid code or purchase one:
const a = [5, 4, 3, 2, 1];
Create a new array b
that contains the values from a
that are even.
To see the solution, enter a valid code or purchase one:
const a = [1, 2, 3, 4, 5];
Find the 1st element in a
that is greater or equal to 3
and assign the value to a new variable b
.
To see the solution, enter a valid code or purchase one:
const a = [1, 2, 3, 4, 5];
Find the index of the 1st element that is even
and assign the value to a new variable b
.
To see the solution, enter a valid code or purchase one:
const a = [1, 2, 3, 4, 5];
Check if all values of a
are greater or equal to 10
and store the resulting value into a new variable all
.
To see the solution, enter a valid code or purchase one:
const numbers = [0, 1, 2, 3, 4, 5];
const words = ['Pizza', 'Cheesy Cannelloni', 'Osso buco'];
reduce()
to calculate the sum of all
values contained in the array numbers
.
Assign the result to a new variable sum
.
reduce()
to find the longest word in the array words
.
Assign the result to a new variable longest
.
To see the solution, enter a valid code or purchase one:
const numbers = [0, 1, 2, 3, 4, 5];
Create a new array selected
that contains all values of numbers
from index 1 to index 3.
To see the solution, enter a valid code or purchase one:
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
Delete the values 2
and 3
in numbers
and replace them with
'a'
and 'b'
.
To see the solution, enter a valid code or purchase one:
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
Delete all values from numbers
, starting at index 4
.
numbers
should only contain the values of indices 0, 1, 2 and 3.
To see the solution, enter a valid code or purchase one:
const a = [1, 2, 3, 4, 5];
Check if any value of a
is negative
and store the resulting value into a new variable any
.
To see the solution, enter a valid code or purchase one:
Cat()
,
that can set the properties _age
and
_name
of any instance of a cat.
jump()
that can be common to all cat instances.
Do not modify the code of the Cat()
constructor function you already declared.
The jump()
method should log the string 'Fiuuuuu!'
.
felix
named 'Felix'
and aged 3
.
tom
named 'Tom'
and aged 5
.
Cat
.
felix
an tom
in 2 separate statements.
To see the solution, enter a valid code or purchase one:
_name
, an _age
and that can bark
.
The bark()
method should display the string 'Woof!'
.
toby
named 'Toby'
and aged 2
.
fox
named 'Fox'
and aged 4
.
To see the solution, enter a valid code or purchase one:
Rabbit
,
with the keyword class
to create a class of rabbits
that have a _name
, an _age
and that can eat
.
The eat()
method should log the string 'Crr crr crr'
.
The class should also have a getter method name
to get the
_name
of a rabbit.
bugs
named 'Bugs'
and aged 1
.
Roger
named 'Roger'
and aged 3
.
To see the solution, enter a valid code or purchase one:
const light = 'green';
let action = '';
if (light === 'green') {
action = 'Go';
} else if (light === 'yellow') {
action = 'Slow down';
} else if (light === 'red') {
action = 'Stop';
} else if (light === 'blue') {
action = 'Pull over';
} else {
action = 'Look at the sky';
}
Rewrite the previous code by using a switch
statement.
To see the solution, enter a valid code or purchase one:
const light = 'green';
let action = '';
if (light === 'green') {
action = 'Go';
} else {
action = 'Stay still';
}
Rewrite the previous code by using the ternary operator.
To see the solution, enter a valid code or purchase one:
let a;
if (a) {
console.log('a');
}
let b = a;
if (b = {}) {
console.log('b');
}
let c = [''];
if (!c) {
console.log('c');
}
if (!c[0]) {
console.log('d');
}
if (0) {
console.log('e');
}
What does the previous code display to the screen?
To see the solution, enter a valid code or purchase one:
const text = 'Elephant';
Convert the variable text
into a Boolean and store
the new value into a another variable called bool
.
Display to the screen the value of bool
.
To see the solution, enter a valid code or purchase one:
const text = '3.1415';
Convert variable text
into a
number and store the new value into another variable called num
.
To see the solution, enter a valid code or purchase one:
const bool = true;
Convert the variable bool
into a String and store the new value into another variable called text
.
To see the solution, enter a valid code or purchase one:
const num = 10;
Convert the variable num
into a String and store the new value into another variable called text
.
To see the solution, enter a valid code or purchase one:
const num = 3.1415;
Convert the variable num
into a String that represents the value
with only 2 digits after the decimal point.
Store the new value to another variable called text
.
To see the solution, enter a valid code or purchase one:
const text = '22';
Convert variable text
into a number and
store the new value into another variable called num
.
To see the solution, enter a valid code or purchase one:
Define 3 functions f1
, f2
and f3
that have 1 parameter text
and that display text
to the screen. Use 3 different ways to do it.
To see the solution, enter a valid code or purchase one:
Create an arrow function basic()
that simply returns the object { a: 1 }
.
To see the solution, enter a valid code or purchase one:
Define a function print
that can take an undefined number of arguments, and that logs each argument to the console.
To see the solution, enter a valid code or purchase one:
const singSong_1 = (song) => {
console.log(`This song "${song}" is great!`);
};
const singSong_2 = (song = 'Billy Jean') => {
console.log(`This song "${song}" is great!`);
};
singSong_1();
singSong_2();
What does the previous code display to the screen?
To see the solution, enter a valid code or purchase one:
// file-1.js
const sum = (x, y) => x + y;
const square = x => x * x;
const mean = data => data.reduce(sum) / data.length;
Export the function sum
by using the native node.js syntax
(also known as the CommonJS syntax).
To see the solution, enter a valid code or purchase one:
// file-1.js
const sum = (x, y) => x + y;
const square = x => x * x;
const mean = data => data.reduce(sum) / data.length;
Export the functions sum
and square
by using the native node.js syntax (also known as the CommonJS syntax).
To see the solution, enter a valid code or purchase one:
// file-1.js
const sum = (x, y) => x + y;
const square = x => x * x;
const mean = data => data.reduce(sum) / data.length;
module.exports = { sum, square };
Let us assume that there is a file file-2.js in the same directory as file-1.js.
Import the function sum
from file-1.js into file-2.js by using the native node.js syntax (also known as the CommonJS syntax).
To see the solution, enter a valid code or purchase one:
// file-1.js
const sum = (x, y) => x + y;
const square = x => x * x;
const mean = data => data.reduce(sum) / data.length;
Export the function sum
by using the ES6 syntax.
To see the solution, enter a valid code or purchase one:
// file-1.js
const sum = (x, y) => x + y;
const square = x => x * x;
const mean = data => data.reduce(sum) / data.length;
Export the functions sum
and square
by using the ES6 syntax.
To see the solution, enter a valid code or purchase one:
// file-1.js
const sum = (x, y) => x + y;
const square = x => x * x;
const mean = data => data.reduce(sum) / data.length;
export { sum };
Let us assume that there is a file file-2.js in the same directory as file-1.js.
Import the function sum
from file-1.js into file-2.js by using the ES6 syntax.
To see the solution, enter a valid code or purchase one:
// file-1.js
const sum = (x, y) => x + y;
const square = x => x * x;
const mean = data => data.reduce(sum) / data.length;
export default sum;
Let us assume that there is a file file-2.js in the same directory as file-1.js.
Import the function sum
from file-1.js into file-2.js by using the ES6 syntax.
To see the solution, enter a valid code or purchase one:
// file-1.js
const sum = (x, y) = > x + y;
const square = x = > x * x;
const mean = data => data.reduce(sum) / data.length;
export { sum, square };
Let us assume that there is a file file-2.js in the same directory as file-1.js.
Import the functions sum
and square
from file-1.js into file-2.js by using the ES6 syntax.
To see the solution, enter a valid code or purchase one:
const a = 0.4 - 0.3;
const b = 0.3 - 0.2;
console.log (a === b);
What does the previous code display to the screen?
To see the solution, enter a valid code or purchase one:
const a = ('a' / 1);
const b = (5 / 0);
const c = (-5 / 0);
console.log(a);
console.log(b);
console.log(c);
console.log(typeof a);
console.log(typeof b);
console.log(typeof c);
What does the previous code display to the screen?
To see the solution, enter a valid code or purchase one:
b
.
Check if b
is equal to NaN
.
c
. Check if c
is
a regular number (different than NaN
,
Infinity
and -Infinity
).
To see the solution, enter a valid code or purchase one:
Define a function randomIntegerBetween()
that has 2 integers as parameters,
min
and max
, and returns a random integer between
min
and max
inclusive.
To see the solution, enter a valid code or purchase one:
Create 3 empty objects a
, b
and c
in 3 different ways.
To see the solution, enter a valid code or purchase one:
const a = { a1: 1, a2: 2, a3: 3 };
a[Symbol.for("a4")] = 4;
let b = Object.create(a);
b.b1 = 5;
b[Symbol.for("b2")] = 6;
for (let p in b) {
console.log(p, b[p]);
}
What does the previous code display to the sreen?
To see the solution, enter a valid code or purchase one:
const a = {
a1: 1,
a2: 2,
countryCallingCodes: { US: 1, UK: 44 }
};
Create a new object b
, which is a deep copy
of the object a
. In other words, if we ever
mutate object a
, it does not affect object b
.
To see the solution, enter a valid code or purchase one:
const a = 1, b = 2, c= 3, d = 4, e = 5;
o
that has property names
a
, b
, c
, d
, e
, and property values
1
, 2
, 3
, 4
, 5
.
p
, but without using ES6/ES2015 shorthand property names syntax.
To see the solution, enter a valid code or purchase one:
const PROPERTY_NAME = 'p1';
const propertyValue = () => 'p' + 2;
Create an object o
that has properties names of p1
and p2
and values of 1
and
2
.
Use PROPERTY_NAME
and propertyValue()
to do so.
To see the solution, enter a valid code or purchase one:
Create an object o
that has a property _color
with a value of 'green'
, a simple getter method and a simple setter method.
Display to the screen the color of o
,
then change it to 'blue'
and finally display the new color of o
to the screen.
To see the solution, enter a valid code or purchase one:
Display to the screen all the properties of the global object of your environment.
To see the solution, enter a valid code or purchase one:
const array = [...'abc'];
const object = { k1: 1, k2: 2, k3: 3 };
Without running the code, what does each of the following loop print to the screen?
// Loop 1
for (let value in array) {
console.log(value);
}
// Loop 2
for (let value of array) {
console.log(value);
}
// Loop 3
for (let value in object) {
console.log(value);
}
// Loop 4
for (let value of object) {
console.log(value);
}
To see the solution, enter a valid code or purchase one:
const letters = [...'abcdefghijklmnopqrstuvwxyz'];
const randomIndex = Math.floor(Math.random() * letters.length);
const khaled = Object.assign(
{},
{
[letters[randomIndex]]: 'major key'
}
);
console.log(khaled);
Write some code to check if the object khaled
has the key 'b'
.
If it does, display to the screen 'Another one'
.
Otherwise, display to the screen "Don't play yourself"
.
To see the solution, enter a valid code or purchase one:
const twister = 'Betty Botter bought some butter';
Assign the first 3 characters of the string twister
to a variable first_3
.
To see the solution, enter a valid code or purchase one:
const danger = 'Croco-croco-dile';
'o'
of the string
danger
to a variable a
.
'co'
of
the string danger
to a variable b
.
To see the solution, enter a valid code or purchase one:
const tall = 'Giraffe-raffe-gi';
tall
starts with 'Gir'
and assign the return value to a variable a
.
tall
ends with '-gi'
and assign the return value to a variable b
.
To see the solution, enter a valid code or purchase one:
const numbers = '12345 34567 56789';
Check if the string numbers
includes the characters '345'
and assign the return value to a variable t
.
To see the solution, enter a valid code or purchase one:
const symetry = '--------0|0--------';
Replace the '-'
with '_'
in the string symetry
and assign the return value to a variable t
.
To see the solution, enter a valid code or purchase one:
const bob = 'I shot the sheriff';
bob
to lower case and assign the return
value to a variable lower
.
bob
to upper case and assign the return
value to a variable upper
.
To see the solution, enter a valid code or purchase one:
const franck = 'Singing in the rain';
Assign the character at index 5 of the string franck
to a variable t
.
To see the solution, enter a valid code or purchase one:
const snoop = 'Calvin';
Asign the ASCII code of the character at index 4 of the string snoop
to a new variable t
.
To see the solution, enter a valid code or purchase one:
const heisenberg = "I'm in the empire business";
heisenberg
and assign the new
value to a variable a
.
heisenberg
and assign the new value
to a variable b
.
'*'
at the beginning of heisenberg
and
assign the new value to a variable c
.
'!'
at the end of heisenberg
and assign
the new value to a variable d
.
To see the solution, enter a valid code or purchase one:
const birdy = " Flying pelican ";
birdy
and assign the new
value to a variable a
.
birdy
and assign the new
value to a variable b
.
birdy
and assign the new
value to a variable c
.
To see the solution, enter a valid code or purchase one:
const u = 'Talk ';
const v = 'that talk RiRi! '
u
and v
and assign the value to a new variable a1
.
u
and v
in a different way and assign the value to a new variable a2
.
a
3 times
and assign the value to a new variable b
.
To see the solution, enter a valid code or purchase one:
const a = 'one-two-three';
const b = "one-\
two-\
three";
const c = `one-
two-
three`;
const d = "one-\ntwo-\nthree";
console.log(a === b);
console.log(b === c);
console.log(c === d);
What does the previous code print to the screen?
To see the solution, enter a valid code or purchase one:
const love = '❤️❤️❤️';
const mula = '$$$';
console.log(love.length);
console.log(mula.length);
What does the previous code print to the screen and why is that?
To see the solution, enter a valid code or purchase one:
const a = 'caf\u{e9}';
const b = 'cafe\u{301}';
console.log(a);
console.log(b);
If you run the previous code, you see the string 'café'
logged twice on the screen.
If we add console.log(a === b);
at the end of the previous code,
what would be displayed to the screen?
To see the solution, enter a valid code or purchase one:
let [a, b] = [1];
const [c, d] = [2, 3, 4];
[a, b] = [b, a];
console.log(a);
console.log(b);
console.log(c);
console.log(d);
What does the previous code display to the screen?
To see the solution, enter a valid code or purchase one:
let [e, ...f] = [5, 6, 7, 8];
let [...g] = ['Pluto'];
let [first, ...last] = 'abcd';
console.log(e);
console.log(f);
console.log(g);
console.log(first);
console.log(last);
What does the previous code display to the screen?
To see the solution, enter a valid code or purchase one:
const color = {red: 0, green: 10, blue: 20, alpha: 1};
let { yellow, red, blue, alpha } = color;
console.log(yellow);
console.log(red);
console.log(blue);
console.log(alpha);
What does the previous code display to the screen?
To see the solution, enter a valid code or purchase one:
let barrow_1 = 'Bonnie';
const barrow_2 = 'Clyde';
barrow_1 = 'Raymond';
console.log(barrow_1);
barrow_2 = 'Ralph';
console.log(barrow_2);
What does the previous code display to the screen?
To see the solution, enter a valid code or purchase one:
What variable name is a valid name in JavaScript?
1elephant
tallGiraffe_1
$crocodile
nice-athletic-camel
wise_small_hippo
zebra2
new
To see the solution, enter a valid code or purchase one:
const a = ['Giraffe', 10, false, null];
const [b, c, d, e] = a;
const f = a[4];
const g = function() {
return true;
};
const h = { key_1: 1, key_2: 2 };
const i = Symbol.for('Have you ever used a Symbol?');
const j = 10n;
const values = [a, b, c, d, e, f, g, h, i, j];
for (let value of values) {
console.log(typeof value);
}
What does the previous code display to the screen?
To see the solution, enter a valid code or purchase one:
let dog = 'Spartacus';
let cat;
{
let dog = 'Crixus';
cat = 'Oenomaus';
squirrel = 'Ashur';
}
const reassign = cat => cat = 'Barca';
const changeName = newDog => dog = 'Agron';
reassign(cat);
changeName(dog);
console.log(dog);
console.log(cat);
console.log(squirrel);
What does the previous code print to the screen?
To see the solution, enter a valid code or purchase one:
let shouldContinue = true;
setTimeout(() => { shouldContinue = false; }, 200);
while(shouldContinue === true) {
console.log('Looping, looping, looping...')
};
What does the previous code display to the screen?
To see the solution, enter a valid code or purchase one: