Wiki Elegant

Which Generation Are You?

Try finding your ancestors and offspring with code.

Create a function that takes a number x and a character y ("m" for male, "f" for female), and returns the name of an ancestor (m/f) or descendant (m/f).

  • If the number is negative, return the related ancestor.
  • If positive, return the related descendant.
  • You are generation 0. In the case of 0 (male or female), return "me!".

Examples

generation(2, "f") ➞ "granddaughter"

generation(-3, "m") ➞ "great grandfather"

generation(1, "f") ➞ "daughter"

Notes

Check the Resources tab for helpful hints.

GenerationMaleFemale
-3great grandfathergreat grandmother
-2grandfathergrandmother
-1fathermother
0me!me!
1sondaughter
2grandsongranddaughter
3great grandsongreat granddaughter
Answer (1)
function generation(x,y) {
const elo = {
‘-3’: { m: “great grandfather”, f: “great grandmother” },
“-2”: { m: “grandfather”, f: “grandmother” },
“-1”: { m: “father”, f: “mother” },
0: { m: “me!”, f: “me!” },
1: { m: “son”, f: “daughter” },
2: { m: “grandson”, f: “granddaughter” },
3: { m: “great grandson”, f: “great granddaughter” },
};
return elo[x][y]
}
Answer 2
function generation(x,y) {
let f = [‘great grandmother’,‘grandmother’,‘mother’,‘me!’,‘daughter’,‘granddaughter’,‘great granddaughter’];
let m = [‘great grandfather’,‘grandfather’,‘father’,‘me!’,‘son’,‘grandson’,‘great grandson’];
return y === ‘m’ ? m[3+x] : f[3+x];
}
Answer (3)
const generation = (g, s) => ([,,‘grand’,‘great grand’][Math.abs(g)] || ) +
[(s==‘m’?‘fa’:‘mo’)+‘ther’,‘me!’,s==‘m’?‘son’:‘daughter’][Math.sign(g) + 1];


Answer (4)
function generation(x,y) {
const generations = {
“-3”: [“great grandfather”, “great grandmother”],
“-2”: [“grandfather”, “grandmother”],
“-1”: [“father”, “mother”],
0: [“me!”, “me!”],
1: [“son”, “daughter”],
2: [“grandson”, “granddaughter”],
3: [“great grandson”, “great granddaughter”]
}
return generations[x][y === “m” ? 0 : 1]
}
Answer (5)
const generation = (x, y) =>
`${Math.abs(x) > 2 ? “great “ : ”}`+
`${Math.abs(x) > 1 ? “grand” : ”}` +
`${x === 0 ? “me!” : x > 0 ?
y === ‘m’ ? “son” : “daughter” :
y === ‘m’ ? “father”:“mother”}`
Answer (6)
function generation(x,y) {
return generations[x+][y]
}
const generations = {
‘-3’:{
m:‘great grandfather’,
f:‘great grandmother’,
},
‘-2’:{
m:‘grandfather’,
f:‘grandmother’,
},
‘-1’:{
m:‘father’,
f:‘mother’,
},
‘0’:{
m:‘me!’,
f:‘me!’,
},
‘1’:{
m:‘son’,
f:‘daughter’,
},
‘2’:{
m:‘grandson’,
f:‘granddaughter’,
},
‘3’:{
m:‘great grandson’,
f:‘great granddaughter’,
}
}
Answer(7)
function generation(x,y) {
let f = {“-3”:“great grandmother”,“-2”:“grandmother”,“-1”:“mother”,“0”:“me!”,“1”:“daughter”,“2”:“granddaughter”,“3”:“great granddaughter”}
let m = {“-3”:“great grandfather”,“-2”:“grandfather”,“-1”:“father”,“0”:“me!”,“1”:“son”,“2”:“grandson”,“3”:“great grandson”}
if(y==‘f’)return f[x]
return m[x]
}
Answer (8)
function makePlusFunction(baseNum) {
return y => baseNum + y
}


Answer (9)
function makePlusFunction(baseNum) {
return function(a){
return baseNum + a;
}
}


Answer (10)
function makePlusFunction(baseNum) {
return function(newArg){
return baseNum+newArg;
}
}


Answer (11)
function makePlusFunction(baseNum) {
return (x)=>baseNum+x;
}


Answer (12)
function makePlusFunction(baseNum) {
return function (a){
return baseNum + a
}
}


Answer (13)
function makePlusFunction(baseNum) {
return function plusFive(num2)
{
return baseNum+num2;
}
}


Answer(14)
function makePlusFunction(x) {
return function(y){
return x+y
}
}


Answer (15)
function makePlusFunction(baseNum) {
return function(arg){
return baseNum+arg;
};
}
Answer (16)
function makePlusFunction(baseNum, num) {
return (num) => baseNum + num
}


Answer (17)
function makePlusFunction(baseNum) {
return (n) => n + baseNum
}


Answer (18)
function makePlusFunction(baseNum) {
function secondFunction(newArg) {
return newArg + baseNum
}
return secondFunction
}


Answer (19)
function makePlusFunction(baseNum) {
function inner(argument) {
return baseNum + argument;
}
return inner;
}


Answer (20)
function makePlusFunction(baseNum) {
function inner(num){
return baseNum + num
}
return inner
}


Answer (21)
function makePlusFunction(baseNum) {
const newPlusFunction = function(num){
return num+baseNum
}
return newPlusFunction
}


Answer (22)
function makePlusFunction(baseNum) {
return function(anotherNum) {
return baseNum + anotherNum;
}
}


Answer (23)
function makePlusFunction(baseNum) {
return function (newnumber) {
return newnumber+baseNum
}
}


Answer (24)
function makePlusFunction(baseNum) {
return function(num){
return baseNum + num;
}
}


Answer (25)
function makePlusFunction(baseNum) {
return function plusBaseNum(newArg) {
return baseNum + newArg
}
}