Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

firstLast6

Hi,
I was working on below challenge to find 6 at first and last position of array.

http://codingbat.com/prob/p185685

public boolean firstLast6(int[] nums) {



if(nums.contains(6))
{
return true;
}

else
{
return false;
}
  
}

Open in new window


i put some  code. Not exactly sure how to go about it. please advise
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

public boolean firstLast6(int[] nums) {

int firstInt=nums[0];
int numsLength=nums.length;
int lastInt=nums[numsLength-1];

if(firstInt==6)
{
return true;
}

if(lastInt==6)
{
return true;
}

else
{
return false;
}
  
}

Open in new window


i made changes and passed the tests. i wonder how i can improve my code. Please advise
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America 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 gudii9

ASKER

public boolean firstLast6(int[] nums) {
  return nums[0]==6 || nums[nums.length-1]==6;
}

Open in new window


above passed all tests too in just one single simple line. couple  of questions here
1. What are temporary variables are there any permenant variables too if yes which to use when
2. How the false scenario taken care automatically how it passed all the tests( returning true is somewhat fine but returning false scenario not clear)

please advise
there aren't really permanent variables, but some variables can be more temporary than others
http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

nums[0]==6 and nums[nums.length-1]==6 are either true or false.
SOLUTION
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