Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: TheOdinProject/javascript-exercises
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: braymuk/javascript-exercises
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Checking mergeability… Don’t worry, you can still create the pull request.
  • 7 commits
  • 14 files changed
  • 1 contributor

Commits on Nov 22, 2019

  1. finished repeatString.js

    braymuk committed Nov 22, 2019
    Copy the full SHA
    7db6fc5 View commit details

Commits on Nov 25, 2019

  1. Copy the full SHA
    4ae8fa2 View commit details
  2. Got through like 4 tests of removeFromArray with some shitty code but…

    … then hit a roadblock after that
    braymuk committed Nov 25, 2019
    Copy the full SHA
    c637372 View commit details

Commits on Nov 26, 2019

  1. Finished sumAll.js practice, took me way too long to figure out how t…

    …o check if a variable is a number
    braymuk committed Nov 26, 2019
    Copy the full SHA
    b3dd38d View commit details
  2. Finished leapYear.js practice

    braymuk committed Nov 26, 2019
    Copy the full SHA
    c57f5f1 View commit details
  3. Copy the full SHA
    432e6dd View commit details

Commits on Jan 11, 2020

  1. Finished Calculator.js

    braymuk committed Jan 11, 2020
    Copy the full SHA
    ace489e View commit details
45 changes: 38 additions & 7 deletions calculator/calculator.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,55 @@
function add () {

var sum = 0;
for(var i=0;i<arguments.length;i++){
sum+= arguments[i];
}
return sum;
}

function subtract () {

var value = arguments[0]-arguments[1];
return value;
}

function sum () {

}
function sum (arr) {
return arr.reduce(function(a,b){
return a + b
}, 0);
};


