📜 ⬆️ ⬇️

We play in the country and some JavaScript

Somewhere a couple of weeks ago, I was wandering through the vast expanses of the Internet and came across such an interesting “game” link . Through this page, you can check how many countries you are in a position to remember the respected% username%. But for one moment I was essentially alerted and puzzled. The first country that came to my mind was Canada, it was successfully added to the list of countries that I remembered. Then a few more. When I entered Australia, the script clearly failed. Nothing happened. "Australia" is so funny in the text box for entering countries. I questioned the writing of the country and climbed into the wiki to clarify. But there was no mistake in writing. Isn't it David David Blaine? But climbed into FireBug discovered the following interesting thing
var countries = new Array ( 'Australia' , 'Austria' , 'Azerbaijan' , ...);
// function of checking whether the entered word is a country
function isCountry ©
{
for ( var i = 0; i <countries.length; i ++) {
if (c.toLowerCase () == countries [i] .toLowerCase ())
{
return i;
}
}
return false ;
}
// directly check itself

if (i = isCountry (v))
{
found.innerHTML + = countries [i] + ',' ;
countC ++;
countries.splice (i, 1);
if (countC == 0) count.innerHTML = 'You remembered ALL countries !!!' ;
else count.innerHTML = 'Remains' + countries.length;
t.value = " ;
}
* This source code was highlighted with Source Code Highlighter .


so "Australia" in the array of countries with the index "0". The isCountry function will work out normally and return the index of Australia's entry - 0.
The following function outputting the found countries to the block, when checking the condition if (i = isCountry (v)), will receive i = 0
that is, if (0) and as a result, the version with Australia will be “considered false”. In JS, as in PHP, false is equivalent to 0.
If we recall such a course as “software testing”, then the program works most critically at the limits of the allowable interval, and when drawing up test options, it is necessary to take into account, first of all, extreme options.
Thanks also to everyone who read this line up. And now let me suggest a solution to the same problem, but using Jquery
')
var countries = new Array ( 'Australia' , 'Austria' , ... 'Japan' );

var count = 0;
// function that searches the array by value (analog php function)
function array_search (needle, haystack, strict) {
// successful
//
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)

var strict = !! strict;

for ( var key in haystack) {
if ((strict && haystack [key] .toLowerCase () === needle) || (! strict && haystack [key] .toLowerCase () == needle)) {
return key;
}
}

return false ;
}
// timer variables
var minutes = 4;
var seconds = 59;

$ ( document ) .ready ( function () {
// start the timer
var timer = window.setInterval ( function ()
{ var timeStr = '' ;
if (seconds <0) minutes--;
if (minutes <= 0 && seconds <= 0)
{
$ ( '#result' ) .empty (). append ( “You remembered the“ + count + “countries” );
$ ( '#clock' ) .empty (). val ();
window.clearInterval (timer);
}
if (seconds == -1) {
seconds = 59;
}
timeStr + = "0" + minutes;
timeStr + = ((seconds <10)? ": 0" : ":" ) + seconds;
$ ( '#clock' ) .empty (). val (timeStr);
seconds--;
}, 1000);

// direct verification entered on the country
$ ( '#text' ) .keyup ( function () {
var result = array_search ($ ( '#text' ) .val (). toLowerCase (), countries);
if (result! == false ) {
count ++;
seconds + = 3;
$ ( '#found' ) .append (countries [result] + "," );
$ ( '#again' ) .show ();
countries.splice (result, 1);
$ ( '#text' ) .val ( "" );
$ ( '#count' ) .empty (). append ( "Left:" + countries.length);
}
});


});
* This source code was highlighted with Source Code Highlighter .

In general, everything is the same, but on Jquery. It looks nicer and clearer. Australia’s zero-index problem could be solved in two ways by introducing a fictitious zero element into the list of countries (but why keep redundant data, and this is more like patching rather than eliminating an error) or act differently. As was said above, 0 and false in JS are equivalent (and the functions IsNaN and IsFinite do not distinguish between them. Recall a construction of the form === which not only checks for a match of values, but also a coincidence of types
thus array_search () also returns the entry of a particular element.

var result = array_search ($ ( '#text' ) .val (). toLowerCase (), countries);
if (result! == false ) * Source code highlighter .

for Australia, 0 is returned, but 0 is different in type, from false, and thus the code block responsible for the output of the found countries will work in the normal mode. =)

Thanks to all. I hope this article even helped you something, or made you think about it. And this is my PPNH, do not judge strictly, I'm still not a magician, I'm just learning
Long could not lay out, what he wrote, lacked karma

Source: https://habr.com/ru/post/40506/


All Articles