Skip to content

Commit

Permalink
Merge pull request YearOfProgramming#160 from Zirhc86/master
Browse files Browse the repository at this point in the history
challenges 1 & 2 in javascript
  • Loading branch information
Remillardj committed Jan 4, 2017
2 parents 0448e14 + db3f7d0 commit d58966e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
19 changes: 19 additions & 0 deletions challenge_1/javascript/Zirhc86/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 2017 Year of Programming
## Challenge_1
### Reverse a String
#### Synopsis
reverses a given string

#### Instructions
Open index.html in any browser and open developer console.
You can run `test()` with no arguments to log *Example string!* reversed, or pass
a string to log it reversed. ie:
```javascript
test('Reverse this');
```


### Contact
Chris -- zirhc86@gmail.com

https://github.com/Zirhc86
11 changes: 11 additions & 0 deletions challenge_1/javascript/Zirhc86/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Challenge_1|YoP 2017</title>
</head>

<body>
<script src="./stringReverse.js"></script>
</body>
</html>
25 changes: 25 additions & 0 deletions challenge_1/javascript/Zirhc86/src/stringReverse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

console.log('Running test() with no argument will log \'Example string!\' reversed.');

/*
********************************************************************
************************CHALLENGE ANSWER ***************************
********************************************************************/
function reverseString(stringToReverse) {
return stringToReverse.split('').reverse().join('');
}

/********************************************************************
*********************************************************************
*********************************************************************/


// Test function. Logs default string if no string is passed when called
function test(testString){
if(testString){
console.log(reverseString(testString));
}
else{
console.log(reverseString('Example string!'));
}
}
19 changes: 19 additions & 0 deletions challenge_2/javascript/Zirhc86/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 2017 Year of Programming
## Challenge_2
### Single Number
#### Synopsis
Searches through array for characters that only appear once, then logs them to
console.

#### Instructions
Open index.html in any browser and open developer console.
Two preset arrays will output their single characters.
You can run `test()` with your own defined array. ie:
```javascript
test([3, 2, 'z', 2, 6, 'i']);
```

### Contact
Chris -- zirhc86@gmail.com

https://github.com/Zirhc86
9 changes: 9 additions & 0 deletions challenge_2/javascript/Zirhc86/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html lang="en"><head>
<meta charset="utf-8">
<title>Challenge_2|YoP 2017</title>
</head>

<body>
<script src="./singleNumber.js"></script>

</body></html>
31 changes: 31 additions & 0 deletions challenge_2/javascript/Zirhc86/src/singleNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var array = [2, 3, 4, 2, 3, 5, 4, 6, 4, 6, 9, 10, 9, 8, 7, 8, 10, 7];
var mixedArray =
[2,'a','l',3,'l',4,'k',2,3,4,5,'a',6,'c',4,'m',6,'m','k',9,10,9,8,7,8,10,7];

// Passes array values as keys into 'acc' object and increments by 1 each time
// a value is encountered
function getTally(arr){
return arr.reduce(function(acc, curr){
acc[curr] ? acc[curr]++ : acc[curr] = 1;
return acc;
}, {});
}

// Filter out any keys in object that has a value of 1, and add to a string,
// separating chars with ', ' for each new addition
function logSingles(obj){
var singles = '';
for (var key in obj){
if(obj[key] === 1){
singles.length >=1 ? singles += ', ' + key : singles = key;
}
}
return singles;
}

function test(arr){
console.log(logSingles(getTally(arr)));
}

test(array);
test(mixedArray);

0 comments on commit d58966e

Please sign in to comment.