Skip to content

Instantly share code, notes, and snippets.

View Yigaue's full-sized avatar
🦍
building

Gospel Lekia Yigaue

🦍
building
View GitHub Profile
@Yigaue
Yigaue / ternary.php
Last active January 27, 2020 18:51
Ternary operators collection
<?php
/**
| The commented code shows the full code of the ternary equiv.
*/
// if ($age = 20 < 22)
// {
let data = [
{id: 1, title: "Don't Waste your life ", price: 33, Author: "John Piper"},
{id: 2, title: "Living for a greater purpose", price: 12, Author: "JH Baritam"},
{id: 3, title: "The call for Mercy", price: 405, Author: "Lekia Yiga"},
{id: 4, title: "Heart Blindness", price: 550, Author: "Unknown author"}
];
let newPrice = data.map((book) => book.price + 200);
console.log(newPrice);
// result:
// (4) [233, 212, 605, 750]
data.map(item => {
if (item.price > 100) {
return {
id: item.id,
title: item.title,
price: item.price * (1 - 0.2)
};
}
return item;
});
let data = [
{id: 1, title: "Don't Waste your life ", price: 33, Author: "John Piper"},
{id: 2, title: "Living for a greater purpose", price: 12, Author: "JH Baritam"},
{id: 3, title: "The call for Mercy", price: 405, Author: "Lekia Yiga"},
{id: 4, title: "Heart Blindness", price: 550, Author: "Unknown author"}
];
let discountedBooks = data.filter(item => item.price > 100);
console.log(discountedBooks);
//result
/*
let data = [
{id: 1, title: "Don't Waste your life ", price: 33, Author: "John Piper"},
{id: 2, title: "Living for a greater purpose", price: 12, Author: "JH Baritam"},
{id: 3, title: "The call for Mercy", price: 405, Author: "Lekia Yiga"},
{id: 4, title: "Heart Blindness", price: 550, Author: "Unknown author"}
];
let discountedBooks = data.find(item => item.price > 100);
console.log(discountedBooks);
// result
let data = [
{id: 1, title: 'Nite watch', price: 33, discount:0},
{id: 2, title: 'what I want', price: 12, discount:0.4},
{id: 3, title: 'Mercy at last', price: 405, discount:0.25},
{id: 4, title: 'Nothting between', price: 550, discount:0}
];
let discountedBooks = data.some(item => item.discount > 0 );
console.log(discountedBooks);
// result
let data = [
{id: 1, title: 'Nite watch', price: 33, discount:0},
{id: 2, title: 'what I want', price: 12, discount:0.4},
{id: 3, title: 'Mercy at last', price: 405, discount:0.25},
{id: 4, title: 'Nothting between', price: 550, discount:0}
];
let discountedBooks = data.every(item => item.discount > 0 );
console.log(discountedBooks);
// result
let data = [
{id: 1, title: 'Nite watch', price: 33, discount:0},
{id: 2, title: 'what I want', price: 12, discount:0.4},
{id: 3, title: 'Mercy at last', price: 405, discount:0.25},
{id: 4, title: 'Nothting between', price: 550, discount:0}
];
priceSum = data.reduce((acc, currentValue) => acc + currentValue.price, 0);
console.log(priceSum);
// console result
1000
@Yigaue
Yigaue / Permeability.php
Created October 30, 2020 11:52
Prosper Permeability algorithm
$permeabilityArray = [9230, 5940, 3080, 2860, 2790, 2370, 1464, 594, 593, 526, 500, 366];
/**
* Each number in the array should take turns to divided it self and every other number in the array,
* and a number can only divide itself or a number that has index greater than it.
* E.g 9230 can divide itself and every other number. 3080 can not divide 5940, and 9230 but can divide
* 2860, 2790, 2370, 1464, 594, 593, 526, 500, and 366.
* Print result different set of arrays.
*/