diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2021-11-27 06:22:13 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2021-11-27 06:22:13 +0800 |
| commit | 0eda0bca04386212cbc96153d1c68ac4ac1d4dc8 (patch) | |
| tree | 50ed7edfa52301571ca457b178e6a2e6a5072514 | |
| parent | 0bf570cdb07085a3c431f68a15968458c706cb3e (diff) | |
| download | perlweeklychallenge-club-0eda0bca04386212cbc96153d1c68ac4ac1d4dc8.tar.gz perlweeklychallenge-club-0eda0bca04386212cbc96153d1c68ac4ac1d4dc8.tar.bz2 perlweeklychallenge-club-0eda0bca04386212cbc96153d1c68ac4ac1d4dc8.zip | |
Week 140 Task 2: JavaScript solution and Perl solution
| -rw-r--r-- | challenge-140/cheok-yin-fung/javascript/ch-2.js | 60 | ||||
| -rw-r--r-- | challenge-140/cheok-yin-fung/perl/ch-2.pl | 35 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-140/cheok-yin-fung/javascript/ch-2.js b/challenge-140/cheok-yin-fung/javascript/ch-2.js new file mode 100644 index 0000000000..2ddffa5f97 --- /dev/null +++ b/challenge-140/cheok-yin-fung/javascript/ch-2.js @@ -0,0 +1,60 @@ +/* Very rough JavaScript*/ + + +document.write("<p>The Weekly Challenge Week 140</p>"); +document.write("<p>Task 2 Multiplication Table</p>"); +document.write("i: <input type=\"text\" placeholder=\"\" id=\"i\">"); +document.write("<br>"); +document.write("j: <input type=\"text\" placeholder=\"\" id=\"j\">"); +document.write("<br>"); +document.write("k: <input type=\"text\" placeholder=\"\" id=\"k\">"); +document.write("<br>"); + + +document.write("<button onclick=\"calc()\">Calculate</button>" ); + + +function calc() { + let i = parseInt(document.getElementById("i").value); + let j = parseInt(document.getElementById("j").value); + let k = parseInt(document.getElementById("k").value); + + if (isNaN(i) || isNaN(j) || isNaN(k)) { + window.alert("Unable to parse integer. Terminate calculation."); + return; + } + + if (i*j<k) { + window.alert("k > i*j. Terminate calculation."); + return; + } + + + document.write("<p>" + "i: "+ i + ";j: " +j+";k: " + k + "." + "</p>"); + + document.write("<table border=\"1\" style=\"text-align:right\">"); + + let _arr = []; + + for (var _i = 1; _i <= i; _i++) { + document.write("<tr>"); + for (var _j = 1; _j <= j; _j++) { + document.write("<td>"); + document.write(_i*_j); + _arr.push(_i*_j); + document.write("</td>"); + } + document.write("</tr>"); + } + + document.write("</table>"); + + let arr = _arr.sort(); + + document.write("<p> Answer: "+arr[k-1]+"</p>"); + + + return; +} + + diff --git a/challenge-140/cheok-yin-fung/perl/ch-2.pl b/challenge-140/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..47335de088 --- /dev/null +++ b/challenge-140/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,35 @@ +# The Weekly Challenge 140 +# Task 2 Multiplication Table +# Usage: $ ch-2.pl $i $j $k +use v5.12.0; +use warnings; +use Test::More tests => 3; + +my ($i,$j,$k) = ($ARGV[0], $ARGV[1], $ARGV[2]); + +say table2array($i,$j,$k) if defined($ARGV[2]); + + + +sub table2array { + my ($i,$j,$k) = ($_[0], $_[1], $_[2]); + + die "k value too large \n" unless $i*$j >= $k; + + my @_arr; + + for my $_i (1..$i) { + for my $_j (1..$j) { + push @_arr, $_i*$_j; + } + } + + my @arr = sort {$a<=>$b} @_arr; + + return $arr[$k-1]; +} + +ok table2array(2, 3, 4) == 3; +ok table2array(3, 3, 6) == 4; +ok table2array(9, 8, 71) == 64; + |
