Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pure function and data immutability in functional programming approach #14

Open
mhaidarhanif opened this issue Jul 27, 2018 · 0 comments

Comments

@mhaidarhanif
Copy link
Contributor

let names = ["Haidar", "Hanif"];

// Helper function
const addExclamation = text => {
	const transformedText = text + "!!!";
	return transformedText;
};

// Good example
const displayFormatted = array => {
	return array.map(item => {
		return addExclamation(item);
	});
};

// Bad example
const displayFormattedBad = array => {
	for (let index = 0; index < array.length; index++) {
		array[index] += "!!!";
		const element = array[index];
		console.log(element);
	}
};

const resultNames = displayFormatted(names);

// Avoid changing the data directly
// displayFormattedBad(names);

console.log(names);
console.log(resultNames);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants