Skip to content

Practice 1 - In-class tasks

webprogramming language-features arrays in-class

These are the exercises we work through together during the first practice. Each one is a small, self-contained function — there is no story to follow and no application to build. We start with loops and conditions, then solve some of the same problems again with array methods.

Work through them in order. We will not finish all of them in class; whatever is left is yours to complete at home.

How to run it

Download the starter files, open the folder in VS Code, then right-click index.html and choose Open with Live Server. The page itself is empty on purpose — press F12 and switch to the Console tab. That is where you will see everything.

How to check your work

There are no automatic tests here. You check your own work: write the function, then call it and print the result.

function double(n) {
  return n * 2;
}

console.log(double(21));

The Console shows 42. Every task below ends with a call and the value it should print — compare what you see against that line. The starter file already contains these calls, commented out; remove the // once you have written the function.

If the Console shows undefined, you almost certainly forgot to return something.

Operators you will need

7 % 2        // 1  - remainder after division
6 % 2        // 0  - so `n % 2 === 0` means "n is even"
2 ** 3       // 8  - 2 to the power of 3
5 === "5"    // false - strict equality: compares value AND type

Objects (for the last task only)

Everything up to task j uses arrays. Task j needs one new thing: an object. An object stores values under names instead of under positions:

const person = { name: "Alice", age: 23 };

console.log(person.name);   // "Alice"
console.log(person.age);    // 23

person.age = 24;            // change a value
person.city = "Budapest";   // add a new one

You can also build one up from scratch:

const counters = { small: 0, large: 0 };
counters.small = counters.small + 1;

console.log(counters);      // { small: 1, large: 0 }

That is all you need for task j.

Starter package

The task description and starter files, ready to work on locally.

Included files: index.htmlindex.js

Tasks

describeValue(value)

Builds a short sentence describing a value and its type.

  • Parameter: value: a value of any type.
  • Returns: a string in the form "<value> is a <type>", where the type is what the typeof operator reports.

Use a template literal, not + concatenation.

console.log(describeValue(42));
console.log(describeValue("hello"));
console.log(describeValue(true));

Should print:

42 is a number
hello is a string
true is a boolean

areSame(a, b)

Decides whether two values are truly identical.

  • Parameters: a and b: values of any type.
  • Returns: true if the two values have the same type and the same value; otherwise false.
console.log(areSame(5, 5));
console.log(areSame(5, "5"));
console.log(areSame("web", "web"));

Should print:

true
false
true

Once it works, try areSame(NaN, NaN) in the Console. The answer is surprising — we will talk about why.

sumAll(numbers)

Adds up every number in an array. Use a loop.

  • Parameter: numbers: an array of numbers.
  • Returns: the sum of all elements. Returns 0 for an empty array.
console.log(sumAll([1, 2, 3, 4]));
console.log(sumAll([]));

Should print:

10
0

sumEven(numbers)

Adds up only the even numbers. Use a loop with a condition inside it.

  • Parameter: numbers: an array of numbers.
  • Returns: the sum of the even elements. Returns 0 if there are none.
console.log(sumEven([1, 2, 3, 4, 5, 6]));
console.log(sumEven([1, 3, 5]));

Should print:

12
0

cubeAll(numbers)

Raises every number to the power of three and collects the results into a new array. Use a loop: create an empty array, then push each result into it.

  • Parameter: numbers: an array of numbers.
  • Returns: a new array containing the cube of each element, in the same order. The original array must not change.
console.log(cubeAll([1, 2, 3]));
console.log(cubeAll([]));

Should print:

[1, 8, 27]
[]

cubeAllWithMap(numbers)

Exactly the same job as cubeAll, but written with the map array method instead of a loop.

  • Parameter: numbers: an array of numbers.
  • Returns: a new array containing the cube of each element.

This function and your cubeAll from task e must return identical results for identical input. One is a loop, the other is a single line — that comparison is the point of the exercise.

console.log(cubeAllWithMap([1, 2, 3]));

Should print:

[1, 8, 27]

evenNumbers(numbers)

Keeps only the even numbers. Use the filter array method.

  • Parameter: numbers: an array of numbers.
  • Returns: a new array containing only the even elements, in their original order.
console.log(evenNumbers([1, 2, 3, 4, 5, 6]));
console.log(evenNumbers([1, 3, 5]));

Should print:

[2, 4, 6]
[]

cubeEvens(numbers)

Keeps the even numbers and cubes them. Chain filter and map together in one expression.

  • Parameter: numbers: an array of numbers.
  • Returns: a new array containing the cubes of the even elements.
console.log(cubeEvens([1, 2, 3, 4]));

Should print:

[8, 64]

Think about the order: filtering first and cubing second does less work than cubing first and filtering second.

mergeAndCube(first, second)

Joins two arrays together and cubes every number in the result. Use the spread syntax (...) to combine them.

  • Parameters: first and second: two arrays of numbers.
  • Returns: a new array containing the cubes of all elements of first followed by the cubes of all elements of second. Neither input array may be modified.
console.log(mergeAndCube([1, 2], [3]));
console.log(mergeAndCube([], [2]));

Should print:

[1, 8, 27]
[8]

countByParity(numbers)

Counts how many even and how many odd numbers an array contains, and reports both in a single object.

  • Parameter: numbers: an array of numbers.
  • Returns: an object with two properties, even and odd, each holding the count of numbers of that kind.
console.log(countByParity([1, 2, 3, 4, 5]));
console.log(countByParity([]));

Should print:

{ even: 2, odd: 3 }
{ even: 0, odd: 0 }

Requirements

  • Write each task as a function, in the form function name(...) { ... }.
  • Declare variables with const or let. Do not use var.
  • A function that returns an array must return a new array and leave its input unchanged.
  • Use strict comparison (=== and !==), not == and !=.
  • Before moving on to the next task, call the function and print the result — make sure you actually saw the expected value in the Console.