Skip to content

Instantly share code, notes, and snippets.

@tbonemalone
Created September 19, 2014 13:24
Show Gist options
  • Save tbonemalone/572d8039515b15c758cd to your computer and use it in GitHub Desktop.
Save tbonemalone/572d8039515b15c758cd to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
(function(){
var arr = [];
// generate random number to test
var genNumber = function(){
return Math.ceil(Math.random() * 100);
}
for(var i=0; i<10; i++) {
number = genNumber();
arr.push(genNumber());
}
// sort the array in ascending order
sortedArr = arr.sort(function(a, b) {
return a - b;
});
// take the first two elements and remove them from sortedArr
triangleSides = sortedArr.splice(0,2);
// determine ceiling for third element
thirdSideCeiling = triangleSides[1] + triangleSides[0] - 1;
// find first element that matches criteria for third element
thirdSide = sortedArr.every(function(el) {
if(el < thirdSideCeiling) {
triangleSides.push(el);
return false;
}
});
// simple check that criteria is met
// a + b > c
// b + c > a
// a + c > b
checkForTriangle = function(arr){
if(arr.length < 3) {
console.log("No solution available from provided numbers");
} else {
console.log("triangle sides are " + arr);
}
}
checkForTriangle(triangleSides);
})();
</script>
<script id="jsbin-source-javascript" type="text/javascript">(function(){
var arr = [];
// generate random number to test
var genNumber = function(){
return Math.ceil(Math.random() * 100);
}
for(var i=0; i<10; i++) {
number = genNumber();
arr.push(genNumber());
}
// sort the array in ascending order
sortedArr = arr.sort(function(a, b) {
return a - b;
});
// take the first two elements and remove them from sortedArr
triangleSides = sortedArr.splice(0,2);
// determine ceiling for third element
thirdSideCeiling = triangleSides[1] + triangleSides[0] - 1;
// find first element that matches criteria for third element
thirdSide = sortedArr.every(function(el) {
if(el < thirdSideCeiling) {
triangleSides.push(el);
return false;
}
});
// simple check that criteria is met
// a + b > c
// b + c > a
// a + c > b
checkForTriangle = function(arr){
if(arr.length < 3) {
console.log("No solution available from provided numbers");
} else {
console.log("triangle sides are " + arr);
}
}
checkForTriangle(triangleSides);
})();</script></body>
</html>
(function(){
var arr = [];
// generate random number to test
var genNumber = function(){
return Math.ceil(Math.random() * 100);
}
for(var i=0; i<10; i++) {
number = genNumber();
arr.push(genNumber());
}
// sort the array in ascending order
sortedArr = arr.sort(function(a, b) {
return a - b;
});
// take the first two elements and remove them from sortedArr
triangleSides = sortedArr.splice(0,2);
// determine ceiling for third element
thirdSideCeiling = triangleSides[1] + triangleSides[0] - 1;
// find first element that matches criteria for third element
thirdSide = sortedArr.every(function(el) {
if(el < thirdSideCeiling) {
triangleSides.push(el);
return false;
}
});
// simple check that criteria is met
// a + b > c
// b + c > a
// a + c > b
checkForTriangle = function(arr){
if(arr.length < 3) {
console.log("No solution available from provided numbers");
} else {
console.log("triangle sides are " + arr);
}
}
checkForTriangle(triangleSides);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment