Skip to content

Commit

Permalink
Adding new live assignment (#9)
Browse files Browse the repository at this point in the history
* Adding Anagram assignment donated by Capgemini and model answer.

* Tidy up to meet PR guidelines.

* Formatting change requested in PR

* Removing blank lines at end to pass code review.

* Removing blank lines at end to pass code review.

* Removing blank lines at end to pass code review.

* Removing blank lines at end to pass code review.

---------

Co-authored-by: atg103 <[email protected]>
  • Loading branch information
atg103 and atg103 authored Sep 18, 2024
1 parent 918c3aa commit a671c78
Show file tree
Hide file tree
Showing 17 changed files with 7,972 additions and 0 deletions.
2 changes: 2 additions & 0 deletions live-assignments/anagram/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules

21 changes: 21 additions & 0 deletions live-assignments/anagram/answer/app/css/global.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
body {
font-family: 'Arial', 'Tahoma', 'sans-serif';
}
#form {
width: 330px;
text-align: right;
margin-bottom: 4px;
}
label {
display: block;
width: 330px;
margin-bottom: 2px;
}
#output {
border: 1px solid #999;
display: block;
width: 322px;
height: 25px;
padding: 4px;
font-size: 11pt;
}
21 changes: 21 additions & 0 deletions live-assignments/anagram/answer/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en_GB">
<head>
<link rel="stylesheet" type="text/css" href="/css/global.css" />
<script type="text/javascript" src="/js/anagram.js"></script>
<script type="text/javascript" src="/js/app.js"></script>
<script type="text/javascript">
window.addEventListener("load", function() {
new App();
});
</script>
</head>
<body>
<div id="form">
<label>First Word: <input type="text" id="word1"/></label>
<label>Second Word: <input type="text" id="word2"/></label>
<button id="submit">Check Anagrams</button>
</div>
<section id="output"></section>
</body>
</html>
17 changes: 17 additions & 0 deletions live-assignments/anagram/answer/app/js/anagram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function AnagramChecker () { }

AnagramChecker.prototype.checkAnagrams = function(word1, word2) {
if (word1 == null || word2 == null) {
return false;
}
return this.prepareWord(word1) === this.prepareWord(word2);
};

AnagramChecker.prototype.prepareWord = function(word) {
return word.split('').sort().join('');
};

if (module) {
// this exists in the Node/Jest environment but not in the browser.
module.exports = AnagramChecker;
}
27 changes: 27 additions & 0 deletions live-assignments/anagram/answer/app/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function App() {
this.anagramChecker = new AnagramChecker();

var submitControl = document.querySelector('#submit');
var self = this;
submitControl.onclick = function() { self.checkForAnagrams() };

console.debug("Application initialised.");
}

App.prototype.setOutput = function(text) {
document.querySelector("#output").innerText = text;
};

App.prototype.checkForAnagrams = function() {
var firstWord = document.getElementById("word1").value;
var secondWord = document.getElementById("word2").value;
if (firstWord == "" || secondWord == "") {
this.setOutput("");
} else {
if (this.anagramChecker.checkAnagrams(firstWord, secondWord)) {
this.setOutput("These words are anagrams of each other.");
} else {
this.setOutput("These words are not anagrams of each other.");
}
}
};
Loading

0 comments on commit a671c78

Please sign in to comment.