id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

gaurav.gupta@mq.edu.au arrays > distanceFromHome
prev  |  next  |  chance

In a legacy system, directions are stored by magnitude followed by direction. Thus, "12E" represents " travel 12 kms towards east, "4N" represents 4kms to the north, "120S" represents 120 kms south and finally "3W" represents 3 kms towards west.

Given an array that holds a collection of individiaul directions, return how far from the starting point will we be.

You can use Integer.parseInt(<String>) to convert a String to int (provided it can be converted).

You will also need String functions such as charAt and substring.


distanceFromHome(["10N", "10W", "10S", "10E"]) → 0.0
distanceFromHome(["10N", "10W", "10S", "8E"]) → 2.0
distanceFromHome(["8N", "6W"]) → 10.0

...Save, Compile, Run (ctrl-enter)

double distanceFromHome(String[] directions) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 4 Post-solution available

Copyright Nick Parlante 2017 - privacy