Bad Data explainer

Aim of the puzzle: Use console.log() to debug code that is not working as intended.
Walk through of solution: When a function or a block of code isn’t working properly, it can be useful to add console.log() statements to take a closer look at what is going on.

In this puzzle, the produce array is imported from additional code. The getLength() function will iterate through this array and print the length of each string in the array to the console.

There is a problem, however. For one of the items in the array, the function is printing a length of 0.

To figure out what’s going on, let’s use a console.log() to take a look at each string in the produce array.

To complete the puzzle, add a console.log() to the for loop, and use the looping variable as an argument. This will print each string in the produce array to the console, one at a time.

When this has been done, running the code will show that one of the items in the produce array is an empty string, which is the source of the problem!

Sample code solution:

import { produce } from 'grasshopper.data';

function getLength(words) {
    for (let word of words) {
        console.log(word);
        console.log(word.length + ' characters long');
    } 
}

getLength(produce);

Javascript Concepts: Debugging, console.log(), For Loops, .length
Additional Code (hidden code that runs before the puzzle’s code):

let produce = ['bananas', 'radishes', 'basil', 'asparagus', '', 'leeks', 'potatoes'];
3 Likes

No comments?

Should I take that to mean everyone solved this task without any problems? :rofl:

3 Likes

Yeah… :man_shrugging:

did you? :man_shrugging:

I had a little trouble… which then I felt really dumb about because it should’ve been easy.

1 Like

So, soon straight forward :+1::+1::smile_cat::grin:

import

{

produce

}

from

‘grasshopper. data’

;

function

getLength

(

words

,


)

{

for

(

let

word

of

words

)

{

console

.

log

(

word

)

;

console

.

log

(

word

.

length

’ letters’

)

;

}

}

;

getLength
(
produce

;

i dont get how to fo it

i got it now i though it was words LOL

I got it, but I have a question!
At getLenght function, why the paramater is “words” and not “produce”, since the objects are inside de “produce” array. And the “produce” array doesn’t have any variable named “words”.

And how the code understands that “words” refeers to the items in the “produce” array?

1 Like

Why the code does not run

Hello dear @skuleton ,
thank you sharing your query.

In your 4th line, your parentheses are not at the accurate place.
It should be like:

console.log(word.length+ 'characters long');

Please update it & I hope error will flee away.
Don’t forget to notify if any further problem faced.

-Ahnaf

1 Like

Thank you very much

1 Like

you’re obviously welcome

i still don’t get it please help, it has become extremely frustrating already


Hello friends of self-education, this is the correct answer, cross.


If any one has any problem try this code I am sure it will help u