Closed j3ppah closed 3 years ago
additional info: i have also tried the obvious things, like key.toString(), since it is being asked as a string. but that does not change anything.
The problem is in your code. Your return
statement appears to be correctly returning the desired key name (at face value). But the problem is the placement of the return
statement. Its inside of two nested forEach
functions which means its return
ing from the it's parent foreEach
statement and not from your Munch
function.
This is what you have:
function Munch(myArray) {
console.log(myArray)
myArray.forEach((element) => {
Object.entries(element).forEach(([key, value]) => {
if (value === 'lollipop') {
console.log(key)
return key
}
})
})
}
As you can see, the item gets returned from Object.entries(element).forEach
but not from the Munch
program. You could store the results to a variable declared in the parent Munch
function and simply return that value. For example:
function Munch(myArray) {
var keyname;
myArray.forEach((element) => {
Object.entries(element).forEach(([key, value]) => {
if (value === 'lollipop') {
console.log(key)
keyname = key
}
})
})
return keyname
}
I didn't test (your or my) code at all so there may be something off in that but it should illustrate my point.
@j3ppah if you could acknowledge receipt of my last message. Would like to hide my comment after I know you've seen my last response. (since its a potential spoiler for others).
Thank you, i have got it. I will test it shortly. I don't think that is clear from the game/level description. But thank you so much for replying
The issue is, that when you do it the way you are suggesting, which should also be valid. It throws an error: r: TypeError: right-hand side of 'in' should be an object, got null Had the issue earlier, and decided to change it to what i have now, since the other will not compile. It is weird however, because it compiles fine locally.
Got it to work, but i deffinatly believe somthing is behaving a bit weird. :)! Thanks for replying!
As mentioned previously, I havent tested so there may be syntax errors but using that concept should work ( my proof of concept I came up with for testing is something similar but different ).
Alternatively, you could us the standard nested for loop instead of forEach
:
function Munc(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
console.log(arr[i][j]);
return arr[i][j];
}
}
}
Then any return
statement would properly return from the Munch
function and not from the forEach
function.
@j3ppah hope that helps!
Hi. I am having an issue, where the level 8 Munchkin will not accept my answer. Am i misunderstanding somthing, or is this a bug? Hope you can help. If the elf code is needed aswell, i can post that. Decided not to, to avoid spoilers for others.
Here is my function, that returns the correct answer (to my understanding) function Munch(myArray) { console.log(myArray) myArray.forEach((element) => { Object.entries(element).forEach(([key, value]) => { if (value === 'lollipop') { console.log(key) return key } }) }) }
Here is the console output from the game. (2 different runs) Info: [{"t3":12,"6ya4":20,"fu":"wq6hj","b32":"p3h","l8ps":42,"v5":40,"we":14},{"3yw1c":33,"dk394":"qvay","g7q":44,"plw9j":"yd4v","w4":33,"sidz":"i8","zlvl0":"nt50","ci":44,"8t4j":"1l9"},{"7142":"lollipop","0nxm":14,"7b":"1jz","7b0":"9d","xvc":19,"br08q":"j4qke","lkq":"jc0p","tx1a2":40},{"nyy":49,"9azd":12,"k5":"42z","8a8lm":39,"5xu":"zc","q6kh":28,"mr":"mh"},{"u5":13,"6v":32,"of2":21,"63jdq":36,"0uam":10,"3m":37,"udy":"af7","b8bdg":46},{"rycv":"jc","chy":"urf","dt":10,"wqvuq":"u1yt0","r4q8":"po","zvs0l":"zveq","xab7":18,"a0":20},{"qjeeg":"vlfv9","uskt":"m8g","e4il4":30,"w66ix":26,"j7":23,"b0ql":"is","x7h":"kz262"},{"jo":48,"somy":24,"reju0":"0g","e4ic4":36,"h88m":"sjhx","iv5m":"35g","1v":45}]
Info: "7142"
Error: Munchkin #0: Incorrect, your function did not return the proper key name with a value of lollipop
Info: [{"idn4":"r457","m6":"s2lo","sik9":"8x","zj7h":"dy6ud","hk":30,"lft5":"37","cj9r4":"8lsk9","9cj":17,"y0a":"8u29","b9xx6":41},{"7op9":50,"1nu":49,"rz":"0i0u","59pf4":29,"06p9":38,"mh":12,"qru0u":"dvl","haxz":"fcpbh"},{"m4r2":"nfzx","lx":"mxkdw","qtuey":"c25l","d3lx":"p7x","wihh":"jpea","aankd":44,"6p":28,"cwlt0":16},{"i0q":47,"m06z":14,"1d":"5yp","mbn":14,"1e0":21,"kq":"qa","mvkd":"xor39","cdn":30,"fyst":"uq9o","dz1x":49},{"51":26,"3hic":"1tybk","65w":"pb","xpej8":35,"n6mh":"qxf","a0pgv":33,"4gu":"5btl","jp5":"w0","k5":"hq9"},{"8u4r":19,"weh":"uvx8e","2fwy":"ar0","4r":37,"55p":"yd2dm","ro":20,"kmlp4":50,"n89":39},{"lul":"qfj","3thee":"46a4","jali":36,"0y":"rqcjs","lz":29,"mq":"5pzjq","cc":23,"wwn":31},{"fzkl":"0y","rb4":"lollipop","hgg2j":"5j","dh":"ji","t5e2":27,"14n5e":"38","xzrbu":"87ou","pm3b":"qff3","0a":"hp","4pw":20},{"26":"a8","20p":"9lqd","fk":46,"y8ui":40,"phr":29,"hmv":"ms8","to":50},{"3b4vi":"hdyj9","mig":10,"t9":"1hidk","4our":18,"8z5b":"eka","nyo":"vhmwm","qqt":"d2vu"}]
Info: "rb4"
Error: Munchkin #0: Incorrect, your function did not return the proper key name with a value of lollipop