Skip to content

Instantly share code, notes, and snippets.

@SergProduction
Created January 12, 2017 00:03
Show Gist options
  • Save SergProduction/2651daa49ca6a238c9618e28d6bb660d to your computer and use it in GitHub Desktop.
Save SergProduction/2651daa49ca6a238c9618e28d6bb660d to your computer and use it in GitHub Desktop.
<div id="quest">
<input type="text"/>
<button>Спросить</button>
</div>
const tmpl = [
{
parent:0,
id:0,
desc: "У меня нет ответа на данный вопрос",
key: "",
quest: false
},
{
parent:0,
id:1,
desc: false,
key: "тарифы",
quest: 'У нас есть тариф мини и макс, о каком тарифе вам рассказать подробней?'
},
{
parent:1,
id:2,
desc: "тариф мини стоит 500 рублей",
key: "мини",
quest: false
},
{
parent:1,
id:3,
desc: "тариф макс стоит 1000 рублей",
key: "макс",
quest: false
}
];
$('#quest button').on('click', (e) => {
let quest = $('#quest input').val()
$('#quest input').val('')
let branch = tmpl.filter( el => quest.match(/[а-я\w]+/ig).filter( world => new RegExp(world,'i').test(el.key) ).length > 0 );
console.log(branch)
view( quest )
cnotroller( branch )
})
function cnotroller( branch ){
if( branch.length > 1 ){
let parent = 0;
branch.forEach( el => {
if( el.parent > parent ){
parent = el.parent
}
})
let branch_up = branch.filter( el => el.parent == parent)[0]
view( branch_up, '#008b1f' )
}
else{
view( branch[0], '#008b1f' )
}
}
function view( worlds, color ){
if( typeof worlds == 'object')
worlds = worlds.desc ? worlds.desc : worlds.quest
let p = document.createElement('p');
p.textContent = worlds;
p.style.color = color ? color : ''
document.body.appendChild(p)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment