Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@amandarfernandes
Created December 11, 2017 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amandarfernandes/4f7bbc188efbe8e81d8f4786ba8bc9fe to your computer and use it in GitHub Desktop.
Save amandarfernandes/4f7bbc188efbe8e81d8f4786ba8bc9fe to your computer and use it in GitHub Desktop.
JSArraySomeEvery
/*
Write a function called hasOddNumber which accepts an array and returns true if the array contains at least one odd number, otherwise it returns false.
Examples:
hasOddNumber([1,2,2,2,2,2,4]) // true
hasOddNumber([2,2,2,2,2,4]) // false
*/
function hasOddNumber(arr){
return arr.some(function(value){
return value % 2 !== 0;
});
}
/*
Write a function called hasAZero which accepts a number and returns true if that number contains at least one zero. Otherwise, the function should return false
Examples:
hasAZero(3332123213101232321) // true
hasAZero(1212121) // false
*/
function hasAZero(arr){
return (""+arr).split("").some(function(value){
return value === '0';
});
}
/*
Write a function called hasOnlyOddNumbers which accepts an array and returns true if every single number in the array is odd. If any of the values in the array are not odd, the function should return false.
Examples:
hasOnlyOddNumbers([1,3,5,7]) // true
hasOnlyOddNumbers([1,2,3,5,7]) // false
*/
function hasOnlyOddNumbers(arr){
return arr.every(function(value){
return value % 2 !== 0;
});
}
/*
Write a function called hasNoDuplicates which accepts an array and returns true if there are no duplicate values (more than one element in the array that has the same value as another). If there are any duplicates, the function should return false.
Examples:
hasNoDuplicates([1,2,3,1]) // false
hasNoDuplicates([1,2,3]) // true
*/
function hasNoDuplicates(arr){
return arr.every(function(value,index,array){
return array.lastIndexOf(value) === index
});
}
/*
Write a function called hasCertainKey which accepts an array of objects and a key, and returns true if every single object in the array contains that key. Otherwise it should return false.
Examples:
var arr = [
{title: "Instructor", first: 'Elie', last:"Schoppik"},
{title: "Instructor", first: 'Tim', last:"Garcia", isCatOwner: true},
{title: "Instructor", first: 'Matt', last:"Lane"},
{title: "Instructor", first: 'Colt', last:"Steele", isCatOwner: true}
]
hasCertainKey(arr,'first') // true
hasCertainKey(arr,'isCatOwner') // false
*/
function hasCertainKey(arr, key){
return arr.every(function(value){
return key in value;
});
}
/*
Write a function called hasCertainValue which accepts an array of objects and a key, and a value, and returns true if every single object in the array contains that value for the specific key. Otherwise it should return false.
Examples:
var arr = [
{title: "Instructor", first: 'Elie', last:"Schoppik"},
{title: "Instructor", first: 'Tim', last:"Garcia", isCatOwner: true},
{title: "Instructor", first: 'Matt', last:"Lane"},
{title: "Instructor", first: 'Colt', last:"Steele", isCatOwner: true}
]
hasCertainValue(arr,'title','Instructor') // true
hasCertainValue(arr,'first','Elie') // false
*/
function hasCertainValue(arr, key, searchValue){
return arr.every(function(value){
return key in value && value[key]===searchValue;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment