Skip to content

Instantly share code, notes, and snippets.

View Chuloo's full-sized avatar

William Chuloo

View GitHub Profile
@Chuloo
Chuloo / array-every-method.js
Last active February 6, 2019 17:10
Test array of objects
// Array of students
const students = [{name: 'John', score: 50} , {name: 'Peter', score: 60}, {name: 'James', score: 55}];
// Threshold Score
const passScore = 50;
// Test students
const studentsPassed = students.every(student => student.score >= passScore);
// Display status
@Chuloo
Chuloo / array-some-method.js
Created February 6, 2019 17:09
Test for number divisible by 5
// Create new array
const array = [2, 5, 10, 8, 20]
// Create test function
const testFunction = number => number % 5 === 0
// Apply function
const result = array.some(testFunction)
// Display result
@Chuloo
Chuloo / array-some-method.js
Created February 8, 2019 10:04
Presence of Key:Value in Objects
// Array of objects
const users = [{firstName: 'Peter', lastName: 'Parker'}, {firstName: 'Harry'}];
// Verify properties of an object
let hasLastName = users.some(function(user){
return !!user.lastName;
})
// Output result
console.log(hasLastName);
@Chuloo
Chuloo / array-tostring-method.js
Created February 8, 2019 11:05
Convert names array to string
// Array of names
const names = ['john', 'chris', 'mike', 'holly', 'will']
// Convert to string
const stringNames = names.toString()
// Output string
console.log(stringNames)
@Chuloo
Chuloo / array-tostring-method.js
Created February 8, 2019 11:14
Convert array to string
// Create array with strings
const values = ['1', '2', '3', '4'];
// Convert array to string separated by a comma
const string = values.toString();
// Output string
console.log(string);
@Chuloo
Chuloo / array-foreach-method.js
Created February 8, 2019 11:26
Double numbers
// Create array of numbers
let array = [1, 2, 3, 4, 5]
// Double each number and display
array.forEach(function(number){
console.log(number*2)
})
@Chuloo
Chuloo / array-foreach-method.js
Created February 8, 2019 11:34
Copy array content
// Create new array
const students = ['Jerry', 'Mary', 'Brenda', 'John'];
// Create empty array to be copied into
const classA = [];
// Copy names into new array
students.forEach(student => {
classA.push(student)
});
@Chuloo
Chuloo / array-foreach-method.js
Created February 8, 2019 11:37
Better days ahead
// Array of dates
const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
// with for loop
for(i = 0; i < days.length; i++){
console.log(days[i]);
}
console.log('----------------------')
@Chuloo
Chuloo / array-inlcudes-method.js
Created February 8, 2019 11:49
Part of Class
// Create an array of names
const classNames = ['amazon', 'facebook', 'google', 'scotch']
// Verify inclusion in classNames
let partOfClass = classNames.includes('microsoft')
// Output result
console.log(partOfClass)
@Chuloo
Chuloo / array-inlcudes-method.js
Created February 8, 2019 11:58
Search days of the week
// Weekdays
const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
// Random month
const month = 'May';
// Verify inclusion in array
const isDay = days.includes(month);
// Output result