/    Sign up×
Community /Pin to ProfileBookmark

Help building a function for HTML

Hi Guys,

I know very little of functions or syntax so I Would appreciate any help in building a function for my HTML webpage.

Basically the function should receive one argument ({{CountryCode}}) and return a number (which is the country code ID predefined below)

Country code ID:

US = “2105”
UK = “1103”
GER = “704”
ESP = “1918”
AUS = “115”
CAN = “304”

Country code: (function argument)

US
UK
GER
ESP
AUS
CAN

The function should compare the Country code argument to a list of Country codes predefined and return the country code ID value.

To better explain myself for instance lets say that the argument received is “US”. the function should go over the list of country codes and return the value “2105”

Function header should look like this:

GetCountryID ({{CountryCode}}) {

}

Would appreciate your help.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@jedaisoulJun 12.2015 — Hi and welcome to the site. I've moved this thread to the JavaScript forum.

This looks like homework to me, so I'd remind other posters to, by all means, help as requested, but do not give the solution.
Copy linkTweet thisAlerts:
@TcobbJun 12.2015 — Look at generic javascript "objects," which are essentially name-value pairs. This would probably be the easiest route to go.
Copy linkTweet thisAlerts:
@orenpp1authorJun 13.2015 — Let me know if I've done well....

{{BuyerCountry}} and {{TrackingNumber}} is a variable from an outside source and works fine.

Outcome should reflect in HTML URL under "cc" (initials for Country Code):

[COLOR="#000000"]

http://www.MyWebSite?pt=1&cc=[/COLOR]
[COLOR="#FF0000"]GetCountryID()[/COLOR]&nums={{TrackingNumber}}

<!DOCTYPE html>

<html>

<body>

<p id="demo"></p>

<script>

var country = {US = "2105" UK = "1103" GER = "704" ESP = "1918" AUS = "115" CAN = "304"}

function GetCountryID(){

document.getElementById("demo").innerHTML = country.{{BuyerCountry}};

}

</script>

</body>
Copy linkTweet thisAlerts:
@TcobbJun 15.2015 — As you've probably noticed if you have tried to run that code
[CODE]var country = {US = "2105" UK = "1103" GER = "704" ESP = "1918" AUS = "115" CAN = "304"}[/CODE]

that's not a correct way to write a javascript object. Try:

[CODE]var country = {"US" : "2105", "UK": "1103", "GER": "704", "ESP": "1918", "AUS":"115", "CAN": "304"};[/CODE]
Copy linkTweet thisAlerts:
@minhntJun 15.2015 — As you've probably noticed if you have tried to run that code
[CODE]var country = {US = "2105" UK = "1103" GER = "704" ESP = "1918" AUS = "115" CAN = "304"}[/CODE]

that's not a correct way to write a javascript object. Try:

[CODE]var country = {"US" : "2105", "UK": "1103", "GER": "704", "ESP": "1918", "AUS":"115", "CAN": "304"};[/CODE][/QUOTE]


And the function should be:

[CODE]
function countryCode(countryId) {
var country = {"US" : "2105", "UK": "1103", "GER": "704", "ESP": "1918", "AUS":"115", "CAN": "304"};
var id = countryId.toUpperCase();
if(country[id]) {
return country[id];
}

return -1;
}
[/CODE]
Copy linkTweet thisAlerts:
@orenpp1authorJun 15.2015 — Hi,

Didn't understand your code, it's probably my lack of experience....

Let me simplify things by explaining. my outcome should be this link:

http://www.MyWebSite?pt=1&cc=(Variable1)&nums={{TrackingNumber}}

Variable 1 should be a function returning a number representing country code (should be embeded in the URL

{{TrackingNumber}} is a variable for tracking number that is assigned automatically from an outside source.

As shown I did create an object with values to switch country code to country ID but I need help for the whole URL to show properly.

Having difficulties in syntax and don't know how to make {{TrackingNumber}} & {{BuyerCountry}} global to the page

In away that my URL will work

Would appreciate any help....
×

Success!

Help @orenpp1 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 5.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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