Last active
March 26, 2018 22:44
-
-
Save michaelmiller2116/25c03869bbf1bea7ba918b07eca1730b to your computer and use it in GitHub Desktop.
Just a great list of solutions that I wish I had thought of.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given two integers a and b, which can be positive or negative, find the sum of all the numbers between including them too and return it. If the two numbers are equal return a or b. | |
Note: a and b are not ordered! | |
Examples | |
GetSum(1, 0) == 1 // 1 + 0 = 1 | |
GetSum(1, 2) == 3 // 1 + 2 = 3 | |
GetSum(0, 1) == 1 // 0 + 1 = 1 | |
GetSum(1, 1) == 1 // 1 Since both are same | |
GetSum(-1, 0) == -1 // -1 + 0 = -1 | |
GetSum(-1, 2) == 2 // -1 + 0 + 1 + 2 = 2 | |
const GetSum = (a, b) => { | |
let min = Math.min(a, b), | |
max = Math.max(a, b); | |
return (max - min + 1) * (min + max) / 2; | |
} | |
********************************************************************** | |
REALLY COOL WAY TO AVOID IF ELSE (BUT NOT IF ELSE IF...) | |
// JS Nuggets: Ternary Operator LINK: https://www.youtube.com/watch?v=s4sB1hm73tw | |
// condition ? expr1 : expr2 | |
var age = 15; | |
if (age >= 18) { | |
console.log("You are an adult!"); | |
} else { | |
console.log("You are a kid"); | |
}; | |
console.log((age >= 18) ? "You are an adult!" : "You are a kid."); | |
var stop; | |
age > 18 ? ( | |
console.log("OK, you can go."), | |
stop = false | |
) : ( | |
console.log("Sorry, you are much too young!"), | |
stop = true | |
); | |
var firstCheck = false, | |
secondCheck = false, | |
access = firstCheck ? "Access denied" : secondCheck ? "Access denied" : "Access granted"; | |
console.log(access); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment