From 663faae86960a6f730aacee0a1bd5779677ae016 Mon Sep 17 00:00:00 2001 From: Phil B Date: Tue, 4 Sep 2018 17:21:28 +0100 Subject: [PATCH] Kate and Phils completed operators --- src/operators.js | 62 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/src/operators.js b/src/operators.js index 341d9cb..031cb93 100644 --- a/src/operators.js +++ b/src/operators.js @@ -1,76 +1,98 @@ // Rewrite function as arrow function -function convertToNumber(string){ +const convertToNumber= (string) => +string; // function receives a string representation // of a number convert it to a number and return it -} + // Rewrite function as arrow function -function convertToNegativeNumber(string){ +const convertToNegativeNumber = (string) => +string > 0 ? -string:+string; // function receives a string representation // of a number // convert string to number and return a negative version of the number -} // Rewrite function as arrow function -function invertBoolean(input){ +const invertBoolean = (input) => typeof input === "boolean" ? !input : input; // function receives one argument which could be of any type // if it is a boolean negate it, true -> false, false to true // if the input is not a boolean return the input -} + // Rewrite function as arrow function -function convertToBoolean(list){ - // Function receives an array of values which could be of any type +const convertToBoolean = (list) => list.map(x=> !!x); + // Function receives an array of values which could be of any type // Convert all values in array to boolean and return the array -} + // Rewrite function as arrow function -function deleteStrings(list){ +const deleteStrings= (list) => list.map(item => typeof item === "string" ? undefined :item); // Function receives an array of strings and numbers // delete strings in array, (make them undefined) // return resulting array -} + // Rewrite function as arrow function -function deleteYear(cars){ +const deleteYear=(cars) => { + for (i=0;i (number >= from && number < to) ? true : false; // function receives three numbers as arguments // return true if number is equal to or greater than from and less than to // return false otherwise -} + // Rewrite function as arrow function -function evens(a, b, c, d){ +const evens = (a, b, c, d) => !(a%2 + b%2 + c%2 + d%2); // function receives 4 numbers return true if all numbers are even and false otherwise // do not use if/else or ternary -} + // Rewrite function as arrow function -function averageOfStrings(list){ +const reducer = (accumulator, currentValue) => accumulator + currentValue; + +const averageOfStrings = (list) => { + let strings = list.filter(item => typeof item === "string"); + let stringsToNumbers = strings.map(item => +item); + return stringsToNumbers.reduce(reducer)/stringsToNumbers.length; +} + // function receives an array of numbers and string representation of numbers // calculate an average value of numbers represented as strings and return it. -} + // Rewrite function as arrow function -function footballScores(results){ +const footballScores = (results)=> { + let winners=[]; + for (i=0;iresults[i].away) { + winners[i]="home"; + } else if (results[i].home