How Much is True? || JavaScript

How Much is True? || JavaScript

Create a function which returns the number of?true?values there are in an array.

Examples

countTrue([true, false, false, true, false]) ? 2

countTrue([false, false, false, false]) ? 0

countTrue([]) ? 0        

Notes

  • Return?0?if given an empty array.
  • All array items are of the type bool (true?or?false).

First solution:

No alt text provided for this image

Second solution :



function countTrue(arr){
	var count = 0;
	for(var i = 0; i < arr.length; i++){
		if(arr[i] === true){
			count++;
		}
	}
	return count;
}
        

要查看或添加评论,请登录

Ahmed Khaleel的更多文章

社区洞察

其他会员也浏览了