function multiply () {
function multiply (arr) {
var sum = arr[0];
for(i=1;i<arr.length;i++) {
sum *= arr[i];
}
return sum;

}

function power() {
var base = arguments[0];
var power = arguments[1];
var one = 1;
for(i=0;i<power;i++) {
one*= base;
}
return one;

}

function factorial() {
function factorial(num) {
var one = 1;
if (num===0) {
return 1;
} else {
var number = num;
for (i=1;i<num;i++) {
number *= num-i;
}
return number;

}

}

30 changes: 15 additions & 15 deletions calculator/calculator.spec.js
Original file line number Diff line number Diff line change
@@ -5,73 +5,73 @@ describe('add', function() {
expect(calculator.add(0,0)).toEqual(0);
});

xit('adds 2 and 2', function() {
it('adds 2 and 2', function() {
expect(calculator.add(2,2)).toEqual(4);
});

xit('adds positive numbers', function() {
it('adds positive numbers', function() {
expect(calculator.add(2,6)).toEqual(8);
});
});

describe('subtract', function() {
xit('subtracts numbers', function() {
it('subtracts numbers', function() {
expect(calculator.subtract(10,4)).toEqual(6);
});
});

describe('sum', function() {
xit('computes the sum of an empty array', function() {
it('computes the sum of an empty array', function() {
expect(calculator.sum([])).toEqual(0);
});

xit('computes the sum of an array of one number', function() {
it('computes the sum of an array of one number', function() {
expect(calculator.sum([7])).toEqual(7);
});

xit('computes the sum of an array of two numbers', function() {
it('computes the sum of an array of two numbers', function() {
expect(calculator.sum([7,11])).toEqual(18);
});

xit('computes the sum of an array of many numbers', function() {
it('computes the sum of an array of many numbers', function() {
expect(calculator.sum([1,3,5,7,9])).toEqual(25);
});
});

describe('multiply', function() {
xit('multiplies two numbers', function() {
it('multiplies two numbers', function() {
expect(calculator.multiply([2,4])).toEqual(8);
});

xit('multiplies several numbers', function() {
it('multiplies several numbers', function() {
expect(calculator.multiply([2,4,6,8,10,12,14])).toEqual(645120);
});
});

describe('power', function() {
xit('raises one number to the power of another number', function() {
it('raises one number to the power of another number', function() {
expect(calculator.power(4,3)).toEqual(64); // 4 to third power is 64
});
});

describe('factorial', function() {
xit('computes the factorial of 0', function() {
it('computes the factorial of 0', function() {
expect(calculator.factorial(0)).toEqual(1); // 0! = 1
});

xit('computes the factorial of 1', function() {
it('computes the factorial of 1', function() {
expect(calculator.factorial(1)).toEqual(1);
});

xit('computes the factorial of 2', function() {
it('computes the factorial of 2', function() {
expect(calculator.factorial(2)).toEqual(2);
});

xit('computes the factorial of 5', function() {
it('computes the factorial of 5', function() {
expect(calculator.factorial(5)).toEqual(120);
});

xit('computes the factorial of 10', function() {
it('computes the factorial of 10', function() {
expect(calculator.factorial(10)).toEqual(3628800);
});
});
9 changes: 8 additions & 1 deletion leapYears/leapYears.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const leapYears = function() {
const leapYears = function(year) {
if(year%4===0 && year%100!==0) {
return true

} else if(year%400===0) {
return true
} else {
return false
}
}

module.exports = leapYears
10 changes: 5 additions & 5 deletions leapYears/leapYears.spec.js
Original file line number Diff line number Diff line change
@@ -4,19 +4,19 @@ describe('leapYears', function() {
it('works with non century years', function() {
expect(leapYears(1996)).toEqual(true);
});
xit('works with non century years', function() {
it('works with non century years', function() {
expect(leapYears(1997)).toEqual(false);
});
xit('works with ridiculously futuristic non century years', function() {
it('works with ridiculously futuristic non century years', function() {
expect(leapYears(34992)).toEqual(true);
});
xit('works with century years', function() {
it('works with century years', function() {
expect(leapYears(1900)).toEqual(false);
});
xit('works with century years', function() {
it('works with century years', function() {
expect(leapYears(1600)).toEqual(true);
});
xit('works with century years', function() {
it('works with century years', function() {
expect(leapYears(700)).toEqual(false);
});
});
14 changes: 13 additions & 1 deletion removeFromArray/removeFromArray.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
const removeFromArray = function() {
const removeFromArray = function(array, pos1, pos2, pos3, pos4) {
let removed1 = array.splice(pos1-1,1);
if(pos2>0) {
let removed2 = array.splice(pos2-1,1);
}
if(pos3>0) {
let removed3 = array.splice(pos3-1,1);
}
if(pos4>0) {
let removed4 = array.splice(pos4-1,1);
}


return array;
}

module.exports = removeFromArray
8 changes: 4 additions & 4 deletions removeFromArray/removeFromArray.spec.js
Original file line number Diff line number Diff line change
@@ -4,16 +4,16 @@ describe('removeFromArray', function() {
it('removes a single value', function() {
expect(removeFromArray([1, 2, 3, 4], 3)).toEqual([1, 2, 4]);
});
xit('removes multiple values', function() {
it('removes multiple values', function() {
expect(removeFromArray([1, 2, 3, 4], 3, 2)).toEqual([1, 4]);
});
xit('ignores non present values', function() {
it('ignores non present values', function() {
expect(removeFromArray([1, 2, 3, 4], 7, "tacos")).toEqual([1, 2, 3, 4]);
});
xit('ignores non present values, but still works', function() {
it('ignores non present values, but still works', function() {
expect(removeFromArray([1, 2, 3, 4], 7, 2)).toEqual([1, 3, 4]);
});
xit('can remove all values', function() {
it('can remove all values', function() {
expect(removeFromArray([1, 2, 3, 4], 1, 2, 3, 4)).toEqual([]);
});
xit('works with strings', function() {
10 changes: 7 additions & 3 deletions repeatString/repeatString.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const repeatString = function() {

const repeatString = function(string,n) {
for(i=0;i<=n;i++) {
return string.repeat(n);
} if(n<0) {
return "ERROR";
}
}

module.exports = repeatString
module.exports = repeatString
8 changes: 4 additions & 4 deletions repeatString/repeatString.spec.js
Original file line number Diff line number Diff line change
@@ -4,16 +4,16 @@ describe('repeatString', function() {
it('repeats the string', function() {
expect(repeatString('hey', 3)).toEqual('heyheyhey');
});
xit('repeats the string many times', function() {
it('repeats the string many times', function() {
expect(repeatString('hey', 10)).toEqual('heyheyheyheyheyheyheyheyheyhey');
});
xit('repeats the string 1 times', function() {
it('repeats the string 1 times', function() {
expect(repeatString('hey', 1)).toEqual('hey');
});
xit('repeats the string 0 times', function() {
it('repeats the string 0 times', function() {
expect(repeatString('hey', 0)).toEqual('');
});
xit('returns ERROR with negative numbers', function() {
it('returns ERROR with negative numbers', function() {
expect(repeatString('hey', -1)).toEqual('ERROR');
});
});
8 changes: 7 additions & 1 deletion reverseString/reverseString.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const reverseString = function() {
const reverseString = function(string) {
let array = string.split("");
let rev = array.reverse();
console.log(rev)
let commas = rev.join("");
console.log(commas);
return commas;

}

4 changes: 2 additions & 2 deletions reverseString/reverseString.spec.js
Original file line number Diff line number Diff line change
@@ -5,11 +5,11 @@ describe('reverseString', function() {
expect(reverseString('hello')).toEqual('olleh');
});

xit('reverses multiple words', function() {
it('reverses multiple words', function() {
expect(reverseString('hello there')).toEqual('ereht olleh')
})

xit('works with numbers and punctuation', function() {
it('works with numbers and punctuation', function() {
expect(reverseString('123! abc!')).toEqual('!cba !321')
})
});
58 changes: 57 additions & 1 deletion sumAll/sumAll.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
const sumAll = function() {
// This can be brought into a simple one liner like this:
// return (typeof value === 'number') && value === Number(value) && Number.isFinite(value)

// We'll just use guard clauses here.
function isNumber (value) {
// We will not coerce boolean to numbers, although we could.
// We will not coerce strings to numbers, even though we could try.
// Referencing https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
if (typeof value !== 'number') {
return false
}

// Consider this as the NaN check.
// NaN is a number.
// NaN has the unique property of never equaling itself.
// Pulled this hack right off of MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN
if (value !== Number(value)) {
return false
}

// At this point, we for sure have some sort of number.
// But not all numbers are finite, and realistically we want finite numbers.
if (Number.isFinite(value) === false) {
return false
}

return true
}


const sumAll = function(start,finish) {

if (finish<0 || start<0 || start !== Number(start) || finish !== Number(finish)) {
return 'ERROR'
} else {
if (finish>start) {
let sum = start
let dif = Math.abs(finish-start+1);
for (i=1;i<dif;i++) {
let x = start+i
sum += x

}
return sum
}
if (start>finish) {
let sum = finish
let dif = Math.abs(start-finish+1);
for (i=1;i<dif;i++) {
let x = finish+i
sum += x

}
return sum
}
}


}

module.exports = sumAll
10 changes: 5 additions & 5 deletions sumAll/sumAll.spec.js
Original file line number Diff line number Diff line change
@@ -4,19 +4,19 @@ describe('sumAll', function() {
it('sums numbers within the range', function() {
expect(sumAll(1, 4)).toEqual(10);
});
xit('works with large numbers', function() {
it('works with large numbers', function() {
expect(sumAll(1, 4000)).toEqual(8002000);
});
xit('works with larger number first', function() {
it('works with larger number first', function() {
expect(sumAll(123, 1)).toEqual(7626);
});
xit('returns ERROR with negative numbers', function() {
it('returns ERROR with negative numbers', function() {
expect(sumAll(-10, 4)).toEqual('ERROR');
});
xit('returns ERROR with non-number parameters', function() {
it('returns ERROR with non-number parameters', function() {
expect(sumAll(10, "90")).toEqual('ERROR');
});
xit('returns ERROR with non-number parameters', function() {
it('returns ERROR with non-number parameters', function() {
expect(sumAll(10, [90, 1])).toEqual('ERROR');
});
});
10 changes: 6 additions & 4 deletions tempConversion/tempConversion.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const ftoc = function() {

const ftoc = function(temp) {
raw = (temp-32)*(5/9)
return Math.round(raw*10)/10;
}

const ctof = function() {

const ctof = function(temp) {
raw = (temp)*(9/5)+32
return Math.round(raw*10)/10;
}

module.exports = {
Loading