const data = "abcdff"
const list = data.split('').reduce((x, y) =>
Array.isArray(x) ? x.concat(x[x.length - 1][1] + y): [x + y]
)
console.log('list', list); // ["ab", "bc", "cd", "df", "ff"]
function handleClickFor(index, text) {
a[index].addEventListener('click', function() {
dec2(text, text);
summCounter1();
callsPerDay();
callsOverall();
});
}
Array(3).fill().forEach(function(_, i) {
handleClickFor(i, 'count' + (i + 1) )
})
this.props.data
определён как default props(не будет меняться), но в большинстве случаев будет(идёт из родительского state либо из глобального хранилища, то есть store). this.props.data
обновится, локальный state останется прежним. Придётся дергать componentWillRecieveProps
. handle = {this.test}
. Пробелов быть не должно.export default class Board extends React.Component {
test = () => {
console.log(this)
}
render(){
const temp = this.props.data.map((item, index) =>
<Task data={item} key={index} handle={this.test} />
)
return <div className="board">{temp}</div>
}
}
const items = [
{name:'Отвертка',price:'150', quantity: 100},
{name:'Ключ',price:'100', quantity: 200},
{name:'Зубило',price:'180', quantity: 500},
{name:'Напильник',price:'200', quantity: 1000},
{name:'Молоток',price:'500', quantity: 250}
];
function getDiscount(x) {
if(x < 10) return 5;
if(x > 10 && x < 50) return 7;
if(x > 50 && x < 100) return 10;
if(x > 100) return 15;
}
function getCustomerBenefit(item) {
const benefit = item.quantity * 0.01 * item.price * getDiscount(item.quantity);
console.log('money you can save due to discount', benefit);
return benefit;
}
function calculateTotalPayment(item) {
const beforeDiscount = item.quantity * item.price;
const totalPayment = beforeDiscount - getCustomerBenefit(item);
console.log('beforeDiscount', beforeDiscount);
console.log('total payment(discount included)', totalPayment);
return totalPayment;
}
calculateTotalPayment(items[4]);
loadingScreen()
неявно возвращает промис, надо дождаться его выполненияasync function game() {
await loadingScreen()
console.log('screen is loaded!')
}
const list1 = [{id:1}, {id:2}];
const list2 = [{id: 2}];
R.difference(list1, list2); // [{"id": 1}]
<body>
<form role="form">
<div class="form-group " id="box">
<label class="control-label" id="label" for="input"></label>
<input type="text" class="form-control" id="input">
</div>
</form>
</body>
const input = document.getElementById('input');
const box = document.getElementById('box');
const label = document.getElementById('label');
const re = /^[0-9]{0,4}$/;
input.addEventListener('input', validate);
function validate(e) {
const value = e.target.value.trim();
console.log(value);
if(!value) {
label.textContent = '';
box.className = "form-group";
return;
}
if(re.test(value)) {
box.className = "form-group has-success";
label.textContent = "Well, it's ok for now!";
} else {
box.className = "form-group has-warning";
label.textContent = "Oh dude, totally weak!";
}
}
function connect(...mappers){
// не знаю как connect получает доступ к store, поэтому тупо захардкодил, но это не важно
// чтобы ответить на вопрос
const store = {
getState() {
return {
players: ['Rooney', 'Mata', 'Messi', 'Ronaldo', 'Zlatan']
}
},
dispatch(actionCreator) {
return function (...args) {
return Promise
.resolve( actionCreator(...args) )
.then(() => console.log(`I have just called ${actionCreator.name} actionCreator`));
};
}
};
let fromStore = {};
if(typeof mappers[0] === "function") {
const state = store.getState();
const stateToProps = mappers[0](state);
fromStore = {...stateToProps};
}
if(typeof mappers[1] === "function") {
fromStore = { ...fromStore, ...mappers[1](store.dispatch) };
}
return function(MyComponent) {
const HOC = Component => props =>
<Component dispatch={store.dispatch} {...fromStore} {...props} />
return HOC(MyComponent);
};
}
const App = (props = { a: 1, b: 2 }) => (
console.log('App props', props)
);
const fetchData = (api) => console.log(`I am gonna fetch some data from this api ${api}`)
const mapStateToProps = state => ({ players: state.players });
const mapDispatchToProps = dispatch => ({ fetchData: dispatch(fetchData) });
const ConnectedApp = connect(mapStateToProps, mapDispatchToProps)(App);
console.log('connectedApp props', ConnectedApp({ hello: 'hello kitty' }).props);
console.log('connectedApp props', <ConnectedApp hello={'hello kitty'} />);
var $activeElements = $('.element.active');
$activeElements.removeClass('active');