Skip to content
Kevin Boere edited this page Oct 17, 2023 · 22 revisions

Opdracht week 1: Dinsdag

Exercise 1

The following array contains multiple data types. Normalize them by converting them all to numbers using a function and log the result in the console

Given Example

My Solution

const data = [
	1,
	2,
	"3",
	"4",
	5
]

function convertArrayStringsToNumbers() {
	/* Your code here should convert the data array to 
        an array containing only numbers and no strings 
        and log the code to the console. */
}

convertArrayStringsToNumbers();
const data = [
	1,
	2,
	"3",
	"4",
	5
]

function convertArrayStringsToNumbers() {
	/* convert the array into only numbers */
        let newArray = data.map(Number);
        
        /* log the array */
        console.log(newArray);
}

convertArrayStringsToNumbers();

Exercise 2

The following array contains strings with randomized capitals. You're tasked with converting all strings to lowercase with a first letter capitalized

Given Example

My Solution

const data = [
	"robert",
	"vincent",
	"lAuRa",
	"Cas",
	"wIMER",
	"rOOs"
];

/* This should the result be:

	const data = [
		"Robert",
		"Vincent",
		"Laura",
		"Cas",
		"Wimer",
		"Roos"
	];

*/


function convertArrayStringsToCapitalized() {
	/* Write your functionality here and log the result */
}

convertArrayStringsToCapitalized();
const data = [
	"robert",
	"vincent",
	"lAuRa",
	"Cas",
	"wIMER",
	"rOOs"
];

function convertArrayStringsToCapitalized() {
	/* Make a new array and convert first item into a upper case then the other characters should get a lower case */
        const newArray = data.map(string => string.charAt(0).toUpperCase() + string.slice(1).toLowerCase());

        console.log(newArray);

}

convertArrayStringsToCapitalized();

Exercise 3

This array of objects contains some weird data, and some useless points, You're tasked in transforming and normalizing this data to the second example

Given Example

My Solution

const data = [
	{
		name: "robert",
		age: "29",
		residence: "amsterdam",
		work: {
			title: "Lecturer",
			employer: "Hogeschool van Amsterdam"
		}
	},
	{
		name: "berend",
		age: "32",
		residence: "rotterdam",
		work: {
			title: "Front-end Developer",
			employer: "DEPT"
		}
	},
	{
		name: "ubaida",
		age: "26",
		residence: "Amersfoort",
		work: {
			title: "Project Manager",
			employer: "Clarify"
		}
	}
];


/* Filter by age, normalize capitals in names, convert ages to 
numbers, remove work.

	const data = [
		{
			name: "Robert",
			age: 29,
			residence: "Amsterdam",
		},
		{
			name: "Berend",
			age: 32,
			residence: "Rotterdam",
		}
	];

*/

function transformArrOfObj() {
	/* Write your functionality here and log the result */
}
const data = [
	{
		name: "robert",
		age: "29",
		residence: "amsterdam",
		work: {
			title: "Lecturer",
			employer: "Hogeschool van Amsterdam"
		}
	},
	{
		name: "berend",
		age: "32",
		residence: "rotterdam",
		work: {
			title: "Front-end Developer",
			employer: "DEPT"
		}
	},
	{
		name: "ubaida",
		age: "26",
		residence: "Amersfoort",
		work: {
			title: "Project Manager",
			employer: "Clarify"
		}
	}
];

function transformArrOfObj(data) {
  return data
    /* when age is not a number you can filter out that whole object
    .filter(item => !isNaN(Number(item.age)))
    */

    .map(obj => ({
      name: obj.name.charAt(0).toUpperCase() + obj.name.slice(1).toLowerCase(),
      age: Number(obj.age),
      residence: obj.residence
    }));
}

console.log(transformArrOfObj(data));

Clone this wiki locally