Plz explain how this code is working to find the big word in a sentence

function findBigOne(str){
  let words = str.split(' ');
  return words.reduce(function(a,b){
    return Math.max(a, b.length);

console.log(findBigOne('hi, my name is miazan mahi'))

Why don’t you see if you can try to explain what you think the code is doing and we can help fill in the gaps for you?

1 Like

I am just confused about how a and b are exchanging their values in every iteration round of reduce method

Not sure what you mean by exchanging, but a represents the accumulator during each iteration. b represents the current element being iterated over. a will start as the first element and b as the second element unless an initial value is specified (like the 0 in the code above). In all proceeding iterations, a will be the value returned from the previous iteration.

Thank you so much. I have one last query about this code and that is what will be the value of a in second and in third round of iteration. I need know the exact value.

Add a console.log statement to display the values of a and b before the return statement in the reduce method’s callback function to see what they are at every iteration.