Skip to content

Instantly share code, notes, and snippets.

@pawarvijay
Created April 7, 2017 13:35
Show Gist options
  • Save pawarvijay/9755d37669c711c614c1568d5ebae475 to your computer and use it in GitHub Desktop.
Save pawarvijay/9755d37669c711c614c1568d5ebae475 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/wezeri
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
<!DOCTYPE html>
<\!--
Created using JS Bin
http://jsbin.com
Copyright (c) 2017 by pawarvijay (http://jsbin.com/wezeri/3/edit)
Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var data = {
name : 'vijay',
friends : [
{
name:'manish',
friends : [
{name :"ajit", friends : []}
]
},
{
name : 'pranit',
friends : [
{name : 'prajakta', friends : []},
{name : 'pavan', friends : []}
]
},
{
name : 'rahul',
friends : [
{
name : 'parul',
friends : [
{
name : 'tom' ,
friends : []
}
]
}
]
}
]
}
var currentNode = data;
var visitedList = []
var q = []
function exploreCurrentNode(){
currentNode.friends.forEach(function(friend){
var object = Object.assign({}, friend);
object.parent = currentNode.name;
visitedList.push(object)
q.push(object)
})
}
exploreCurrentNode()
while (q.length > 0) {
emptyQueue()
}
function emptyQueue(){
q.forEach(function(itemq){
currentNode = q.shift();
exploreCurrentNode()
})
}
visitedList.forEach(function(item){
console.log('parent : ' + item.parent + ' -----> child : ' + item.name)
})
console.log(q)
function findParent(name){
var asdf
visitedList.forEach(function(item1){
if(item1.name == name){
asdf = item1
}
})
return asdf
}
var count = 0
function findConnectionCount(name){
var hmm = findParent(name)
if(hmm){
count = count + 1
findConnectionCount(hmm.parent)
}else{
alert(count)
console.log('nothing left')
}
}
findConnectionCount('parul')
<\/script>
</body>
</html>
</script>
<script id="jsbin-source-javascript" type="text/javascript"><!DOCTYPE html>
<\!--
Created using JS Bin
http://jsbin.com
Copyright (c) 2017 by pawarvijay (http://jsbin.com/wezeri/3/edit)
Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var data = {
name : 'vijay',
friends : [
{
name:'manish',
friends : [
{name :"ajit", friends : []}
]
},
{
name : 'pranit',
friends : [
{name : 'prajakta', friends : []},
{name : 'pavan', friends : []}
]
},
{
name : 'rahul',
friends : [
{
name : 'parul',
friends : [
{
name : 'tom' ,
friends : []
}
]
}
]
}
]
}
var currentNode = data;
var visitedList = []
var q = []
function exploreCurrentNode(){
currentNode.friends.forEach(function(friend){
var object = Object.assign({}, friend);
object.parent = currentNode.name;
visitedList.push(object)
q.push(object)
})
}
exploreCurrentNode()
while (q.length > 0) {
emptyQueue()
}
function emptyQueue(){
q.forEach(function(itemq){
currentNode = q.shift();
exploreCurrentNode()
})
}
visitedList.forEach(function(item){
console.log('parent : ' + item.parent + ' -----> child : ' + item.name)
})
console.log(q)
function findParent(name){
var asdf
visitedList.forEach(function(item1){
if(item1.name == name){
asdf = item1
}
})
return asdf
}
var count = 0
function findConnectionCount(name){
var hmm = findParent(name)
if(hmm){
count = count + 1
findConnectionCount(hmm.parent)
}else{
alert(count)
console.log('nothing left')
}
}
findConnectionCount('parul')
<\/script>
</body>
</html>
</script></body>
</html>
<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com
Copyright (c) 2017 by pawarvijay (http://jsbin.com/wezeri/3/edit)
Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var data = {
name : 'vijay',
friends : [
{
name:'manish',
friends : [
{name :"ajit", friends : []}
]
},
{
name : 'pranit',
friends : [
{name : 'prajakta', friends : []},
{name : 'pavan', friends : []}
]
},
{
name : 'rahul',
friends : [
{
name : 'parul',
friends : [
{
name : 'tom' ,
friends : []
}
]
}
]
}
]
}
var currentNode = data;
var visitedList = []
var q = []
function exploreCurrentNode(){
currentNode.friends.forEach(function(friend){
var object = Object.assign({}, friend);
object.parent = currentNode.name;
visitedList.push(object)
q.push(object)
})
}
exploreCurrentNode()
while (q.length > 0) {
emptyQueue()
}
function emptyQueue(){
q.forEach(function(itemq){
currentNode = q.shift();
exploreCurrentNode()
})
}
visitedList.forEach(function(item){
console.log('parent : ' + item.parent + ' -----> child : ' + item.name)
})
console.log(q)
function findParent(name){
var asdf
visitedList.forEach(function(item1){
if(item1.name == name){
asdf = item1
}
})
return asdf
}
var count = 0
function findConnectionCount(name){
var hmm = findParent(name)
if(hmm){
count = count + 1
findConnectionCount(hmm.parent)
}else{
alert(count)
console.log('nothing left')
}
}
findConnectionCount('parul')
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment