Hi,
I am on functional programming on JS course in FC. One thing that confuses me is when to return more than once? take for example my code:
function checkPositive(arr) {
return arr.every(function(element) {
return element > 0;
});
}
is there a better way to understand when to use the return statement more than once and and the logic behind when how you know?
thanks
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36
Challenge: Use the every Method to Check that Every Element in an Array Meets a Criteria
Link to the challenge:
ugwen
2
log things to see what is happening. for example:
function check(arr) {
console.log('started func')
return arr.every(function(e) {
console.log(e);
return e < 10});
}
console.log( check([1, 2, 3, -4, 5]) )
change the test to see what happens, eg: e>=0
or e=5
ugwen
3
or
function check(arr) {
let testLog = false
testLog = arr.every(function(e) {console.log(e);return e < 10})
console.log('inside testLog:',testLog)
return testLog
}
console.log( 'result:', check([1, 2, 3, -4, 5]) )
or look at it like this:
function check(arr) {
function test(e) {
return e >= 0;
}
const testLog = arr.every(test)
return testLog
}
console.log( 'result:', check([1, 2, 3, -4, 5]) )
or:
function test(e) {
return (e > 4);
}
function check(arr) {
const testLog = arr.every(test)
return testLog
}
console.log( 'result:', check([1, 2, 3, -4, 5]) )
system
Closed
4
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.