aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNuno Vieira <nunovieira220@gmail.com>2020-09-23 00:51:21 +0100
committerNuno Vieira <nunovieira220@gmail.com>2020-09-23 00:51:21 +0100
commit876afaf800b4d1290ace6500ddad34ca726de9d7 (patch)
tree54615003c337309e5292cdc6d5b33f727ff12ef7
parent127e524981e50269bf600100aaa0300a33951256 (diff)
downloadperlweeklychallenge-club-876afaf800b4d1290ace6500ddad34ca726de9d7.tar.gz
perlweeklychallenge-club-876afaf800b4d1290ace6500ddad34ca726de9d7.tar.bz2
perlweeklychallenge-club-876afaf800b4d1290ace6500ddad34ca726de9d7.zip
Add nunovieira220 js solution to challenge 079
-rw-r--r--challenge-079/nunovieira220/js/ch-1.js12
-rw-r--r--challenge-079/nunovieira220/js/ch-2.js17
2 files changed, 29 insertions, 0 deletions
diff --git a/challenge-079/nunovieira220/js/ch-1.js b/challenge-079/nunovieira220/js/ch-1.js
new file mode 100644
index 0000000000..9216296d84
--- /dev/null
+++ b/challenge-079/nunovieira220/js/ch-1.js
@@ -0,0 +1,12 @@
+// Input
+const N = 4;
+
+// Count set bits
+let counter = 0;
+
+Array.from({ length: N }, (_, i) => i + 1).forEach(n => {
+ const bin = parseInt(n, 10).toString(2);
+ counter += bin.split('1').length - 1;
+});
+
+console.log(counter); \ No newline at end of file
diff --git a/challenge-079/nunovieira220/js/ch-2.js b/challenge-079/nunovieira220/js/ch-2.js
new file mode 100644
index 0000000000..6fe1744ee6
--- /dev/null
+++ b/challenge-079/nunovieira220/js/ch-2.js
@@ -0,0 +1,17 @@
+// Input
+const A = [3, 1, 3, 1, 1, 5];
+
+// Trapped rain water counter
+let counter = 0;
+let max = 0;
+
+A.forEach(item => {
+ if(item >= max) {
+ max = item;
+ } else {
+ counter += max - item;
+ }
+});
+
+// Output
+console.log(counter); \ No newline at end of file