/    Sign up×
Community /Pin to ProfileBookmark

a simple javascript function needed

I need a simple javascript function f(z) which does the following.

for z=3,4 —> output =1
for z=5,6 —> output =2
for z=7,8 —> output =3
for z=9,10 —> output =4
for z=11,12 —> output =5
for z=13,14 —> output =6
for z=15,16 —> output =7

There is no other value of z than given above..

Can you please help ? how do I write this function ?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Gozzy82Feb 09.2010 — this should be it..

<i>
</i>&lt;script type="text/javascript"&gt;
function f(z){
var ret;
switch(z){
case "3,4": ret = 1; break;
case "5,6": ret = 2; break;
case "7,8": ret = 3; break;
case "9,10": ret = 4; break;
case "11,12": ret = 5; break;
case "13,14": ret = 6; break;
case "15,16": ret = 7; break;
default: ret = 0; break;
}
return ret;
}
/* uncomment this to test the function
window.onload = function(){
alert( f('11,12') );
}
*/
&lt;/script&gt;

Copy linkTweet thisAlerts:
@rpg2009Feb 09.2010 — Got a little silly with the ands(&&)

[CODE]function f(z){
var x = z.match(/^(dd?),(dd?)$/);
return (x && (x[1]==x[2]-1) && (x[2]<17 && x[2]>3) && !(x[2]%2))?x[2]/2-1:"invalid entry";
}[/CODE]


Output examples

[CODE]alert (f('2,3')); //invalid entry
alert (f('3,4')); //1
alert (f('15,16')); //7
alert (f('17,18')); //invalid entry
alert (f('3,6')); //invalid entry
alert (f('6,7')); //invalid entry
alert (f('9,10')); //4
alert (f('a,b')); //invalid entry[/CODE]


RLM
Copy linkTweet thisAlerts:
@rpg2009Feb 10.2010 — I need a [B]simple[/B] javascript function[/QUOTE]

Given the question and a bit of hindsight. A breakdown.

var x = z.match(/^[B]([/B]dd?[B])[/B],[B]([/B]dd?[B])[/B]$/);

The regex
[CODE]/^ begins with
( capture this
d number between 0-9
x? match x 0 or 1 times
so dd? means a single or double digit number. d{1,2} does the same.
) end of capture
escapes and , means match a comma
(dd?) capture second single or double digit number
$/ ends with
()'s or back-references capture the matches in an array. In this case x[1] is the first (match) and x[2] the second (match).

so if z = '3,4' the match returns x[1] = 3 and x[2] = 4[/CODE]


the condition
[CODE]
x && (x[1]==x[2]-1) && (x[2]<17 && x[2]>3) && !(x[2]&#37;2)

x is there a match at all?
x[1]==x[2]-1 does the first number equal the second-1?
x[2]<17 && x[2]>3 does the second number fall between 3 and 17?
!(x[2]%2) is 2 a factor of the second number or in other words is x[2] an even number?
[/CODE]


[CODE]?x[2]/2-1 if that matches, return the second number divided by 2 and take away 1
: "invalid entry"; else return invalid.[/CODE]


I'm sure this could be improved on using better or simpler logic, but thought a breakdown may be helpful.

RPG
×

Success!

Help @cofactor 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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