7. Data Cleaning - TimvanAlphen/functional-programming GitHub Wiki

Trying out .map and .filter

(Week 1, 19-10-20)

For this exercise I wanted to see if I could inplement a .map and .filter function that will actually work. First I loaded in the data with help from the lecture by Laurens.

//The survey data is loaded from an .js file.
const surveyAnswers = data

//Variables with the surveyAnwsers and collumnname of "vooropleiding"
let kolomNaam = "vooropleiding"
let lijstAntwoorden = getAnswersForQuestion(surveyAnswers, kolomNaam)

//console.log van de lijst met antwoorden.
console.log("Survey answers are:",lijstAntwoorden)


//Function that saves an array with the answersForQuestions and pushes them to the variable "lijstAntwoorden".
function getAnswersForQuestion(answers, question){
	let answersForQuestion = []
  for (answer of answers){
  	answersForQuestion.push(answer[question])
 }
	return answersForQuestion
}

A filter that goes trough the "lijstAntwoorden" varable and returns only 'Havo'

With help from the Youtube video: (https://www.youtube.com/watch?v=BMUiFMZr7vk&ab_channel=FunFunFunction) I made made a .filter that goes trough the let "lijstAntwoorden" and only returns the answers 'HAVO'.

//The filter function only returns 'MBO' Bron: https://www.youtube.com/watch?v=BMUiFMZr7vk&ab_channel=FunFunFunction
let niveau = lijstAntwoorden.filter(function(lijstAntwoorden) {
	return lijstAntwoorden === 'HAVO'
})

A map that adds an emoji "=)" after MBO, HAVO or VWO.

Using a different video by the same creator: (https://www.youtube.com/watch?v=bCqtb-Z5YGQ&t=300s&ab_channel=FunFunFunction) I made a .map wich goes trough the let "niveau" and adds an "=)" afer all the awnsers.

//Map adds an eomji "=)" after MBO.
let emoji = niveau.map(function(niveau){
	return niveau + ' =)'
})

console.log("Niveau is:" ,niveau)

console.log(emoji)

(Week 2, 26-10-20)

I tried cleaning up the row "oogKleur" using .map and .filter. Here i used a .map to change all the the awnsers in "lijstAntwoorden" to uppercase with "toUpperCase". Then I used .filter to only show the awnsers that include 7 characters in "upperCased" because hex values have 7 characters. Now the awnsers in the "oogkleur" row are shown in capital letters and the awnsers that arent 7 characters long are filtered out.

//A .map that changes the variable "lijstAntwoorden" to upper case.
let upperCased = lijstAntwoorden.map(lijstAntwoorden => lijstAntwoorden.toUpperCase());
//A .filter that removes the strigs in the variable "upperCased" that aren't 7 symbols long.
let correctHexValues = upperCased.filter(upperCased => upperCased.length == 7)



	console.log("Uppercased dataset:" ,upperCased)
	console.log("Only 7 symblos:" ,correctHexValues)

With a lot of help from Gijs Laarman I tried to clean up the list of eye colors with a forloop. This function contains a forloop that goes trough all the colors in the list of eye colors and checks if the awnser start with a "#" and is 7 characters long wich is then put in a variable that makes the colors uppercase with .toUpperCase(). Than the hexcolors are pushed to the variable filteredData with an array.

console.log('for loop: ', filterEyeColorsForLoop(lijstAntwoorden))


function filterEyeColorsForLoop(listOfEyeColors) {
// Create an array for my filtered data.
	let filteredData = []

	for (const color of listOfEyeColors) { 
		// Check if the hex code is valid.
		if (color.startsWith('#') && color.length === 7) {
			// Change hex to uppercase
			const hexColor = color.toUpperCase()

			// Add color to filtered list
			filteredData.push(hexColor)
		}
     }

    return filteredData
}

Again with help from Gijs Laarman I tried to do the same thing I tried before with a forloop but this time using .map and .filter. This function has a variable that transforms the listOfEyeColors to upper case with a .map function. It also has a variable that filters the upper cased awnsers using .filter and using an if statement it looks if the awnsers start with a "#" and looks if the lengts of the awnsers are 7 characters long. Wich is then returned to correctHexValues.

// Loop through answers and make all items uppercase.
function filterEyeColors(listOfEyeColors) {
	// Transform every eye color to uppercase.
	let upperCased = listOfEyeColors.map(color => color.toUpperCase())

	// Filter out all invalid Hex codes.
	let correctHexValues = upperCased.filter(color => {
		if (color.startsWith('#') && color.length === 7) {
			return color
		}
	})

	return correctHexValues
}



console.log('correctHexValues: ', filterEyeColors(lijstAntwoorden))