var arr = ["Apple", "Coffee", "Avocado", "Pear", "Date", "Cherry", "Peach", "Apricot", "Blueberry", "Olive", "Papaya", "Orange", "Coconut", "Peanut", "Blackberry"];
/*
// 1. Sort Alphabetically
var arr2 = [];
arr2 = arr.sort()
console.log(arr2);
// 2. Sort Alphabetically descending
var arr3 = [];
arr3 = arr.sort().reverse();
console.log(arr3);
// 3. Create a function to return fruits that starts with "A"
function getFruit(arr, x) {
var sortFruit = [],
prop;
x = x.toUpperCase();
for (prop in arr) {
(arr[prop].charAt(0) == x) ? sortFruit.push(arr[prop]) : null;
}
return sortFruit;
}
var arr4 = getFruit(arr, "p");
console.log(arr4);
// 4. Create a function to Reverse all values of Array
function revArray(revArr) {
var revOutput = [];
for (prop in revArr) {
revOutput.push(revArr[prop].split('').reverse().join(""));
}
return revOutput;
}
var arr5 = revArray(arr);
console.log(arr5);
*/
// 5. Create a function to pass array and check passed character is present in array value. If present return those values;
function matchAlpha(myArr, z){
var matchedVal = [];
for(var i in myArr) {
if(myArr[i].indexOf(z) !== -1){
matchedVal.push(myArr[i]);
}
}
return matchedVal;
}
var arr6 = matchAlpha(arr, "o");
console.log(arr6);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.