開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- Node.js, Safari(JavaScript エンジン)
- Learning JavaScript [邦訳](参考書籍)
Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)の Part 1(Starting with the basics)、Chapter 7(Strings)の Exercise 7-1、7-3.を JavaScript で取り組んでみる。
Exercise 7-1、7-3.
コード(Emacs)
HTML5
<pre id="output0"></pre> <label for="word0">word0: </label> <input id="word0" type="text" value="banana"> <label for="letter0">letter0: </label> <input id="letter0" type="text" value="a"> <label for="word1">word1: </label> <input id="word1" type="text" value="IBM"> <label for="word1">n = </label> <input id="n0" type="number" step="1" value="-1"> <button id="run0">run</button> <button id="clear0">clear</button> <script src="sample1.js"></script>
JavaScript
let btn0 = document.querySelector('#run0'),
btn1 = document.querySelector('#clear0'),
pre0 = document.querySelector('#output0'),
input_word0 = document.querySelector('#word0'),
input_letter0 = document.querySelector('#letter0'),
input_word1 = document.querySelector('#word1'),
input_n = document.querySelector('#n0'),
inputs = [input_word0, input_letter0, input_word1, input_n],
p = (x) => pre0.textContent += x + '\n';
let count = (word, letter) => {
let count = 0,
i = 0;
for(;;) {
i = word.indexOf(letter, i);
if (i === -1) {
break;
}
i += 1;
count += 1;
}
return count;
};
let count1 = (word, letter) => {
let count = 0;
for (let i = 0; i < word.length; i += 1) {
if (word.substring(i, i + 1) === letter) {
count += 1;
}
}
return count;
};
let count2 = (word, letter) =>
word.split('').filter((c) => c === letter).length;
let rotateLetter = (letter, n) => {
n %= 26;
n += n < 0 ? 26 : 0;
let o = letter.codePointAt(0) + n;
if (/[a-z]/.test(letter)) {
return String.fromCodePoint(o - ('z'.codePointAt(0) < o ? 26 : 0));
}
if (/[A-Z]/.test(letter)) {
return String.fromCodePoint(o - ('Z'.codePointAt(0) < o ? 26 : 0));
}
return letter;
};
let rotateWord = (word, n) =>
word.split('')
.map((c) => rotateLetter(c, n))
.join('');
let UnitTest = () => {
let that = {},
run = () => {
Object.keys(that).forEach((key) => {
if (that.setUp) {
that.setUp();
}
if (/^test/.test(key)) {
p(key);
that[key]();
}
if (that.tearDown) {
that.tearDown();
}
});
},
assertEqual = (x, y) => {
if (x === y) {
p('ok');
} else {
p(`failure - ${x} !== ${y}`);
}
},
assertTrue = (x) => {
if (x) {
p('ok');
} else {
p(`failure`);
}
},
assertFalse = (x) => {
if (x) {
p('failure');
} else {
p(`ok`);
}
},
assertThrow = (fn, name) => {
try {
fn();
p('failure');
} catch (e) {
if (e.name === name) {
p('ok');
} else {
p('failure');
}
}
};
that.run = run;
that.assertEqual = assertEqual;
that.assertTrue = assertTrue;
that.assertFalse = assertFalse;
that.assertThrow = assertThrow;
return that;
};
let Test = () => {
let that = UnitTest();
that.test_count01 = () => that.assertEqual(count('', 'a'), 0);
that.test_count02 = () => that.assertEqual(count('a', 'a'), 1);
that.test_count03 = () => that.assertEqual(count('b', 'a'), 0);
that.test_count04 = () => that.assertEqual(count('banana', 'a'), 3);
that.test_count05 = () => that.assertEqual(count('bnn', 'a'), 0);
that.test_count06 = () => that.assertEqual(count('日本語', 'a'), 0);
that.test_count07 =
() => that.assertEqual(count('日本語abcde日本語', '日'), 2);
that.test_count11 = () => that.assertEqual(count1('', 'a'), 0);
that.test_count12 = () => that.assertEqual(count1('a', 'a'), 1);
that.test_count13 = () => that.assertEqual(count1('b', 'a'), 0);
that.test_count14 = () => that.assertEqual(count1('banana', 'a'), 3);
that.test_count15 = () => that.assertEqual(count1('bnn', 'a'), 0);
that.test_count16 = () => that.assertEqual(count1('日本語', 'a'), 0);
that.test_count17 =
() => that.assertEqual(count1('日本語abcde日本語', '日'), 2);
that.test_count21 = () => that.assertEqual(count2('', 'a'), 0);
that.test_count22 = () => that.assertEqual(count2('a', 'a'), 1);
that.test_count23 = () => that.assertEqual(count2('b', 'a'), 0);
that.test_count24 = () => that.assertEqual(count2('banana', 'a'), 3);
that.test_count25 = () => that.assertEqual(count2('bnn', 'a'), 0);
that.test_count26 = () => that.assertEqual(count2('日本語', 'a'), 0);
that.test_count27 =
() => that.assertEqual(count2('日本語abcde日本語', '日'), 2);
that.test_rotate1 = () => that.assertEqual(rotateWord('A', 3), 'D');
that.test_rotate2 = () => that.assertEqual(rotateWord('Z', 1), 'A');
that.test_rotate3 = () => that.assertEqual(rotateWord('cheer', 7), 'jolly');
that.test_rotate4 =
() => that.assertEqual(rotateWord('melon', -10), 'cubed');
that.test_rotate5 = () => that.assertEqual(rotateWord('IBM', -1), 'HAL');
that.test_rotate6 =
() => that.assertEqual(
rotateWord(rotateWord('Think Perl 6', 13), 13),
'Think Perl 6');
return that;
};
let output = () => {
Test().run();
let word0 = input_word0.value,
letter0 = input_letter0.value,
word1 = input_word1.value,
n = parseInt(input_n.value, 10);
p(count(word0, letter0));
p(count1(word0, letter0));
p(count2(word0, letter0));
p(rotateWord(word1, n));
};
inputs.forEach((input) => input.onchange = output);
btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';
output();
0 コメント:
コメントを投稿