/    Sign up×
Community /Pin to ProfileBookmark

using objects for lookups

// Setup
function phoneticLookup(val) {
var result = “”;

// Only change code below this line
switch(val) {
case “alpha”:
result = “Adams”;
break;
case “bravo”:
result = “Boston”;
break;
case “charlie”:
result = “Chicago”;
break;
case “delta”:
result = “Denver”;
break;
case “echo”:
result = “Easy”;
break;
case “foxtrot”:
result = “Frank”;
}

// Only change code above this line
return result;
}

// Change this value to test
phoneticLookup(“charlie”);

[B]Convert the switch statement into a lookup table called lookup. Use it to lookup val and assign the associated string to the result variable.[/B]

I got this:

// Setup
function phoneticLookup(val) {
var result = “”;

// Only change code below this line
val ={
“alpha”:”Adam”,
“bravo”:”Boston”,
“charlie”:”Chicago”,
“delta”:”Denver”,
“echo”:”Easy”,
“foxtrot”:”Frank”
};

// Only change code above this line
return result;
}

// Change this value to test
phoneticLookup(“charlie”);

[B]How do I assign what string to the result variable? [/B]

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJul 14.2016 — IMO it has to read like this:
[CODE]// Setup
function phoneticLookup(val) {
var result = "";

// Only change code below this line
data ={
"alpha":"Adam",
"bravo":"Boston",
"charlie":"Chicago",
"delta":"Denver",
"echo":"Easy",
"foxtrot":"Frank"
};
return data[val];
}[/CODE]
This es a very neat approach as it enables for getting the result in one line of code.
Copy linkTweet thisAlerts:
@TrainJul 14.2016 — To make things easier reading use the BB code

http://www.webdeveloper.com/forum/misc.php?do=bbcode
Copy linkTweet thisAlerts:
@WebDevN00bauthorJul 14.2016 — IMO it has to read like this:
[CODE]// Setup
function phoneticLookup(val) {
var result = "";

// Only change code below this line
data ={
"alpha":"Adam",
"bravo":"Boston",
"charlie":"Chicago",
"delta":"Denver",
"echo":"Easy",
"foxtrot":"Frank"
};
return data[val];
}[/CODE]
This es a very neat approach as it enables for getting the result in one line of code.[/QUOTE]

Oh no, it's coz I'm doing a course so I'd like to know how result can be formed??? pls following instructions ?
Copy linkTweet thisAlerts:
@SempervivumJul 14.2016 — The only place where I didn't follow the instructions is this:
[CODE] return data[val];[/CODE]
Can be easily modified:
[CODE]var result = data[val];
// Only change code above this line
return result;
}[/CODE]
Copy linkTweet thisAlerts:
@WebDevN00bauthorJul 14.2016 — can moderation pls lifted it's kinda annoyign
Copy linkTweet thisAlerts:
@WebDevN00bauthorJul 14.2016 — The only place where I didn't follow the instructions is this:
[CODE] return data[val];[/CODE]
Can be easily modified:
[CODE]var result = data[val];
// Only change code above this line
return result;
}[/CODE]
[/QUOTE]

It still doesn't work. phoneticLookup("alpha") should equal "Adams"

[B]phoneticLookup("bravo") should equal "Boston"

phoneticLookup("charlie") should equal "Chicago"

phoneticLookup("delta") should equal "Denver"

phoneticLookup("echo") should equal "Easy"

phoneticLookup("foxtrot") should equal "Frank"

phoneticLookup("") should equal undefined

You should not use case, switch, or if statements [/B]


Of the bold, Only the last statement is true.
Copy linkTweet thisAlerts:
@SempervivumJul 14.2016 — Tested it and it works fine for me. If it does not for you you
[LIST]
  • [*]either didn't take over my solution correctly

  • [*]or do not test correctly

  • [/LIST]

    Note that I changed the name of the object. Did you take over this?
    Copy linkTweet thisAlerts:
    @SempervivumJul 14.2016 — PS: A "var" is obsolete. Should read like this:
    [CODE] result = data[val];
    // Only change code above this line
    return result;
    }[/CODE]
    Copy linkTweet thisAlerts:
    @WebDevN00bauthorJul 14.2016 — It still doesn't work. phoneticLookup("alpha") should equal "Adams"

    [B]phoneticLookup("bravo") should equal "Boston"

    phoneticLookup("charlie") should equal "Chicago"

    phoneticLookup("delta") should equal "Denver"

    phoneticLookup("echo") should equal "Easy"

    phoneticLookup("foxtrot") should equal "Frank"

    phoneticLookup("") should equal undefined

    You should not use case, switch, or if statements [/B]


    Of the bold, Only the last statement is true.[/QUOTE]

    // Setup

    function phoneticLookup(val) {

    var result = "";

    // Only change code below this line

    val = {

    "alpha":"Adams",

    "bravo":"Boston",

    "charlie":"Chicago",

    "delta":"Denver",

    "echo":"Easy",

    "foxtrot":"Frank"

    };

    result=data[val];

    // Only change code above this line

    return result;

    }

    // Change this value to test

    phoneticLookup("charlie");
    Copy linkTweet thisAlerts:
    @WebDevN00bauthorJul 14.2016 — that's what i did which didnt work
    Copy linkTweet thisAlerts:
    @daveyerwinJul 15.2016 — <script type="text/javascript">

    function phoneticLookup(val) {

    var result = "";

    lookup = {

    "alpha":"Adams",

    "bravo":"Boston",

    "charlie":"Chicago",

    "delta":"Denver",

    "echo":"Easy",

    "foxtrot":"Frank"

    };

    result = lookup[val]

    return result;

    }

    alert( phoneticLookup("charlie"));

    </script>
    Copy linkTweet thisAlerts:
    @WebDevN00bauthorJul 16.2016 — <script type="text/javascript">

    function phoneticLookup(val) {

    var result = "";

    lookup = {

    "alpha":"Adams",

    "bravo":"Boston",

    "charlie":"Chicago",

    "delta":"Denver",

    "echo":"Easy",

    "foxtrot":"Frank"

    };

    result = lookup[val]

    return result;

    }

    alert( phoneticLookup("charlie"));

    </script>[/QUOTE]

    got it thanks
    ×

    Success!

    Help @WebDevN00b spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...