#Day95 of #100DaysOfCode
Another tiring day but full of networking and meeting amazing folks. I had the bestest day of my life…
I met leaders of Dublin and London Java groups and I look forward to collaborating with them for our mobile talks… 
Another session of codewars
Problem Description
Complete the square sum function so that it squares each number passed into it and then sums the results together.
For example, for [1, 2, 2] it should return 9 because 1^2 + 2^2 + 2^2 = 9
Solution
function squareSum(numbers){
let array = numbers.map(number => number * number)
return array.reduce((one, two) => one + two, 0)
}
Sample Tests
describe("Tests", () => {
it("test", () => {
Test.assertEquals(squareSum([1,2]), 5)
Test.assertEquals(squareSum([0, 3, 4, 5]), 50)
Test.assertEquals(squareSum([]), 0)
});
});
Problem Description
Can you find the needle in the haystack?
Write a function findNeedle() that takes an array full of junk but containing one "needle"
After your function finds the needle it should return a message (as a string) that says:
"found the needle at position " plus the index it found the needle, so:
findNeedle(['hay', 'junk', 'hay', 'hay', 'moreJunk', 'needle', 'randomJunk'])
Solution
function findNeedle(haystack) {
// your code here
let index = haystack.indexOf("needle");
return "found the needle at position " + index;
}
Sample Tests
describe("Tests", () => {
it("test", () => {
var haystack_1 = ['3', '123124234', undefined, 'needle', 'world', 'hay', 2, '3', true, false];
var haystack_2 = ['283497238987234', 'a dog', 'a cat', 'some random junk', 'a piece of hay', 'needle', 'something somebody lost a while ago'];
var haystack_3 = [1,2,3,4,5,6,7,8,8,7,5,4,3,4,5,6,67,5,5,3,3,4,2,34,234,23,4,234,324,324,'needle',1,2,3,4,5,5,6,5,4,32,3,45,54];
Test.assertNotEquals(findNeedle(haystack_1), undefined, "Your function didn't return anything");
Test.assertEquals(findNeedle(haystack_1), 'found the needle at position 3')
Test.assertEquals(findNeedle(haystack_2), 'found the needle at position 5')
Test.assertEquals(findNeedle(haystack_3), 'found the needle at position 30')
});
});
Problem Description
Very simple, given an integer or a floating-point number, find its opposite.
Examples:
1: -1
14: -14
-34: 34
Solution
function opposite(number) {
//your code here
return number * -1
}
Sample Tests
describe("Tests", () => {
it("test", () => {
Test.assertEquals(opposite(1), -1,)
});
});
That is all for today…