rating component returns "undefined" (javascript)

61 Views Asked by At

I'm facing a challenge from Front End Mentor - interactive rating component.

When I use 'console.log' in function 'getRate' it works and logs a number from button.

Also, when I assign a number to 'let rate' instead of a function, then function 'submit' shows this number in a 'thank-you-state'.

But when I try to combine all of this by this: 'let rate = getRate();' then i see 'undefined' instead of a number in a 'thank-you-state'.

    let chosenRate = document.querySelector('.chosen-rate');
   
    function getRate() {
        document.querySelectorAll('.rating-btn').forEach(btn => {
            btn.addEventListener('click', e => {
                return e.target.innerText;
            });
        });
    };
    
    let rate = getRate();
    
    function submit() {
        chosenRate.innerText = rate;
        document.querySelector('.submit-btn').addEventListener('click', () => {
            document.querySelector('.rating-state').classList.add('hide');
            document.querySelector('.thank-you-state').classList.remove('hide');
        });
    }

    getRate();
    submit();
<div class="survey-box">
                <div class="rating-state">                  
                    <h2>How did we do?</h2>
                    <p>
                        Please let us know how we did with your support request. All
                        feedback is appreciated to help us improve our offering!
                    </p>
                    <div id="buttons" class="buttons">
                        <button class="rating-btn btn-1">1</button
                        ><button class="rating-btn btn2">2</button
                        ><button class="rating-btn btn-3">3</button
                        ><button class="rating-btn btn-4">4</button
                        ><button class="rating-btn btn-5">5</button>
                    </div>
                    <button class="submit-btn">Submit</button>
                </div>

                <div class="thank-you-state hide">                  
                    <div class="selected-rate">
                        <p>You selected <span class="chosen-rate"></span> out of 5</p>
                    </div>
                    <h2>Thank you!</h2>
                    <p>
                        We appreciate you taking the time to give a rating. If you ever need
                        more support, don't hesitate to get in touch!
                    </p>
                </div>
            </div>
1

There are 1 best solutions below

0
JameyPlay On

I remove getRate() function

let chosenRate = document.querySelector('.chosen-rate');
    document.querySelectorAll('.rating-btn').forEach(btn => {
        btn.addEventListener('click', e => {
          chosenRate.innerText = e.target.innerText;
        });
    });



function submit() {
    document.querySelector('.submit-btn').addEventListener('click', () => {
        document.querySelector('.rating-state').classList.add('hide');
        document.querySelector('.thank-you-state').classList.remove('hide');
    });
}

submit();

ref. my repo: interactive-rating-component