function binary_Search(items, value){ var firstIndex = 0, lastIndex = items.length - 1, middleIndex = Math.floor((lastIndex + firstIndex)/2); while(items[middleIndex] != value && firstIndex < lastIndex) { if (value < items[middleIndex]) { lastIndex = middleIndex - 1; } else if (value > items[middleIndex]) { firstIndex = middleIndex + 1; } middleIndex = Math.floor((lastIndex + firstIndex)/2); } return (items[middleIndex] != value) ? -1 : middleIndex; } var items = [1, 2, 3, 4, 5, 7, 8, 9]; console.log(binary_Search(items, 1)); console.log(binary_Search(items, 5));
Find the leap years from a given range of years
function leap_year_range(st_year, end_year) {
var year_range = [];
for (var i = st_year; i <= end_year; i++)
{
year_range.push(i);
}
var new_array = [];
year_range.forEach(
function(year)
{
if (test_LeapYear(year))
new_array.push(year);
});
return new_array;
}
function test_LeapYear(year) {
if ((year % 4 === 0 && year % 100 !== 0) || (year % 100 === 0 && year % 400 === 0)) {
return year;
} else {
return false;
}
}
console.log(leap_year_range(2000,2012));
var year_range = [];
for (var i = st_year; i <= end_year; i++)
{
year_range.push(i);
}
var new_array = [];
year_range.forEach(
function(year)
{
if (test_LeapYear(year))
new_array.push(year);
});
return new_array;
}
function test_LeapYear(year) {
if ((year % 4 === 0 && year % 100 !== 0) || (year % 100 === 0 && year % 400 === 0)) {
return year;
} else {
return false;
}
}
console.log(leap_year_range(2000,2012));
JavaScript Higher Order Functions & Arrays
const companies= [
{name: "Company One", category: "Finance", start: 1981, end: 2004},
{name: "Company Two", category: "Retail", start: 1992, end: 2008},
{name: "Company Three", category: "Auto", start: 1999, end: 2007},
{name: "Company Four", category: "Retail", start: 1989, end: 2010},
{name: "Company Five", category: "Technology", start: 2009, end: 2014},
{name: "Company Six", category: "Finance", start: 1987, end: 2010},
{name: "Company Seven", category: "Auto", start: 1986, end: 1996},
{name: "Company Eight", category: "Technology", start: 2011, end: 2016},
{name: "Company Nine", category: "Retail", start: 1981, end: 1989}
];
const ages = [33, 12, 20, 16, 5, 54, 21, 44, 61, 13, 15, 45, 25, 64, 32];
// for(let i = 0; i < companies.length; i++) {
// console.log(companies[i]);
// }
// forEach
// companies.forEach(function(company) {
// console.log(company.name);
// });
// filter
// Get 21 and older
// let canDrink = [];
// for(let i = 0; i < ages.length; i++) {
// if(ages[i] >= 21) {
// canDrink.push(ages[i]);
// }
// }
// const canDrink = ages.filter(function(age) {
// if(age >= 21) {
// return true;
// }
// });
const canDrink = ages.filter(age => age >= 21);
// Filter retail companies
// const retailCompanies = companies.filter(function(company) {
// if(company.category === 'Retail') {
// return true;
// }
// });
const retailCompanies = companies.filter(company => company.category === 'Retail');
// Get 80s companies
const eightiesCompanies = companies.filter(company => (company.start >= 1980 && company.start < 1990));
// Get companies that lasted 10 years or more
const lastedTenYears = companies.filter(company => (company.end - company.start >= 10));
// map
// Create array of company names
// const companyNames = companies.map(function(company) {
// return company.name;
// });
// const testMap = companies.map(function(company) {
// return `${company.name} [${company.start} - ${company.end}]`;
// });
// const testMap = companies.map(company => `${company.name} [${company.start} - ${company.end}]`);
// const ageMap = ages
// .map(age => Math.sqrt(age))
// .map(age => age * 2);
// sort
// Sort companies by start year
// const sortedCompanies = companies.sort(function(c1, c2) {
// if(c1.start > c2.start) {
// return 1;
// } else {
// return -1;
// }
// });
// const sortedCompanies = companies.sort((a, b) => (a.start > b.start ? 1 : -1));
// Sort ages
// const sortAges = ages.sort((a, b) => a - b);
// console.log(sortAges);
// reduce
// let ageSum = 0;
// for(let i = 0; i < ages.length; i++) {
// ageSum += ages[i];
// }
// const ageSum = ages.reduce(function(total, age) {
// return total + age;
// }, 0);
// const ageSum = ages.reduce((total, age) => total + age, 0);
// Get total years for all companies
// const totalYears = companies.reduce(function(total, company) {
// return total + (company.end - company.start);
// }, 0);
const totalYears = companies.reduce((total, company) => total + (company.end - company.start), 0);
// Combine Methods
const combined = ages
.map(age => age * 2)
.filter(age => age >= 40)
.sort((a, b) => a - b)
.reduce((a, b) => a + b, 0);
console.log(combined);
{name: "Company One", category: "Finance", start: 1981, end: 2004},
{name: "Company Two", category: "Retail", start: 1992, end: 2008},
{name: "Company Three", category: "Auto", start: 1999, end: 2007},
{name: "Company Four", category: "Retail", start: 1989, end: 2010},
{name: "Company Five", category: "Technology", start: 2009, end: 2014},
{name: "Company Six", category: "Finance", start: 1987, end: 2010},
{name: "Company Seven", category: "Auto", start: 1986, end: 1996},
{name: "Company Eight", category: "Technology", start: 2011, end: 2016},
{name: "Company Nine", category: "Retail", start: 1981, end: 1989}
];
const ages = [33, 12, 20, 16, 5, 54, 21, 44, 61, 13, 15, 45, 25, 64, 32];
// for(let i = 0; i < companies.length; i++) {
// console.log(companies[i]);
// }
// forEach
// companies.forEach(function(company) {
// console.log(company.name);
// });
// filter
// Get 21 and older
// let canDrink = [];
// for(let i = 0; i < ages.length; i++) {
// if(ages[i] >= 21) {
// canDrink.push(ages[i]);
// }
// }
// const canDrink = ages.filter(function(age) {
// if(age >= 21) {
// return true;
// }
// });
const canDrink = ages.filter(age => age >= 21);
// Filter retail companies
// const retailCompanies = companies.filter(function(company) {
// if(company.category === 'Retail') {
// return true;
// }
// });
const retailCompanies = companies.filter(company => company.category === 'Retail');
// Get 80s companies
const eightiesCompanies = companies.filter(company => (company.start >= 1980 && company.start < 1990));
// Get companies that lasted 10 years or more
const lastedTenYears = companies.filter(company => (company.end - company.start >= 10));
// map
// Create array of company names
// const companyNames = companies.map(function(company) {
// return company.name;
// });
// const testMap = companies.map(function(company) {
// return `${company.name} [${company.start} - ${company.end}]`;
// });
// const testMap = companies.map(company => `${company.name} [${company.start} - ${company.end}]`);
// const ageMap = ages
// .map(age => Math.sqrt(age))
// .map(age => age * 2);
// sort
// Sort companies by start year
// const sortedCompanies = companies.sort(function(c1, c2) {
// if(c1.start > c2.start) {
// return 1;
// } else {
// return -1;
// }
// });
// const sortedCompanies = companies.sort((a, b) => (a.start > b.start ? 1 : -1));
// Sort ages
// const sortAges = ages.sort((a, b) => a - b);
// console.log(sortAges);
// reduce
// let ageSum = 0;
// for(let i = 0; i < ages.length; i++) {
// ageSum += ages[i];
// }
// const ageSum = ages.reduce(function(total, age) {
// return total + age;
// }, 0);
// const ageSum = ages.reduce((total, age) => total + age, 0);
// Get total years for all companies
// const totalYears = companies.reduce(function(total, company) {
// return total + (company.end - company.start);
// }, 0);
const totalYears = companies.reduce((total, company) => total + (company.end - company.start), 0);
// Combine Methods
const combined = ages
.map(age => age * 2)
.filter(age => age >= 40)
.sort((a, b) => a - b)
.reduce((a, b) => a + b, 0);
console.log(combined);
Subscribe to:
Posts (Atom)