Created
November 8, 2017 06:49
-
-
Save jigewxy/343becbfcf7a004ec66f1e253832cba0 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/bucefofepe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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"> | |
function Queue (arr, length, allowRepeat){ | |
if(length===undefined) | |
throw new UserException('Queue Length must be specified'); | |
if(arr.length>length) | |
arr = arr.slice(arr.length-length); //slice the source if length exceeds | |
this.data = arr; | |
this.maxLength = length; | |
this.allowRepeat = allowRepeat || false; | |
} | |
Queue.prototype.addItem = function(item){ | |
if(item!==0 && Boolean(item) === false) | |
{ | |
throw new UserException('unable to add empty or null value'); | |
} | |
else if(this.data.indexOf(item)!==-1 && this.allowRepeat !== true) | |
{ | |
//repeat value has been detected, do nothing; | |
return; | |
} | |
else { | |
if(this.data.length <this.maxLength) | |
{this.data.push(item); } | |
else | |
{ | |
this.data.shift(); | |
this.data.push(item); | |
} | |
} | |
} | |
Queue.prototype.removeItem = function(item){ | |
if(item===undefined) | |
{ | |
this.data.shift(); | |
} | |
else if(Boolean(item)===false) | |
{ | |
throw new UserException('unable to remove invalid value'); | |
} | |
else | |
{ | |
var idx = this.data.indexOf(item); | |
if(idx!==-1) | |
this.data.splice(idx, 1); | |
else | |
console.log('value does not exist'); | |
} | |
} | |
function UserException(message){ | |
this.message = message; | |
this.name = "User Exception: "; | |
this.toString = function(){ | |
return this.name+ this.message; | |
} | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function Queue (arr, length, allowRepeat){ | |
if(length===undefined) | |
throw new UserException('Queue Length must be specified'); | |
if(arr.length>length) | |
arr = arr.slice(arr.length-length); //slice the source if length exceeds | |
this.data = arr; | |
this.maxLength = length; | |
this.allowRepeat = allowRepeat || false; | |
} | |
Queue.prototype.addItem = function(item){ | |
if(item!==0 && Boolean(item) === false) | |
{ | |
throw new UserException('unable to add empty or null value'); | |
} | |
else if(this.data.indexOf(item)!==-1 && this.allowRepeat !== true) | |
{ | |
//repeat value has been detected, do nothing; | |
return; | |
} | |
else { | |
if(this.data.length <this.maxLength) | |
{this.data.push(item); } | |
else | |
{ | |
this.data.shift(); | |
this.data.push(item); | |
} | |
} | |
} | |
Queue.prototype.removeItem = function(item){ | |
if(item===undefined) | |
{ | |
this.data.shift(); | |
} | |
else if(Boolean(item)===false) | |
{ | |
throw new UserException('unable to remove invalid value'); | |
} | |
else | |
{ | |
var idx = this.data.indexOf(item); | |
if(idx!==-1) | |
this.data.splice(idx, 1); | |
else | |
console.log('value does not exist'); | |
} | |
} | |
function UserException(message){ | |
this.message = message; | |
this.name = "User Exception: "; | |
this.toString = function(){ | |
return this.name+ this.message; | |
} | |
}</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Queue (arr, length, allowRepeat){ | |
if(length===undefined) | |
throw new UserException('Queue Length must be specified'); | |
if(arr.length>length) | |
arr = arr.slice(arr.length-length); //slice the source if length exceeds | |
this.data = arr; | |
this.maxLength = length; | |
this.allowRepeat = allowRepeat || false; | |
} | |
Queue.prototype.addItem = function(item){ | |
if(item!==0 && Boolean(item) === false) | |
{ | |
throw new UserException('unable to add empty or null value'); | |
} | |
else if(this.data.indexOf(item)!==-1 && this.allowRepeat !== true) | |
{ | |
//repeat value has been detected, do nothing; | |
return; | |
} | |
else { | |
if(this.data.length <this.maxLength) | |
{this.data.push(item); } | |
else | |
{ | |
this.data.shift(); | |
this.data.push(item); | |
} | |
} | |
} | |
Queue.prototype.removeItem = function(item){ | |
if(item===undefined) | |
{ | |
this.data.shift(); | |
} | |
else if(Boolean(item)===false) | |
{ | |
throw new UserException('unable to remove invalid value'); | |
} | |
else | |
{ | |
var idx = this.data.indexOf(item); | |
if(idx!==-1) | |
this.data.splice(idx, 1); | |
else | |
console.log('value does not exist'); | |
} | |
} | |
function UserException(message){ | |
this.message = message; | |
this.name = "User Exception: "; | |
this.toString = function(){ | |
return this.name+ this.message; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment