Last active
June 25, 2019 20:43
-
-
Save allizad/d0b9ce4303cf443c09dbbb55ba074080 to your computer and use it in GitHub Desktop.
psuedocode example from Suchita - original
This file contains hidden or 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 a string of even and odd numbers, find which is the sole even number or | |
the sole odd number. | |
The return value should be 1-indexed, not 0-indexed. | |
Examples : | |
detectOutlierValue("2 4 7 8 10"); // => 3 - Third number is odd, while the rest of the numbers are even | |
detectOutlierValue("1 10 1 1"); //=> 2 - Second number is even, while the rest of the numbers are odd*/ | |
/* input = string of numbers | |
output = number */ | |
/*pseudocode - | |
1. split() string of numbers into arrays | |
2. loop through the array and check if it is even or odd. | |
3. return the odd/even number | |
*/ | |
function evenOdd(str){ | |
//converting string to array | |
var arr = str.split(); | |
var res = arr[0]; | |
var even = false; | |
for(let i = 1; i < arr.length; i++){ | |
if(arr[i] % 2 === 0 && res % 2 === 0 ){ | |
even = true; | |
} | |
else if(arr[i] % 2 !== 0 && res %2 !== 0){ | |
even = false; | |
} | |
if(even === false && arr[i] %2 === 0) return i; | |
else if (even === true && arr[i] % 2 !== 0) return i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment