I am trying to translate all alphabets in a message to p alphabet(s) in the sequence. for example: Translate("bbcd", 2) will return "ddef". I am stuck at the array part where alphabets[j+p] returns undefined. If I do alphabets[j-p], it will work.
Here is my code:
function Translate(Message, p) {
var alphabets = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
var message_array = Message.split('');
for(var i in message_array){
for(var j in alphabets){
if(message_array[i]==alphabets[j]){
message_array[i]=alphabets[j+p];//returns undefined
}
}
}
return message_array;
}
console.log(Translate("bbcd", 2));
The problem is j+p which is doing a string concatenation, typeof j will return string as you are reading the array index using for...in
function Translate(message, p) {
var alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
return message.split('').map(function(c) {
var idx = alphabets.indexOf(c);
return idx == -1 ? c : alphabets[(idx + p) % alphabets.length]
});
}
var value = Translate("azpq", 2);
result.innerHTML = JSON.stringify(value);
<div id="result"></div>
alphabets[j+p]; returns undefined for j == 26 that it will reach for the inner loop iteration.
use indexOf method of array and change your loop structure to
for(var i in message_array)
{
var index = alphabets.indexOf( message_array[ i ] );
message_array[ i ] = alphabets[ index + 2 ];
}
This also will reach 'undefined' if your Message has a y or z in it.
so, you might want to cycle it back to the beginning
for(var i in message_array)
{
var index = alphabets.indexOf( message_array[ i ] );
message_array[ i ] = alphabets[ (index + 2) % alphabets.length ];
}
function Translate(Message, p) {
var alphabets = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
var messageLength = Message.length;
var returnMessage = '';
for(var i=0; i< messageLength; i++) {
var myChar = Message.charAt(i);
var placeChar = alphabets.indexOf(myChar);
myChar = alphabets[placeChar + p];
returnMessage += myChar;
}
return returnMessage;
}
console.log(Translate("bbcd", 2));
You need to break once the value is found. Otherwise the inner loop will find a match again on the coming cycles.
function Translate(Message, p) {
var alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
var message_array = Message.split('');
for (var i in message_array) {
for (var j in alphabets) {
if (message_array[i] == alphabets[j]) {
console.log(alphabets[parseInt(j) + parseInt(p)])
message_array[i] = alphabets[parseInt(j) + parseInt(p)]; //returns undefined
break;
}
}
}
return message_array;
}
alert(Translate("bbcd", 2));
You need to make sure that j+p is never bigger than the length of alphabets. This will result in a "out of bounds" error. e.g. p = 30.
A fix is to just check the length compared to p and returns something that is defined.
message_array[i] = (j+p > alphabets.length) ? alphabets[j+p] : alphabets[j]; // returns element at j if out of bounds
This suggestion works with an alphabet string instead of an array.
You can access a string like an array for a single character. For the lookup i suggest to use indexOf(), a method availabele at string objects String#indexOf() as well as array objects Array#indexOf(). Both methods returns the position of the found item/string/character or -1 if not found.
For the result I took an empty string and concatinated it with the new character.
For the correct range of the index, while adding some value, the method of choice is to use the modulo operator. It returns the reminder of a division by the second number. Here it is the length of the alphabet string.
function translate(message, p) {
var alphabet = 'abcdefghijklmnopqrstuvwxyz',
i, pos, result = '';
for (i = 0; i < message.length; i++) {
pos = alphabet.indexOf(message[i]);
result += alphabet[(pos + p) % alphabet.length];
}
return result;
}
document.write(translate("bbcd", 2));
Related
I'm struggling with the logic to do the Recursion.
For the grid below I need to search a word and return an array of indexes that form the word if it exists or an empty array [] if it doesn't exist.
The word may start anywhere in the grid, and consecutive letters can be either immediately below or immediately to the right of the previous letter.
grid = [
['c', 'c', 'x', 't', 'i', 'b'],
['c', 'c', 'a', 't', 'n', 'i'],
['a', 'c', 'n', 'n', 't', 't'],
['t', 'c', 's', 'i', 'p', 't'],
['a', 'o', 'o', 'o', 'a', 'a'],
['o', 'a', 'a', 'a', 'o', 'o'],
['k', 'a', 'i', 'c', 'k', 'i'],
];
word = "catnip"
find_word_location(grid, word)
// OUTPUT [ (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), (3, 4) ]
This is what I have so far but it's not working.
function find_word_location (grid, word) {
const dfs = (i, j, wordIndex, res) => {
if (wordIndex == word.length) return;
if (
i > grid.length - 1 ||
j > grid[0].length - 1 ||
grid[i][j] !== word[wordIndex]
)
return;
if (grid[i][j] == word[wordIndex]) {
res.push(`(${i},${j})`);
grid[i][j] = "#";
}
dfs(i + 1, j, wordIndex + 1, res);
dfs(i, j + 1, wordIndex + 1, res);
grid[i][j] = word[wordIndex];
return res;
};
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[0].length; j++) {
if (grid[i][j] == word[0]) {
let result = dfs(i, j, 0, []);
return result;
}
}
}
return [];
}
Some of the issues:
After the initial call of dfs your code always returns the result. This means that it assumes that if the first letter matches, the whole word must be matched starting at that cell. But this is not true. It might not find a match there, while there might be other places in the grid with that first letter which do give a full word match. So this return statement should be conditional (only when there is success).
The array referenced by res only grows. It never shrinks. Realise there is only one res array -- referenced by all res variables that live in the dfs execution contexts. So all partial matches (that eventually fail to lead to a full match) are collected in it, and concatenated one after the other. There is no backtracking applied to it.
I find it more elegant to actually only start building an array when the whole word has been matched, and then fill up an array while getting out of the recursive calls (as opposed to doing this while deepening the recursion).
The recursive calls of dfs completely ignore the values that those calls return, so no distinction is made between failure and success. You'd need to check the result of the first recursive call and if it was successful, the second one should not even be made.
Not a problem, but the condition in if (grid[i][j] == word[wordIndex]) { will always be true, given you already tested the opposite condition in the preceding if statement.
Here is a correction to your code, also implementing the idea I expressed in the second point, i.e. only filling up an array when it is already known there was a success:
// No res argument. res will be populated "inside-out" via the return value
function find_word_location (grid, word) {
const dfs = (i, j, wordIndex) => {
if (wordIndex == word.length) return []; // Start a solution with this res
if (
i > grid.length - 1 ||
j > grid[0].length - 1 ||
grid[i][j] !== word[wordIndex]
)
return;
grid[i][j] = "#";
// Use the returned value
// and apply short-circuit to avoid useless second call
const res = dfs(i + 1, j, wordIndex + 1) || dfs(i, j + 1, wordIndex + 1);
grid[i][j] = word[wordIndex];
if (res) res.unshift([i, j]); // Extend the path we got from recursion
return res;
};
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[0].length; j++) {
if (grid[i][j] == word[0]) {
let result = dfs(i, j, 0, []);
if (result) return result; // conditionally
}
}
}
return [];
}
var grid = [
['c', 'c', 'x', 't', 'i', 'b'],
['c', 'c', 'a', 't', 'n', 'i'],
['a', 'c', 'n', 'n', 't', 't'],
['t', 'c', 's', 'i', 'p', 't'],
['a', 'o', 'o', 'o', 'a', 'a'],
['o', 'a', 'a', 'a', 'o', 'o'],
['k', 'a', 'i', 'c', 'k', 'i'],
];
var word = "catnip";
var result = find_word_location(grid, word);
console.log(result);
This approach does not hand over the result to the recursive call.
The function takes grid, the word or the left over word part, and the actual indices, which has zero as default value.
Inside, the exit condition comes first by checking boundaries.
Then a check for the wanted character follows.
Inside it check with new word length, without actual character and if an empty string, it returns the indices.
The rest is nearly identically without the part for checking the found indices and if they are adjacent to the actual elememnt.
Roughly, get sub indices, check if there are indices and return the result either with actual indices (letter match) or just the rest.
const
findWord = (grid, word, i = 0, j = 0) => {
const isAdjacent = (x, y) => x === i && y === j + 1 || x === i + 1 && y === j;
let sub;
if (i + 1 === grid.length || j + 1 === grid[i].length) return [];
if (grid[i][j] === word[0]) {
const w = word.slice(1);
if (!w.length) return [[i, j]];
sub = findWord(grid, w, i + 1, j);
if (sub.length && isAdjacent(...sub[0])) return [[i, j], ...sub];
sub = findWord(grid, w, i, j + 1);
if (sub.length && isAdjacent(...sub[0])) return [[i, j], ...sub];
}
sub = findWord(grid, word, i + 1, j);
if (sub.length) return sub;
sub = findWord(grid, word, i, j + 1);
if (sub.length) return sub;
return [];
},
grid = [['c', 'c', 'x', 't', 'i', 'b'], ['c', 'c', 'a', 't', 'n', 'i'], ['a', 'c', 'n', 'n', 't', 't'], ['t', 'c', 's', 'i', 'p', 't'], ['a', 'o', 'o', 'o', 'a', 'a'], ['o', 'a', 'a', 'a', 'o', 'o'], ['k', 'a', 'i', 'c', 'k', 'i']],
word = "catnip",
result = findWord(grid, word);
console.log(result.map(p => p.join('-')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
For example say you have the string 'ab?d?f' and you must grab the string and replace it with any random letters in the '?' like 'abcdef' or 'abjdlf' but it cannot be 'abbdef' or 'abcdff'.
I have attempted this below:
const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
const randomLeter = () => letters[Math.floor(Math.random() * 26)];
const riddle = 'ab?d?f';
let pickedLetter;
let solvedRiddle;
function solve(riddle) {
for (let i = 0; i < riddle.length; i++) {
if (riddle[i] === '?' && !(riddle[i-1] === randomLeter) && !(riddle[i+1] === randomLeter)){
console.log('Changing to letter');
solvedRiddle = riddle.replace('?', pickedLetter);
}
pickedLetter = randomLeter();
console.log(i, riddle[i], pickedLetter);
}
return solvedRiddle;
}
// The above only returns the first '?' changed but leaves the second one unchanged ... ??? Why can I not change the value of solvedRiddle a second time inside the loop? I can see by my log that it reads at true, but the value won't be re-written.
console.log(solve(riddle));
function solve(riddle) {
for (let i = 0; i < riddle.length; i++) {
if (riddle[i] === '?'){
console.log('Changing to letter');
let pickedLetter = randomLeter();
while (riddle[i-1] === pickedLetter || riddle[i+1] === pickedLetter) {
pickedLetter = randomLeter();
}
riddle = riddle.replace('?', pickedLetter);
}
}
return riddle;
}
Also, it's because u update and return solvedRiddle. Your original riddle string is not updated, so in the second run, it is still changing the first ?
I modified your code so that it suits the needs, however there may be some bugs or exceptions not handled yet, but it can give you a hint that how you are going to solve the problem.
const letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
let randomLeter = (excluded) =>
{
let letter = false;
do
{
letter = letters[Math.floor(Math.random() * 26)];
}
while(excluded.includes(letter));
return letter;
}
let getIndices = (s, t) => {
return [...s].flatMap((char, i) => (char === t ? i : []));
};
let Solve = (riddle) =>
{
riddle = [...riddle];
//get the locations of all ? in the string
let all = getIndices(riddle, '?');
for(let i = 0; i < all.length; i++)
{
//exluding characters before and after the ?
let excluded = [riddle[all[i] - 1], riddle[all[i] + 1]];
riddle[all[i]] = randomLeter(excluded);
}
return riddle.join("");
};
let riddle = 'ab?d?f';
document.getElementById('riddle').innerHTML = 'The Riddle ' + riddle;
document.getElementById('solution').innerHTML = "Solution " + Solve(riddle);
<h2 id="riddle"></h2>
<h4 id="solution"></h4>
The code below is used to generate a random word.
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function randomWord(length=6){
string = '';
vowels = ["a","e","i","o","u"];
consonants = [
'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm',
'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'
];
max = length / 2;
for (i = 1; i <= max; i++)
{
string += consonants[randomInteger(0,19)];
string += vowels[randomInteger(0,4)];
}
return string;
}
Based on this, I am using the following function to create a sentence.
function randomSentence(tot=10){
returner = '';
for(j=0;j<tot;j++){
returner = returner + " " + randomWord();
}
return returner;
}
postContent = [];
for(a=0;a<10;a++){
postContent.push(randomSentence(5));
}
The value of i<10 is what causes the program to crash, it seems to work when the value is set to 5.
I'm trying to create a simple javascript password generator that spits out the number of characters the user puts in. So, lets say you enter the number 7. I want the program to then spit out 7 random characters from the possibilities variable. Below is my code so far:
var possibilities = ['A', 'B', 'C', 'D', 'E', 'F', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
];
var special_chars = "~`!#$%^&*+=-[]\\\';,/{}|\":<>?"; //no special characters
function generator() {
if (document.getElementById("input").value == '' || document.getElementById("input").value > 61) {
alert("Enter under 61 characters");
} else if (isNaN(document.getElementById("input").value) || document.getElementById("input").value == special_chars) {
alert("Enter numbers only");
} else {
//var userInput = document.getElementById("input").value -- get value of this when clicked, spit out number of items from this array based on the value collected
for (var x = 0; x < possibilities.length; x++) {
var content = possibilities[Math.floor(Math.random() * possibilities.length)];
}
document.getElementById("random_password").innerHTML = content;
}
}
I'm a bit new to JavaScript and am kind of stuck here so any help is appreciated :) Thx in advance!
Here is a working demo. This is uses an existing answer found here. I have added minor changes to fit this question.
function makeid(){
var IdLength=document.getElementById('IdLength');
//Strip anything but 0 to 9
var UserInput=IdLength.value.replace(/[^0-9.]/g, "");
//Update input value
IdLength.value=UserInput;
var Results=document.getElementById('results');
var text = "";
var shuffle = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//Is input is empty?
if(IdLength!==''){
for( var i=0; i < IdLength.value; i++ ){
text += shuffle.charAt(Math.floor(Math.random() * shuffle.length));
}
Results.innerHTML=text;
}
}
<input type="text" id="IdLength" oninput="makeid()" placeholder="Enter 0-9"/>
<span id="results"></span>
I hope this helps. Happy coding!
If you are looking for something basic then you should try the Jen library (works both on browsers and nodejs/io.js).
And actually it is not recommended to use Math.random() for crypto application (such as password generation)
This is a small script that I wrote. I do use this on a day to day basis to generate passwords. It is not perfect but it will do the small tasks. Feel free to play around with it and make it more perfect.
This is the JavaScript code plus the HTML code so you can try it. Basically, you can give Length anything that is a string(I haven't make an error checking for that). If the length is equal to 'rand' then the generator will generate a random password with a length between 19 to 30 characters. If length can't be converted to a number or is lower than 13 then the password generator will generate a password with a length of 13.
For the variable "not" and the feature of "Not". The password generator will not generate a password that contained any character that you placed in the Not field.
When it comes to generating the password, the generator will generate one character for each variable of s, n, u, l(4 in total). It will then generate the rest of it length randomly using the characters from variable a. After that, the generator will shuffle the string that it just generated randomly. It will then alert you the length of the password and give the output of the password in the shadowed box.
Also, it is just easier to obtain a random character at a random position in the string then taking a character from a random index in an array. It is easier when it comes to reconfiguring the script.
<html>
<head>
<script type="text/javascript">
function rand( len, not ){
if ( len === undefined ) len = 13;
if ( typeof not !== 'string' ) not = '';
if ( len === 'rand' ) len = Math.floor(Math.random() * (30 - 19) + 19);
if ( typeof len === 'string' ) len = parseInt(len, 10);
if ( len < 13 || isNaN(len) ) len = 13;
var s = '!#~!##$%^&*()-_=+',
n = '0123456789',
u = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
l = 'abcdefghijklmnopqrstuvwxyz',
a = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#~!##$%^&*()-_=+',
out = '';
if ( (nl = not.length) > 0 ){
var regex = '';
for ( var i = 0; i < nl; i++ ){
regex = not.charAt(i).replace(/[.*+?^${}()|[\]\\]/, '\\$&');
regex = new RegExp( regex , 'g');
if ( s.length > 0 ) s = s.replace( regex, '');
if ( n.length > 0 ) n = n.replace( regex, '');
if ( u.length > 0 ) u = u.replace( regex, '');
if ( l.length > 0 ) l = l.replace( regex, '');
a = a.replace( regex, '');
}
}
if ( s.length > 0 ) out += s.charAt( Math.floor(Math.random() * s.length) );
if ( n.length > 0 ) out += n.charAt( Math.floor(Math.random() * n.length) );
if ( u.length > 0 ) out += u.charAt( Math.floor(Math.random() * u.length) );
if ( l.length > 0 ) out += l.charAt( Math.floor(Math.random() * l.length) );
for ( var i = out.length; i < len; i++) out += a.charAt( Math.floor(Math.random() * a.length) );
out = out.split('').sort(function(a, b){return 0.5 - Math.random()}).join('');
alert(out.length);
return out;
}
function generate(){
document.getElementById("result").value = rand( document.getElementById("length").value, document.getElementById("not").value );
}
</script></head>
<body>
<label for="length">Length:</label><input type="text" id="length" maxlength="5" size="5">
<label for="not">Not:</label><input type="text" id="not" size="12">
<button onclick="generate();" type="button">Generate</button><br />
<input type="text" id="result" size="35" value="Result" disabled><br />
</body>
</html>
Memorable password generator
// Also you can use a custom function to get random numbers instead
const _ = require('lodash/number');
/*
* Generate comfortable to remember password
*
* #param integer length - length of generated password
*
* #return string password
*/
function my_create_password(length)
{
let vowel = [
'a',
'e',
'i',
'o',
'u',
];
let consonant = [
'b',
'c',
'd',
'f',
'g',
'h',
'j',
'k',
'l',
'm',
'n',
'p',
'q',
'r',
's',
't',
'v',
'w',
'x',
'y',
'z',
];
result = '';
for (let i = 0; i < length; i++) {
if (i % 2 === 0) {
result += consonant[_.random(0, 15)];
} else {
result += vowel[_.random(0, 4)];
}
}
return result;
}
Result:
password(8);//kutekaku
password(11);//rahubabatun
In Javascript, I have 2 arrays of 15 string elements, each of 0 to 17 characters.
How can I tell if one of the values of the first of these two arrays has a value equal to one of the values of the second array?
Example:
var array1 = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'];
var array2 = ['z','z','z','z','z','z','z','z','z','z','z','z','z','o','z'];
myFunction(array1,array2); // returns false
Example 2:
var array1 = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','s'];
var array2 = ['z','z','z','z','z','z','z','z','z','z','z','z','z','o','z'];
myFunction(array1,array2); // returns true
Eric J.'s suggestion of an intersection is probably most elegant way to approach the problem.
A straightforward translation of what you're asking could work like this:
function containsAny(array1, array2) {
for (var i=0; i<array1.length; i++) {
for (var j=0; j<array2.length; j++) {
if (array1[i] == array2[j]) return true;
}
}
return false;
}
var array1 = ['a','b','c'];
var array2 = ['1','2','3'];
var array3 = ['a','b','2'];
containsAny(array1, array2); // returns false
containsAny(array2, array3); // returns true
containsAny(array1, array3); // returns true
Assuming dbaupp is right and you have a typo, you want to find the intersection of the two arrays and see if it is non-empty.
The intersection of two sets means the set of elements that both original sets have in common. If there is at least one element in common, the intersection will be non-empty.
To do that, see
Simplest code for array intersection in javascript
This code checks if the two lists have a common element, which is done by sorting the lists and then stepping through them together, advancing whichever list is "behind".
function(list1, list2) {
var l1 = list1.sort(), l2 = list2.sort(),
len1 = l1.length, len2 = l2.length,
i1 = 0, i2 = 0;
while (i1 < len1 && i2 < len2) {
if (l1[i1] == l2[i2])
return true;
if (l1[i1] < l2[i2])
i1++;
else
i2++;
}
return false;
}
NB. as Eric said you could also just intersect the two lists, and check that the resulting list is non-empty, but this code here will be more efficient since it doesn't have to generate a whole new list, nor does it have to go through all of both lists if there is a common element.
Ok, assuming I'm understanding your problem correctly, I tossed together a Fiddle that will match the elements of 2 arrays using the JQuery 'each' method:
http://jsfiddle.net/phillipkregg/Ug3DM/1/
var array1 = ["a","b","c","o"];
var array2 = ["z","z","b","z","z","o"];
array1.each(function(item){
var element = item;
var match = [];
array2.each(function(item2) {
if (item2 === element)
{
match.push(item2);
alert(match);
}
});
});
Maybe that will get you started.
I try to use the 'each' method upstairs, but it seems not work, while 'each' is used for objects, not for array.
so I change the code.
var array1 = ["a","b","c","o"];
var array2 = ["z","z","b","z","z","o"];
var match = [];
$.each(array1, function(key, val){
var element = val;
$.each(array2, function(key, val){
if(element === val)
{
match.push(val);
}
});
});
$.each(match, function(key, val){
alert(key+'--'+val);
});
Most browsers use builtin indexOf and filter methods of Arrays,
if you use these methods often you can conditionally add
them to older browsers.
var array1= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 's'];
var array2= ['z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'z', 'o', 'z'];
var array3= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'o', 'k', 'l', 'm', 'n', 's'];
1. array1.inCommon(array2)>>
(array length 0)
2. array2.inCommon(array3)>>
(array length 1)
o
3. array3.inCommon(array1)>>
(array length 14)
a, b, c, d, e, f, g, h, i, k, l, m, n, s
Array.prototype.inCommon= function(ar){
return this.filter(function(itm){
return ar.indexOf(itm)!= -1;
});
}
Array.prototype.indexOf= Array.prototype.indexOf || function(what, i){
if(typeof i!= 'number') i= 0;
var L= this.length;
while(i< L){
if(this[i]=== what) return i;
++i;
}
return -1;
}
Array.prototype.filter= Array.prototype.filter || function(fun, scope){
var T= this, A= [], i= 0, itm, L= T.length;
if(typeof fun== 'function'){
while(i< L){
if(i in T){
itm= T[i];
if(fun.call(scope, itm, i, T)) A[A.length]= itm;
}
++i;
}
}
return A;
}