# Problem solving for beginner # kata 8 in codewars **Problem -1-** You need to double the integer and return it. `function doubleInteger(i) { return 2*i; } ` **problem-2-** Convert number to reversed array of digits for example "35231 => [1,3,2,5,3]" ``` function digitize(n) { return n.toString().split('').reverse().map(Number); } ``` **problem-3-** 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: for example => ["hay", "junk", "hay", "hay", "moreJunk", "needle", "randomJunk"] --> "found the needle at position 5" ``` function findNeedle(haystack) { return "found the needle at position " + haystack.indexOf("needle"); } ``` **problem-4-** Given an array of integers. Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. 0 is neither positive nor negative. If the input is an empty array or is null, return an empty array. Example For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65]. ``` function countPositivesSumNegatives(input) { let positiveNums = 0; let negativeNums = 0; if (input === null || input.length === 0) { return []; } else { input.forEach((num) => num > 0 ? positiveNums++ : negativeNums += num); } return [positiveNums , negativeNums]; } ``` another solution with best practice \ ``` function countPositivesSumNegatives(input) { if (!Array.isArray(input) || !input.length) return []; return input.reduce((arr, n) => { if (n > 0) arr[0]++; if (n < 0) arr[1] += n; return arr; }, [0, 0]); } ``` **problem-5-** Your task is to make two functions ( max and min, or maximum and minimum, etc., depending on the language ) that receive a list of integers as input, and return the largest and lowest number in that list, respectively. example : * [4,6,2,1,9,63,-134,566] -> max = 566, min = -134 * [-52, 56, 30, 29, -54, 0, -110] -> min = -110, max = 56 * [42, 54, 65, 87, 0] -> min = 0, max = 87 * [5] -> min = 5, max = 5 ``` var min = (list)=>{ minNum =Math.min(...list); return minNum; } var max = (list)=>{ maxNum =Math.max(...list); return maxNum; } ``` **problem-6-** 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. ``` function squareSum(numbers) { return numbers.reduce((sum, num) => sum + num ** 2, 0); } ``` **problem-7-** Write a function that takes an array of words and smashes them together into a sentence and returns the sentence. You can ignore any need to sanitize words or add punctuation, but you should add spaces between each word. Be careful, there shouldn't be a space at the beginning or the end of the sentence! for example : ['hello', 'world', 'this', 'is', 'great'] => 'hello world this is great' ``` function smash (words) { return words.join(' '); }; ``` **problem-8-** Given a non-empty array of integers, return the result of multiplying the values together in order. Example: [1, 2, 3, 4] => 1 * 2 * 3 * 4 = 24 ``` function grow(x){ let multi=1; for(let i=0;i<x.length;i++){ multi*=x[i]; } return multi } ///another solution const grow = (nums) => nums.reduce((product, num) => product * num, 1); ``` **problem-9-** We need a function that can transform a string into a number. What ways of achieving this do you know? Note: Don't worry, all inputs will be strings, and every string is a perfectly valid representation of an integral number. ``` const stringToNumber = function(str){ let res = Number(str); return res; } ``` **problem-10-** Write a function which converts the input string to uppercase. ``` function makeUpperCase(str) { return str.toUpperCase(); } ``` # KATA-7- **problem-1-** The first century spans from the year 1 up to and including the year 100, the second century - from the year 101 up to and including the year 200, etc. ``` function century(year) { year=Math.ceil(eval(year/100)) return year; } ``` **problem-2-** Given an integral number, determine if it's a square number: In mathematics, a square number or perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. The tests will always use some integral number, so don't worry about that in dynamic typed languages. Examples -1 => false 0 => true 3 => false 4 => true 25 => true 26 => false ``` var isSquare = function(n){ return Math.sqrt(n) % 1 === 0; } //or by : const isSquare = n => Number.isInteger(Math.sqrt(n)); ``` **problem-3-** You get an array of numbers, return the sum of all of the positives ones. Example [1,-4,7,12] => 1 + 7 + 12 = 20 Note: if there is nothing to sum, the sum is default to 0. ``` function positiveSum(arr) { let sum=0; for(let i=0;i<arr.length;i++){ if(arr[i]>0){ sum+=arr[i]; } } return sum; // or by another best solution and advanced return arr.reduce((a,b)=> a + (b > 0 ? b : 0),0); } ``` **problem-4-** Given an array of ones and zeroes, convert the equivalent binary value to an integer. Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1. Examples: Testing: [0, 0, 0, 1] ==> 1 Testing: [0, 0, 1, 0] ==> 2 Testing: [0, 1, 0, 1] ==> 5 Testing: [1, 0, 0, 1] ==> 9 Testing: [0, 0, 1, 0] ==> 2 Testing: [0, 1, 1, 0] ==> 6 Testing: [1, 1, 1, 1] ==> 15 Testing: [1, 0, 1, 1] ==> 11 ``` return parseInt(arr.join(""), 2); ``` **problem-5-** Write a function to convert a name into initials. This kata strictly takes two words with one space in between them. The output should be two capital letters with a dot separating them. It should look like this: Sam Harris => S.H patrick feeney => P.F ``` function abbrevName(name){ const newArray = name.split(" ") return (newArray[0][0] + "." + newArray[1][0]).toUpperCase() } ``` **problem-6-** Given a list of integers, determine whether the sum of its elements is odd or even. Give your answer as a string matching "odd" or "even". If the input array is empty consider it as: [0] (array with a zero). Examples: Input: [0] Output: "even" Input: [0, 1, 4] Output: "odd" Input: [0, -1, -5] Output: "even" ``` function oddOrEven(arr) { return arr.reduce((a,b)=>a+b,0) % 2 ? 'odd' : 'even'; } ``` **problem-7-** We need a function that can transform a number (integer) into a string. What ways of achieving this do you know? Examples (input --> output): 123 --> "123" 999 --> "999" -100 --> "-100" ``` function numberToString(num) { return num.toString(); //another solution - return String(num); - return ''+num; } ``` **problem-8-** In this kata you will create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out. Example filter_list([1,2,'a','b']) == [1,2] filter_list([1,'a','b',0,15]) == [1,0,15] filter_list([1,2,'aasf','1','123',123]) == [1,2,123] ``` function filter_list(l) { return l.filter(e => Number.isInteger(e)); } ``` **problem-9-** Implement a function which convert the given boolean value into its string representation. Note: Only valid inputs will be given. ``` function booleanToString(b){ return b.toString(); or return String(b); or return b?"true":"false"; } ``` **problem-10-** In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative? Examples makeNegative(1); // return -1 makeNegative(-5); // return -5 makeNegative(0); // return 0 makeNegative(0.12); // return -0.12 ``` function makeNegative(num) { if (num > 0){ return num * (-1) }else if( num < 0){ return num } else{ return 0 } } //or by another best solution : function makeNegative(num) { return -Math.abs(num); } ``` **problem-11-** Complete the solution so that it reverses the string passed into it. ``` const reverseString = str => [...str].reverse().join(''); ``` **problem-12-** ``` function openOrSenior(data){ function determineMembership(member){ return (member[0] >= 55 && member[1] > 7) ? 'Senior' : 'Open'; } return data.map(determineMembership); } ``` **problem-13-** Your task is to create a function that does four basic mathematical operations. The function should take three arguments - operation(string/char), value1(number), value2(number). The function should return result of numbers after applying the chosen operation. ``` function basicOp(operation, value1, value2) { let result = 0 switch(operation) { case '+': result = value1 + value2 break case '-': result = value1 - value2 break case '*': result = value1 * value2 break case '/': result = value1 / value2 break default: return "not a valid operator" break } return result } ``` **problem-14-** Your task is to convert strings to how they would be written by Jaden Smith. The strings are actual quotes from Jaden Smith, but they are not capitalized in the same way he originally typed them. ``` String.prototype.toJadenCase = function () { let returnString = []; let words = this.toLowerCase().split(' '); for (let i = 0; i < words.length; i++) { word = words[i]; returnString.push(word[0].toUpperCase() + word.slice(1)); } return returnString.join(" "); }; //or by another solution : String.prototype.toJadenCase = function () { return this.split(" ").map(function(word){ return word.charAt(0).toUpperCase() + word.slice(1); }).join(" "); } ```