forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request YearOfProgramming#160 from Zirhc86/master
challenges 1 & 2 in javascript
- Loading branch information
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |