Skip to content

Instantly share code, notes, and snippets.

@oddlyfunctional
Created August 14, 2015 04:33
Show Gist options
  • Save oddlyfunctional/561b1015b25e66f625f3 to your computer and use it in GitHub Desktop.
Save oddlyfunctional/561b1015b25e66f625f3 to your computer and use it in GitHub Desktop.
// Write a function named allEven that accepts an array of
// numbers and returns true if every number is even, false
// otherwise. Use the forEach method to solve
// this problem
function allEven(list) {
var result = true;
list.forEach(function(element) {
if (element % 2 != 0) {
result = false;
}
});
return result;
}
// Write a function named isMember that accepts two
// arguments—an array of strings and a string—and returns true if
// the string is contained in the array, false otherwise. Use the forEach method to solve
// this problem.
function isMember(list, element) {
var result = false;
list.forEach(function(el) {
if (element == el) {
result = true;
}
});
return result;
}
// Write a function named hasOneEven that accepts an array of
// numbers and returns true if it contains at least one even number.
// Use the forEach method to solve this problem.
function hasOneEven(list) {
var result = false;
list.forEach(function(element) {
if (element % 2 == 0) {
result = true;
}
});
return result;
}
// Write a function named countVowels that accepts a string
// representing a sentence and returns the number of vowels
// contained in the sentence. Use the forEach method to solve
// this problem
function countVowels(string) {
var count = 0;
string.split("").forEach(function(letter) {
if (isMember(["a", "e", "i", "o", "u"], letter.toLowerCase())) {
count++;
}
});
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment