Link to home
Start Free TrialLog in
Avatar of juan field
juan field

asked on

how can i get the sole odd and th sole even

// I used the this website to de - obfuscate. http://www.rot13.com/
/*
Problem description (see instructions for how to de-obfuscate).

Tvira n fgevat bs rira naq bqq ahzoref, svaq juvpu vf gur fbyr rira ahzore be gur fbyr bqq ahzore.
 
Gur erghea inyhr fubhyq or 1-vaqrkrq, abg 0-vaqrkrq.

Rknzcyrf :
qrgrpgBhgyvreInyhr("2 4 7 8 10"); // => 3 - Guveq ahzore vf bqq, juvyr gur erfg bs gur ahzoref ner rira
qrgrpgBhgyvreInyhr("1 10 1 1");  //=> 2 - Frpbaq ahzore vf rira, juvyr gur erfg bs gur ahzoref ner bqq
*/

///////// de- obfuscated /////////

// 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
function detectOutlierValue(str){
   var res = "";
   for(var i = 0; i < str.length; i++)
   {
   if(str.indexOf(i) % 2 !== 0)
   res += str.indexOf(i);
   }
   return res;
}


detectOutlierValue("2 4 7 8 10");
//detectOutlierValue("1 10 1 1");
ASKER CERTIFIED SOLUTION
Avatar of Ishaan Rawat
Ishaan Rawat
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Leonidas Dosas
Example only Javascript code .First I created a textarea element:
HTML
<textarea name="" id="textarea_1" cols="30" rows="10"></textarea>

Open in new window


Javascript

var textArea=document.getElementById('textarea_1');
textArea.addEventListener('keyup', function(){
var strText=textArea.value;
var oddNum=[];
var evenNum=[];
for(var i=1;i<=strText.length;i++){
  var number=parseInt(strText.charAt(i-1));
  if(number%2===0 && !isNaN(number)){
    evenNum.push(number);
  }else if(number%2!==0 && !isNaN(number)){
    oddNum.push(number);
  }else{
    continue;
  }
console.log(oddNum);
console.log(evenNum);
}
});

Open in new window


As you can see the function addEventListener creates two arrays. One with the odd numbers and the other with the even.So you can manipulate these arrays as you want.