Clear Filters
Clear Filters

I am having a problem in solving a coursera assignment can you please help me

2 views (last 30 days)
The problem is as such: variable input function
Function called should take two arguments age and year,The function must return true if age is less than the limit. If limit is not mentioned it must have default as 21.Output argument name is too_young.
I have wrote a code can you please verify it,
function too_young=under_age(age,limit)
if nargin<2
limit=18;
end
if limit<18
too_young=true;
else
too_young=false;
end
Function call is as such:
too_young = under_age(18,18)
too_young = under_age(20)
I got the errors as such:
correct for 18 vs 18
Assessment result: incorrect20
Variable too_young has an incorrect value.
Assessment result: correct22
Assessment result: incorrect17 vs 18
Variable too_young has an incorrect value.
Assessment result: incorrect13 vs 12
Variable too_young has an incorrect value.
Assessment result: incorrectRandom tests
Variable too_young has an incorrect value.
under_age(21, 23) failed ...

Accepted Answer

Geoff Hayes
Geoff Hayes on 24 Apr 2020
Nikhil - Iook closely at this code
if limit<18
too_young=true;
else
too_young=false;
end
Why isn't age being used? What happens if the user has passed in a valid limit and now you are ignoring it by using 18? Try using the MATLAB debugger to step through the code and understand why the condition for your if statement is incorrect.

More Answers (1)

HARSHA VARDHAN
HARSHA VARDHAN on 10 Dec 2020
function too_young=under_age(age,limit)
if nargin<2
limit=21;
end
if age<limit
too_young=true;
elseif age>=limit
too_young=false;
end

Categories

Find more on Platform and License in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!