/** Returns square of input param x */ function squareNumber(x) { return x * x; } //Try It println('Square of 3: ' + squareNumber(3)); println('Square of (-5): ' + squareNumber(-5));
/** Returns true if param x is odd */ function isOdd(x) { return x % 2 == 1; } //Try It println('Is 17 Odd: ' + isOdd(17)); println('Is 18 Odd: ' + isOdd(18));
//Outputs the triple of input x let tripleNumFct = (x) => 3 * x //Try It let x = 10; let y = tripleNumFct(x); println(y);
// Assigns function to var myFct */ let myFct = (x) => x*x; //Try It let result = myFct(3); println('Square of 3: ' + result);
let getFirstFct = (array) => array.get(0); let array = ['A++', 'A', 'A-']; let result = getFirstFct(array); println('Result is ' + result);
// Returns max of three numbers let maxFct = (a, b, c) => { let max = (a >= b) ? a : b; return (max >= c) ? max : c; } //Try It let a = 2; let b = 11; let c = 3; println(maxFct(a, b, c));
//Prints a list/array let fct = (list) => { for (let nextElem of list) { println(nextElem); } } //Try It let names = ['Asha', 'Chin', 'Kofi', 'Chin']; fct(names);
//Sums the numbers in a list/array let fct = (list) => { let sum = 0; for (let nextElem of list) { sum += nextElem; } return sum; } //Try It let numbers = [10, 20, 30, 40]; let sum = fct(numbers); println("Sum of list is: " + sum);
//Function that returns true if num is even let isEvenFct = (num) => num % 2 == 0; //Counts the even numbers in a list/array let fct = (list) => { let count = 0; for (let nextElem of list) { if (isEvenFct(nextElem)) count++; } return count; } //Try It let numbers = [7, 8, 9, 10]; let result = fct(numbers); println("Even Count: " + result);
// Returns factorial of param num let factorialFct = (num) => { let result = 1; let x = num; for (let x = num; x > 1; x--) result *= x; return result; } //Try It println('Factorial of 2: ' + factorialFct(2)); println('Factorial of 3: ' + factorialFct(3)); println('Factorial of 4: ' + factorialFct(4));
// Returns index of first match (of searchValue in list) let findFirstIndex = (list, searchValue) => { for (let index = 0; index < list.size(); index++) { let nextElem = list.get(index); if (nextElem.equals(searchValue)) return index; } return -1; } //Try It let names = ['Asha', 'Chin', 'Kofi', 'Chin']; let nm = 'Chin'; let firstIndex = findFirstIndex(names, nm); println(firstIndex);
let isPrime = (num) => { if (num == 1) return false; if (num == 2) return true; if (num % 2 == 0) return false; //3, 5, 7 if (num <= 7) return true; for (let f = 3; f <= 7; f += 2) if (num % f == 0) return false; let max = Math.floor(Math.sqrt(num)); //From 5, prime factors are 6k+/-1 let z = 6; max++; //to include 6k-1let isPrime = (num) => { while (z <= max) { if ((num % (z-1) == 0) || (num % (z+1) == 0)) return false; z += 6; } return true; } //Try It println("is Prime (37): " + isPrime(37)); //test, expect 25 between 1-100 let count = 1; //count "2" for (let n = 3; n < 100; n += 2) if (isPrime(n)) count++; println("Prime count (1-100): " + count); //test, expect 168 between 1-1000 count = 1; //count "2" for (let n = 3; n < 1000; n += 2) if (isPrime(n)) count++; println("Prime count (1-1000): " + count); println("\nList primes 1-100"); for (let n = 3; n < 100; n += 2) if (isPrime(n)) println("Prime: " + n);
// Runs function immediately */ let result = ((x) => x*x)(4); println('Square of 4: ' + result);