gudii9
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
i put some code. Not exactly sure how to go about it. please advise
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;
}
}
i put some code. Not exactly sure how to go about it. please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
public boolean firstLast6(int[] nums) {
return nums[0]==6 || nums[nums.length-1]==6;
}
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Open in new window
i made changes and passed the tests. i wonder how i can improve my code. Please advise