ds-algo-study
Version:
Just experimenting with publishing a package
52 lines (42 loc) • 1.47 kB
JavaScript
/*******************************************************************************
Write a function `one` that accepts an array and a callback as arguments. The
function should call the callback for each element of the array, passing in the
element and its index. The function should return a boolean indicating whether
or not exactly one element of the array results in true when passed into the callback.
Examples:
let result1 = one(['x', 'y', 'z'], function(el) {
return el === 'a';
});
console.log(result1); // false
let result2 = one(['x', 'a', 'y', 'z'], function(el) {
return el === 'a';
});
console.log(result2); // true
let result3 = one(['x', 'a', 'y', 'a', 'z'], function(el) {
return el === 'a';
});
console.log(result3); // false
let result4 = one(['apple', 'dog'], function(el) {
return el.length > 3;
});
console.log(result4); // true
let result5 = one(['apple', 'dog', 'pear'], function(el) {
return el.length > 3;
});
console.log(result5); // false
let result6 = one(['apple', 'dog', 'food', 'cat'], function(el, idx) {
return el.length === idx;
});
console.log(result6); // true
*******************************************************************************/
function one(array, cb) {
let count = 0;
array.forEach(function(el, i) {
if (cb(el, i, array) === true) {
count++;
}
});
return count === 1;
}
//******************---------------------******************\\*/
module.exports = one;