8 kyu
Training JS #12: loop statement --for..in and for..of
11,075 of 11,122myjinxin2015
Loading description...
Fundamentals
Tutorials
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Read the question carefully, this kata is very easy lol.
The longest text ever, but still not a clear task description :/
When I test, my log shows the correct answer ['Ihave, 'money'], but the test fails and it says instead of the correct answer (identical to mine), it got 'undefined'. Not sure how to proceed.
Did you console.log your answer instead of returning it?
Yup, I console.logged it instead of returning it. Thanks!
Auful kata with confusing explanation...
{Our:"earth",is:"a",beautyful:"world"} --> Why should this return only "earth" and "world" (and not "beautyful")
{Ihave:"enough", money:"to",buy:"a",car:"model"} --> But this should return "Ihave" AND NOT "enough" ?!?
Please read the description:
Is
"beautyful"
length equal to 5? Is"enough"
length equal to 5?Ah okay my bad, thought it meant 5 or bigger. (the summary could still be simplified though)
Instructions are super unclear/confusing here. I had to forfeit my solution to come check why random tests keep failing at attempt while the sample ones are passing.
So for future readers: do NOT iterate first on keys and then on values, because you'll get the results in the wrong order. You have to check key-value pairs of properties in the same loop, property by property. I wish at least the first 3 tests had helped to clarify this, but they hadn't.
good explanation, good task.
This comment has been hidden.
This kata uses Node.js v8, which doesn't support
flat
method.flat
method is added on Node.js in version11.15.0
This comment has been hidden.
Care to explain what doesn't work exactly? Read this: https://docs.codewars.com/training/troubleshooting/
this is the error: Expected: '['Ihave', 'money', 'model']', instead got: '[]'
Doesnt matter wich solution i write, always give me that error, i tried most of this kata solutions of other users and cant get it done. In vscode it works fine and return what it should return
Ok what the hell, i tried the submit button instead of the test and it passed LOL
The test button on this one is not working properly
Backup your code, click reset, paste it and try again the sample tests, they work fine for me.
Explanation is very confusing; I had to figure out the logic from the assertion function in order to understand what the prompt should be asking.
Could have been explained much easier as "given an input object, push the value and/or key from the object into an output array if the length of the value and/or key is 5"
Revised
Task description is horrible. Commas are weakly used, so it was hard to understand.
This got me confused. Simple, yet almost pulled my hair out.
This comment has been hidden.
You only need the
value
not thekey
To be more clear,
value
is equivalent tokey
which accesses the key, closingThis comment has been hidden.
Not a suggestion.
This comment has been hidden.
What should i do if the key and value have the same length?
You push both when the length is 5 and do nothing if it's not.
How?
First the key, then the value. You can see that in the last sample test:
This comment has been hidden.
It's different, read this%20is%20a%20string%2C%20your,from%20your%20first%20function%20call)
this must be the most verbose kata on this website... wtf!
how to get in the same order as it is in the object ? I am getting[Pears, sweet, apple]
First you have to check the key length then the key value. If you write the conditions the other way around, you'll get this error.
OP solved it, closing
First Kata I don't understand EVEN after peeking at other warriors answers...
good kata once i finally understood what the task was.
Just to clarify that it is best practice not to use a for in loop to traverse an array (you can use a for of instead) however using a for in to traverse an object is ok? Why is this?
This comment has been hidden.
This comment has been hidden.
key and
value
This comment has been hidden.
The description is not clear, it is confusing. You need to the traverse the
obj
, if the length of the object key equals to 5, then push the key value to the array (you need to define the array by yourself, this time I won't help you). Additionally push the value to the array as well, if the length of the value is equal to 5.Could be: Define an array yourself. Tranverse the object passed and if the length of the object key string is equal to 5 then push this object key string to the array. Additionally if the object's value string length is equal to 5 then push this object's value string to the array. Return this array.
You are right.
So confusing. Please, update the description.
God bless you. I came here to see if someone explained the exercise in a more understandable way. I would have never in a million years understood it from the original description.
thank you. I could not have done it without your explanation
Oh my God! Now I got it! Thank you so much! That description is so BADLY WRITTEN! It made no sense what I was supposed to do...
^^
This comment has been hidden.
You should put two if statements in one for loop
Thank You !:)
I am so close yet cannot figure out how to resolve last test: Expected: '['Pears', 'apple', 'sweet']', instead got: '['Pears', 'apple']'
Do I need to put something after second conditional statement ?
You need two conditions, in the correct order. First condition checks whether the key has length 5, if so, add the key to the array. Second condition checks whether the object has length 5, if so, add the key to the array.
how about when the value and key's lengths are both 5? push them all?
对,两个都要,先key,后value
It was a very helpful kata, thanks Just one issue, the way the instructions are worded its a bit unclear that you are asking to add to the array both the keys, and the object values that are 5 characters long.
In my description, I wrote:
I changed to:
Is this OK?
No, thats also a little unclear. Maybe like this: You need to the traverse the obj, if the length of the object key equals to 5, then push the key value to the array (you need to define the array by yourself, this time I won't help you). Additionally push the value to the array as well, if the length of the value is equal to 5.
OK. Modify the description in accordance with your proposal. Thanks for your help ;-)
Great Kata! Just a note for the description, when it states: "In the example above, keys is "1", "2" and "3", we can't see the quotes because console.log() doesn't show it." It should read "In the example above, keys are "0", "1", "2"..."
Thanks for the informative katas! Been going through each one in your series.
thanks ;-)
Short and simple though it may be, this kata actually provides a clear and concise view of the difference between accessing the name of a property and accessing its value. I am sure that I will be referring back to this again and again as I work on more challenging katas involving objects and their properties in the future. Thanks!
This comment has been hidden.
The wrong point is the
else if
;-)You need two
if
Ah got it! thank you :)
This comment has been hidden.
your problem is the use of
else
, change it to twoif statement
will be right. ;-)This comment has been hidden.
The description says
for..in
has "a flaw" for traversing array. Can you give a reference where I can learn more about the flaw?No reference to you, but I can give you an example.
output:
for..in skip the null value of arr, and traverse the prototype.
Rated and upvoted ;)
Thank you ~~ ;-)
This comment has been hidden.
You should complete it in 1 traverse, for each element of object, first determine
key
, and then determine itsvalue