For Loop Transforming

Transforming is the process of changing the item of an array to another format.

You can go through all items in JavaScript using either:

  1. Traditional for loop
  2. for...of loop

for...of loop is easier to read, but you do not have access to index variable.

Exercise 1

Given an array of number (data parameter), and a number (num parameter), multiple each item in the array by the num parameter, then returns them.

Examples:

  • data: [2,3,2,6], num: 2 -> [4,6,4,12]
  • data: [5,3,1], num: 6 -> [30,18,6]
  • data: [7,7,7], num: 7 -> [49,49,49]
export default function multiplyAll(data, num) {
  // TODO
}

Open browser consoleTests

Exercise 2

Returns an array of string that describe each person in dataset in the format of '{name}, aged {age}'. If age is not available, returns '{name}' only.

An example of the dataset is

js
const dataset = [
{
name: 'Malcolm',
age: 32,
},
{
name: 'Esther',
age: 10,
},
{
name: 'Richie',
age: 50,
},
{
name: 'Audrey',
},
];

When the example is provided to the function, it should returns:

js
['Malcolm, aged 32', 'Esther, aged 10', 'Richie, aged 50', 'Audrey']
export default function getPersonsSummary(dataset) {
  // TODO
}

Open browser consoleTests

Exercise 3

Returns an array that contains the original items in the dataset, with each item is added with a isUnderage property, where person with age < 18 would have isUnderage: true, otherwise would be `false.

An example of the dataset is

js
const dataset = [
{
name: 'Malcolm',
age: 32,
},
{
name: 'Esther',
age: 10,
},
{
name: 'Richie',
age: 50,
},
{
name: 'Audrey',
}
];

When the example is provided to the function, it should returns:

js
[
{
name: 'Malcolm',
age: 32,
isUnderage: false
},
{
name: 'Esther',
age: 10,
isUnderage: true
},
{
name: 'Richie',
age: 50,
isUnderage: false
},
{
name: 'Audrey',
isUnderage: false
},
]
export default function addAgeTagging(dataset) {
  // TODO
}

Open browser consoleTests

Exercise 4

Given an array of strings representing integers, returns a new array where each element is an object with two properties, the original integer string and the integer value squared.

Example:

Given numberStrings is ['1', '2', '3', '4', '5'], the function should returns:

js
[
{
original: '1',
squared: 1,
},
{
original: '2',
squared: 4,
},
{
original: '3',
squared: 9,
},
{
original: '4',
squared: 16,
},
{
original: '5',
squared: 25,
},
]
export default function processIntegers(numberStrings) {
  // TODO
}

Open browser consoleTests

Exercise 5

Given an array of object representing products, where each object has id, name, and price property, returns a new array of objects with a discounted price by applying a 10% discount.

Example:

Given products is:

js
[{id: 5, name: 'orange', price: 10}, {id: 20, name: 'banana', price: 100}]

the function should returns:

js
[
{
id: 5,
name: 'orange',
price: 9
},
{
id: 20,
name: 'banana',
price: 90
}
]
export default function applyDiscount(products) {
  // TODO
}

Open browser consoleTests