Skip to content

Commit 24687ef

Browse files
committed
some from 'medium' challenges
1 parent 014c9a3 commit 24687ef

7 files changed

Lines changed: 489 additions & 42 deletions

File tree

ArrayAdditionI.js

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,77 +11,55 @@ function ArrayAdditionI(arr) {
1111
var status = false;
1212
var sortedArray = arr.sort(function(a,b){return a-b});
1313
var largestIndex = parseInt(sortedArray.length) - 1;
14-
console.log('largestIndex: ' + largestIndex);
15-
1614
var findLargestNum = function(arrSorted){
1715
return arrSorted[largestIndex];
1816
};
19-
2017
var largestNum = findLargestNum(sortedArray);
21-
22-
23-
console.log(sortedArray);
24-
console.log(largestNum);
25-
26-
2718
var mathArray = sortedArray.slice(0);
2819
var count = 0;
29-
30-
3120
var getDiffs = function(arr){
3221
if(!status){
3322
arr.pop();
3423
arr.reverse();
35-
console.log(arr);
3624
if (count > 1){
3725
arr.pop();
3826
}
27+
var max = largestNum;
3928

29+
for (i = arr.length - 1 ; i > -1; i-- ){
30+
max -= arr[i];
31+
if (max == 0){
32+
status = true;
4033

41-
var max = largestNum;
42-
43-
for (i = arr.length - 1 ; i > -1; i-- ){
44-
console.log(max);
45-
max -= arr[i];
46-
if (max == 0){
47-
status = true;
48-
49-
} else if(max < 0) {
50-
max += arr[i];
51-
i--;
52-
}
53-
}
34+
} else if(max < 0) {
35+
max += arr[i];
36+
i--;
37+
}
38+
}
5439

5540
if (!status){
5641
count++;
57-
console.log('count: ' + count);
58-
console.log('max: ' + max);
5942
return max;
6043
} else {
61-
console.log('status: ' + status);
6244
return status;
6345
}
6446
}
6547
}
6648

67-
while (!status && count < sortedArray.length - 1){
68-
console.log('sortedArray: ' + sortedArray);
49+
while (!status && count < sortedArray.length - 1){
50+
mathArray = sortedArray.slice(0);
51+
getDiffs(mathArray);
52+
}
53+
for (var splice = 0; splice < sortedArray.length - 1; splice++){
54+
if (!status){
55+
count = 0;
6956
mathArray = sortedArray.slice(0);
57+
mathArray.splice(splice,1);
58+
7059
getDiffs(mathArray);
71-
}
72-
for (var splice = 0; splice < sortedArray.length - 1; splice++){
73-
if (!status){
74-
count = 0;
75-
console.log('sortedArray: ' + sortedArray);
76-
mathArray = sortedArray.slice(0);
77-
console.log("math array: " + mathArray);
78-
console.log('spliceIndex: ' + splice);
79-
mathArray.splice(splice,1);
80-
console.log('largestNum: ' + largestNum);
81-
getDiffs(mathArray);
8260

83-
}
8461
}
62+
}
8563

8664
var theDiffs = getDiffs(mathArray);
8765

BracketMatcher.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
Using the JavaScript language, have the function BracketMatcher(str) take the str parameter being
3+
passed and return 1 if the brackets are correctly matched and each one is accounted for. Otherwise
4+
return 0. For example: if str is "(hello (world))", then the output should be 1, but if str is
5+
"((hello (world))" the the output should be 0 because the brackets do not correctly match up.
6+
Only "(" and ")" will be used as brackets. If str contains no brackets return 1.
7+
*/

NumberSearch.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
3+
Using the JavaScript language, have the function NumberSearch(str) take the
4+
str parameter, search for all the numbers in the string, add them together,
5+
then return that final number. For example: if str is "88Hello 3World!" the
6+
output should be 91. You will have to differentiate between single digit
7+
numbers and multiple digit numbers like in the example above. So "55Hello"
8+
and "5Hello 5" should return two different answers. Each string will contain
9+
at least one letter or symbol.
10+
11+
12+
*/
13+
14+
function NumberAddition(str) {
15+
16+
var totalSum = 0;
17+
18+
var currentNum = 0;
19+
20+
for (var i = 0; i < str.length; i++) {
21+
22+
if (str[i].match(/[0-9]/)){
23+
24+
currentNum = parseInt(str[i]);
25+
//console.log("currentNum: " + currentNum);
26+
27+
28+
if (str[i+1] && str[i+1].match(/[0-9]/)) {
29+
remainingStr = str.substring(i+1);
30+
//console.log("remainingStr: " + remainingStr);
31+
32+
if (remainingStr[0].match(/[0-9]/)) {
33+
//console.log('matched');
34+
nextNum = parseInt(remainingStr[0]);
35+
currentNum = currentNum * 10 + nextNum;
36+
//console.log("currentNum: " + currentNum);
37+
totalSum += currentNum;
38+
//console.log("totalSum: " + totalSum);
39+
i++;
40+
41+
42+
}
43+
44+
} else {
45+
totalSum += currentNum;
46+
//console.log("totalSum: " + totalSum);
47+
}
48+
49+
}
50+
51+
}
52+
53+
return totalSum;
54+
55+
}
56+
57+
58+
59+
NumberAddition("75Number9"); //output = 84
60+
//NumberAddition("10 2One Number*1*"); // output = 13
61+
//NumberAddition();
62+
//NumberAddition();

StringReduction.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
3+
Using the JavaScript language, have the function StringReduction(str) take the str parameter
4+
being passed and return the smallest number you can get through the following reduction method.
5+
The method is: Only the letters a, b, and c will be given in str and you must take two different
6+
adjacent characters and replace it with the third. For example "ac" can be replaced with "b" but
7+
"aa" cannot be replaced with anything. This method is done repeatedly until the string cannot be
8+
further reduced, and the length of the resulting string is to be outputted. For example: if str
9+
is "cab", "ca" can be reduced to "b" and you get "bb" (you can also reduce it to "cc"). The
10+
reduction is done so the output should be 2. If str is "bcab", "bc" reduces to "a", so you have
11+
"aab", then "ab" reduces to "c", and the final string "ac" is reduced to "b" so the output should be 1.
12+
*/
13+
14+
function StringReduction(str) {
15+
var remainingString = str;
16+
var newString = "";
17+
var shortestStr = str.length;
18+
var nextString = "";
19+
var same = true;
20+
var newerString = "";
21+
var replaceLetters = function(a,b){
22+
debugger;
23+
//console.log("replacing: " + a + " and " + b);
24+
if ((a == "a" && b == "b") || (a == "b" && b == "a")){
25+
return "c";
26+
} else if ((a == "b" && b == "c")||(a == "c" && b == "b")) {
27+
return "a";
28+
} else if((a == "a" && b == "c")||(a == "c" && b == "a")) {
29+
return "b";
30+
} else {
31+
return a+b;
32+
}
33+
}
34+
var iterateStr = function(newStr, remainingStr){
35+
debugger;
36+
var wasDiff = false;
37+
if (remainingStr.length < 2){
38+
return 1;
39+
}
40+
for (i = 0; i < remainingStr.length; i++){
41+
//console.log("remainingStr: " + remainingStr);
42+
if (remainingStr.length < 2){
43+
newStr += remainingStr;
44+
var nextString = "";
45+
nextString += newStr;
46+
//console.log("nextString: " + nextString);
47+
// return iterateStr(newStr,nextString);
48+
} else {
49+
if (remainingStr.charAt(i) != remainingStr.charAt(i+1)){
50+
wasDiff = true;
51+
debugger;
52+
newStr += replaceLetters(remainingStr.charAt(i), remainingStr.charAt(i+1));
53+
i++;
54+
} else if (remainingStr.charAt(i) == remainingStr.charAt(i+1)){
55+
debugger;
56+
newStr += remainingStr.charAt(i);
57+
}
58+
}
59+
if (remainingStr.charAt(i+1) == undefined){
60+
//console.log("calling this!");
61+
newStr += remainingStr.charAt(i);
62+
if (!wasDiff) {
63+
return newStr.length;
64+
}
65+
}
66+
//console.log("newStr: " + newStr);
67+
}
68+
var nextString = "";
69+
nextString += newStr;
70+
//console.log("nextString: " + nextString);
71+
if (nextString == remainingStr){
72+
return nextString.length;
73+
} else if (nextString.length < 2){
74+
return 1;
75+
}
76+
var emptyStr = "";
77+
return iterateStr(emptyStr,nextString);
78+
}
79+
return iterateStr(newString, remainingString);
80+
}
81+
82+
//StringReduction("abcabc"); // output = 2;
83+
StringReduction("cccc"); // output = 4;
84+
//StringReduction("ababccbbcabab"); //

0 commit comments

Comments
 (0)