/assignment

any assignment from Awesome people goes here

Primary LanguageJavaScript

assignment

any assignment from Awesome people goes here

First Example

function forEach(array, fn) {
	for (var i=0; i < array.length; i++) {
		var value = array[i];
		fn(value);
	}
}

function addNumber(array) {
	var result = 0;
	var fn = function(value) {
		result = result + value;
	};
	forEach(array, fn);
	
	return result;
}

var testResult = addNumber([1, 3, 5]);
console.log('should be nine:', testResult);

First Assignment

  • nolink
function filter(array, fn){
	var result = [];
	// TODO: return a new array with only the value that pass the fn test. (meaning the fn returned true)
	return result;
}

var testArr1 = [1, 2, 3, 4];

var evenNumbers = filter(testArr1, function(values){
	var isEven = (values % 2 === 0); // true if value is even. otherwise false.
 	return isEven;
});

console.log('This Should be [2, 4]:', evenNumbers);

Answers:

  1. this is the first solution I came up with: - see on repl.it COqm/0 or gist COqm-0
  2. this one I experiment using forEach - repl.it COsg/0 or gist COsg-0
  3. also forEach but cleaner - repl.it COsg/1 or gist COsg-1

Second Assignment

let items = [
	{id: 'a1', name: 'pisang', price: 22},
	{id: 'b2', name: 'keju', price: 34},
	{id: 'c3', name: 'roti', price: 11},
];

function getItemsByID(items) {
	let itemsByID = {};
	// TODO: Write some logic to put the items into the object by ID so it looks like:
	//   {'a1': 'pisang', 'b2': 'keju', 'c3': 'roti'}
	return itemsByID;
}

Answers:

  1. First confused why this doesn't work - repl.it CPwy/1 or gist CPwy-1 <== this one I forgot to input function call parameter, that's why I confused.
  2. Same as the first one but I try using a var - repl.it CPwy/2 or gist CPwy-2
  3. Final answer - repl.it CPwy/3 or gist CPwy-3

most repl.it session are cloned in gist.github https://gist.github.com/arksy

License

github told me to make license, this reminds me to make it later.