Secret Hidden message on keyboard enter [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Just had a thought and was wondering if it was possible in the spirit of april fools.
Is it possible to have a secret code that you randomly type in a website similar to that on a gaming console like (a, up, down, b, L1, R1), but on the website, you would type on your keyboard "9 2 k (uparrow) 3" and as long as you're on the website, it does something, let's say alert('hey')? Curious to see how you would do it.

// our combo
var combo = [57, 50, 75, 38, 51];
var keys = new Array(combo.length);
$(document).on('keydown', function(evt){
// remove the earliest of the stored keys
keys.shift();
// push in the latest key pressed
keys.push(evt.which);
// compare with combo array - you could use any comparison method (convert to string,...)
if (!keys.some(function(e, i) { return e !== combo[i] }))
alert('hey')
})
Fiddle - https://jsfiddle.net/kzj5e7Lk/
You'll need to click in the run area (bottom right pane) before you try the key combination.

Well, there's a few things you could do to capitalize on this, and make it more obfuscated so the user has to jump through some more hoops to get the secret key combination, but this should be good enough for a basic implementation.
var code = '.57.50.75.38.51';
var entered = '';
var clearCombo;
//keypress won't register up arrow
$('body').keydown(function (e) {
clearTimeout(clearCombo);
entered += '.' + e.which;
clearCombo = setTimeout(function () {
entered = '';
}, 1000);
if (entered === code) {
alert('i pity the foobar');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
That'll require them to enter the code with each character within one second of each other. (set/clearTimeout) It also means they'll have to wait for the timeout to clear out their entered keys if they mess up.
You might also want to check out Mousetrap if you don't mind including an additional library.

Related

I need to programatically create several variables in javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I asked this question but it was marked as duplicate - it's not.
I need to programmatically create X number of variables (the number will be the value of someArray.length - which is also something created programmatically and thus unknown to me).
Is there any way to say "there's 8 items in this set, so I need 8 variables, and I need to be able to call on them/manipulate them later?" Hopefully I'm making sense.
My core problem: I programmatically generate a list of links. Can be any number of links. I need to be able to validate that the user clicks each of these links before advancing.
I programmatically generate a list of links. Can be any number of links. I need to be able to validate that the user clicks each of these links before advancing.
You do not need to create an unknown number of variables to solve your problem (how would you name them?). As stated by other commenters, you have a list of links, and you need to keep track of which ones have been clicked.
There are numerous ways to solve this problem in JavaScript. Below is an example of a straightforward approach which should be easy to follow. The approach is simply to use another array, linksClicked, to keep track of which links have been clicked. To validate, count the number of links that have been clicked and compare to the total number of links.
var arrayOfLinks = [
'http://www.stackoverflow.com',
'http://www.arstechnica.com'
];
var linksClicked = [];
function clickLink(url){
//check if link is in arrayOfLinks
for(var i = 0; i < arrayOfLinks.length; i++){
//if link is in arrayOfLinks, mark it as clicked
if(arrayOfLinks[i] === url){
linksClicked[i] = true;
}
}
}
function checkLinksClicked(){
//count number of links that have been clicked
var linkSum = 0;
for(var i = 0; i < linksClicked.length; i++){
if(linksClicked[i]){
linkSum++;
}
}
return linkSum;
}
console.log(checkLinksClicked());
clickLink('http://www.stackoverflow.com');
console.log(checkLinksClicked());
clickLink('http://www.stackoverflow.com');
console.log(checkLinksClicked());
clickLink('http://www.arstechnica.com');
console.log(checkLinksClicked());

Javascript - Show all telephone numbers in a range [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
If I have phone number ranges such as 5555555555-5599 5555550000-0003 5555550005-0007, my mission is to have it return all results without using a server. Is it possible for it to return without a server a result that would look like:
5555555555
5555555556
5555555557
/* etc */
My previous post about javascript has helped me up to this point but I wanted to rehaul the whole site.
Javascript dashes in phone number
If you could point me in the right direction, I would really appreciate it. I'm just having a mind block right now if this is even possible.
Given a single phone range in the form of "xxxxxxyyyy-zzzz", split the whole string on the dash and the first part of the string at the 6th index. This yields three strings "xxxxxx", "yyyy", and "zzzz". Using a for loop, you can create an array of phone numbers by concatenating the prefix "xxxxxx" onto the range "yyyy"-"zzzz":
// Get an array from a given range "xxxxxxyyyy-zzzz"
function fromRange(range) {
var phoneNumArray = [];
var prefix = range.substring(0,5);
var suffixRange = range.split("-");
for (var suffix = suffixRange[0].substring(4, -1);suffix < suffixRange[1];suffix++) {
phoneNumArray.push(prefix + suffix);
}
return phoneNumArray;
}
Try it in JSFiddle.

How to make a javascript count up, such as world population? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I don't know how to make a Javascript count up that is related to the real time, which means when you reload the page, the counter won't start over again. Would anybody tell me how to make that happen :) Example like http://www.worldometers.info/ Thanks a lot.
The code they are using is likely pulling from a database with an actual value increasing live.
Check out this js fiddle I made for an example of how a simple timer can work. Notice that if you press "Run" multiple times, the time itself will stay constant.
Using a remote database will cause a lot more work, but for saving values across browser refreshes you should learn about localStorage I'd check out W3 School's article on the subject.
In my implementation I use
localStorage.setItem("time", currentTime); // Note that CurrentTime must be a string!
in each iteration of your code after setting the currentTime var.
When you start up your application, a simple if statement
if (localStorage.getItem("time") {
CurrentTime = localStorage.getItem("time");
} else {
// set default values
}
will work, as localStorage.getItem will return null if the value doesn't exist (or if you set the value to null manually).
(for localStorage, you can also use [bracket notation] and will probably see that in most common examples)
localStorage["foo"] = "value";
// is the same as:
localStorage.setItem("foo", "value")
// and
var bar = localStorage["foo"];
// is the same as:
var bar = localStorage.getItem("foo");

Highlighting matching nodes in Tree using JS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a left tree navigation with a search box on top.When the user enters some text in the Search box,the matching nodes of the tree navigation should get highlighted.
I want to do it using Java script,Can anyone point me to any such examples or documentation for this.
Thanks
Without any html etc can't really see what your setup is! But I assume you want something like the following:
http://jsfiddle.net/cwc66a3d/3/
Use indexOf to find any matching cases among the options, then using the position given, insert a span to contain the matching text with a yellow background to give the impression of highlighting.
The timeout is simply because of delays in event firing, sure to make sure it highlights in real time it is needed.
document.onkeydown = function () {
setTimeout(function () { //delay so can take the event fire into account
var list = document.getElementsByClassName('tree');
for (var i = 0; i < list.length; i++) {
list[i].innerHTML = list[i].innerText;
var pos = (list[i].innerHTML).indexOf(s.value);
if (s.value !== '' && pos != -1) {
var a = list[i].innerHTML.substring(0, pos) + '<span style="background: yellow;">' + list[i].innerHTML.substring(pos,1+ (list[i].innerHTML).indexOf(s.value.substr(s.value.length-1, 1))) + '</span>' + (list[i].innerHTML).substring(1+ (list[i].innerHTML).indexOf(s.value.substr(s.value.length-1, 1)), (list[i].innerHTML).length);
list[i].innerHTML = a;
}
}
}, 50);
};

Why is the num pad considered character? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is working fine, but if I use my num pad (right side), it will consider it a character and delete it but the numbers in the left side is accepted. I want my num pad (right side) to be accepted.
<script type="text/javascript">
$("input[name='inputDate']:first").keyup(function(e){
var key=String.fromCharCode(e.keyCode);
if(!( key >= 0 && key <= 9 )) {
$(this).val($(this).val().substr(0,$(this).val().length-1));
}
});
</script>
Updated with this (already solved)
<script type="text/javascript">
$("input[name='birthdate']:first").on('keyup', function(e){
var val = $(this).val();
var key = val.substr(val.length - 1)
var value=$(this).val();
if(value.length === 2|| value.length === 5)$(this).val($(this).val()+'/');
if(!(key>=0&&key<=9))
$(this).val(val.substr(0,val.length-1));
});
</script>
Try this, reading the last character instead of key code:
$("input[name='inputDate']:first").on('keyup', function(e){
var val = $(this).val();
var key = val.substr(val.length - 1)
if(!(key>=0&&key<=9))
$(this).val(val.substr(0,val.length-1));
});
JSFiddle
Note - this won't "protect" you from text being pasted in the input field! For that you'll need to bind input event. Also, a very fast typing can result in some letter "getting trough". So...
A better way of doing the same:
$('input[name="inputDate"]:first').on('input', function(e){
$(this).val( $(this).val().replace(/[^0-9]+/g, '') );
});
JSFiddle
And finally...
If you are creating a date input field and want to make it so that regular visitors can only input a valid date, check out Masked Input Plugin, or maybe some other "input mask" solution.
Be aware that this can still be bypassed with a number of ways and a server-side check is almost always necessary.

Categories