2014年9月5日金曜日

JavaScriptをかじっています♪

※ ソースコードを見やすくするために、Syntaxhilighterを使っています。状況によって表示に時間がかかる場合があります。

今回のものはゲームというよりも自分用の英語トレーニングツールですが、こんなのを作りました♪

『えいごであそぼう』の第2弾、LとRの特訓編です。


LとRの特訓



これ自体はAS3.0を使っていますが、ブログを書くに当たってArrayの中身をカンマ区切りの英単語リストに直す必要あり、お勉強も兼ねてJavaScriptで遊んでみました~ (次の1)を2)のように直したかった)

1) "rice", "lice", "rose", "louse", "light", "right", "grew", "glue", "slew", "straw",
2) rice, lice, rose, louse, light, right, grew, glue, slew, straw,


スクリプト
まだまだつたないコードで恥ずかしいのですが、何かのお役に立てればと思い、あげますね。私もWeb上のあちこちのサイトでお世話になったお礼です。ただし、使用される場合は自己責任である旨、ご了解願いますね。


ちなみに今回は特に次のサイトにお世話になりました。
http://www.html5rocks.com/ja/tutorials/file/dndfiles/
http://d.hatena.ne.jp/sho322/20130627/1372355192


今回のJavaScriptのコードです

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<input id="fileinput" type="file">
 
<script type="text/javascript">
// Extrracting Words from an Array described in AS3.0 format
// Input: inFile which needs to be a text file
// Output: Displays the words separated by a comma ',' and the number of the words
// By Nolan00267 2013-08-31
 
function readSingleFile(evt) {
//Retrieve the first (and only!) File from the FileList object
//In this script, inFile is a text file
var inFile = evt.target.files[0];
 
if (inFile) {
var r = new FileReader();
 
r.onload = function(e) {
// The contents of the inFile is stored as a string
// Trimming out unnecessary whilte spaces adn etc.
// Ex) rice", "lice", "rose", "louse", "light", "right", ...
var str = e.target.result;
str = str.replace(/^\s+|\s+$/g, "");
 
var word = new Array();
var output = new Array();
var count = 0;
 
while(true) {
// Find the first double-quotation mark. The location is stored as num_begin
var num_begin = str.indexOf("\"",0); // Finidng a double-quotation mark '"'
 
// If no double-quotation mark, go out from the while loop
if (num_begin == -1)
break;
 
// Extract the string after the "
var str_temp = str.substring(num_begin + 1,str.length);
// Ex) rice", "lice", "rose", "louse", "light", "right", "grew", "glue", "slew", "straw",
 
//Find the double-quotation for a word, assigining to num_end for the location
var num_end = str_temp.indexOf("\"", 0);
word[count]=str_temp.substring(0, num_end);
 
//Display the extraced word
document.write(word[count] + ", ");
 
str = str_temp.substring(num_end + 1, str_temp.length);
count++;
}
// Display the total number of the word
document.write(count);
 
 
// For inFile info
document.write(
"<br/><br/> The file information <br/>"
+"File name: " + inFile.name + "<br/>"
+"Type: " + inFile.type + "<br/>"
+"Size: " + inFile.size + " bytes"
);
}
 
r.readAsText(inFile);
} else {
alert("Failed to load file");
}
}
</script>



0 件のコメント:

コメントを投稿