I have completed the first version of a web application and deployed it to Heroku.
It is pretty simple for now -- a MEAN stack app that generates the standings of an eight-team league based on the scores entered by users. Some decent CRUD functionality, allowing users also to access games, edit game data and delete games. The back end storage is provided by MongoLab.
No user authentication so far. The next step is to require users to log in to create games and to let them edit or delete only those games that they created.
shielded-oasis-7953.herokuapp.com
Monday, October 26, 2015
Wednesday, May 20, 2015
More JavaScript
Code learning continues. Another great resource is www.interviewcake.com. I haven't explored the entire site, but I like the weekly challenge the owner sends out by email. One recent challenge involved taking a given string and evaluating whether it could be re-arranged into a palindrome (e.g., "house" would return "false", "civci" would return "true", etc.). Here is one solution:
function checkPal (str) {
var counts = {};
var ch, count, i, oddCount, key;
for (i = 0; i < str.length; i++) {
ch = str.charAt(i);
if (counts[ch] > 0) {
counts[ch]++;
} else {
counts[ch] = 1;
}
}
oddCount = 0;
for (key in counts) {
if (counts[key] % 2 == 1) {
oddCount++;
}
}
if (oddCount == 1 || oddCount === 0) {
return true ;
} else {
return false;
}
}
function checkPal (str) {
var counts = {};
var ch, count, i, oddCount, key;
for (i = 0; i < str.length; i++) {
ch = str.charAt(i);
if (counts[ch] > 0) {
counts[ch]++;
} else {
counts[ch] = 1;
}
}
oddCount = 0;
for (key in counts) {
if (counts[key] % 2 == 1) {
oddCount++;
}
}
if (oddCount == 1 || oddCount === 0) {
return true ;
} else {
return false;
}
}
The solution first creates an associative array from the characters of the string. The important bit comes next. A palindrome has at most one character which appears an odd number of times. The function counts the values of each key, and if there is more than one key with an add value, the string cannot be a palindrome, and the function returns "false".
Tuesday, February 17, 2015
JavaScript
I've been learning JavaScript. A great way to do it is to complete challenges, such as Project Euler. One good set I found recently is at www.coderbyte.com -- a good collection of easy, medium and hard challenges.
There is a challenge in the "easy" category, ArrayAdditionI, which seems to give beginning coders difficulty, at least based on the comments I have seen online.
When I looked at it a few weeks ago, I couldn't figure it out. I left it and went on to other challenges. I finally figured out a solution a couple of days ago and completed the coding today. This solution uses binary numbers to check all possible combinations.
function ArrayAdditionI (arr) {
var b = Math.max.apply(null, arr);
var c = arr.indexOf(b);
d = arr.splice(c,1);
d = d.join("");
d = Number(d);
for (var x = 1; x < Math.pow(2,arr.length); x++) {
var y = x.toString(2).split("");
var total = 0;
for (var z = 1; z < y.length + 1; z++ ) {
if (y[y.length - z] === "1") {
total += arr[z-1];
}
}
if (total === d) {return true;}
}
return false;
}
There is a challenge in the "easy" category, ArrayAdditionI, which seems to give beginning coders difficulty, at least based on the comments I have seen online.
When I looked at it a few weeks ago, I couldn't figure it out. I left it and went on to other challenges. I finally figured out a solution a couple of days ago and completed the coding today. This solution uses binary numbers to check all possible combinations.
function ArrayAdditionI (arr) {
var b = Math.max.apply(null, arr);
var c = arr.indexOf(b);
d = arr.splice(c,1);
d = d.join("");
d = Number(d);
for (var x = 1; x < Math.pow(2,arr.length); x++) {
var y = x.toString(2).split("");
var total = 0;
for (var z = 1; z < y.length + 1; z++ ) {
if (y[y.length - z] === "1") {
total += arr[z-1];
}
}
if (total === d) {return true;}
}
return false;
}
Subscribe to:
Posts (Atom)