/    Sign up×
Community /Pin to ProfileBookmark

Java Script – need help

Can someone please tell my why my if statement doesn’t work and ruins all the code above too?
https://codepen.io/raul-rogojan/pen/LoOoVm?editors=0010

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMay 22.2019 — 
  • 1. The conditional expressiong is missing:
    // Make the buttons untargetable
    if (targetIndex === 0) {
    prevBtn.classList.add('cant-click');
    nextBtn.classList.remove('cant-click');
    } else if (targetIndex === slides.length) { // error was here
    prevBtn.classList.remove('cant-click');
    nextBtn.classList.add('cant-click');
    } else {
    prevBtn.classList.remove('cant-click');
    nextBtn.classList.remove('cant-click');
    }

  • 2. targetIndex is not defined
  • Copy linkTweet thisAlerts:
    @SempervivumMay 22.2019 — [[58],[2,4]]
    Copy linkTweet thisAlerts:
    @codyhillauthorMay 22.2019 — @Sempervivum#1603931 I defined it like this const targetIndex = slides[targetIndex] .

    But apparently this is not how you get the index of the targeted slide.
    Copy linkTweet thisAlerts:
    @SempervivumMay 22.2019 — How to get the index of a HTML element inside a nodelist is described here:

    https://stackoverflow.com/questions/3779986/how-to-get-the-index-of-the-element-in-javascript
    Copy linkTweet thisAlerts:
    @codyhillauthorMay 22.2019 — @Sempervivum#1603937 I didn't understood what was saying there but I tried this and it still not working.
    const showHideArrow = (targetIndex, prevBtn, nextBtn, slides) => {
    // Make the buttons untargetable
    if (targetIndex === 0) {
    prevBtn.classList.add('cant-click');
    nextBtn.classList.remove('cant-click');
    } else if (targetIndex === slides.length -1) {
    prevBtn.classList.remove('cant-click');
    nextBtn.classList.add('cant-click');
    } else {
    prevBtn.classList.remove('cant-click');
    nextBtn.classList.remove('cant-click');
    }
    }


    And then I call it to the next button and I add this const const nextIndex = slides.findIndex(slide => slides === nextSlide);

    this is how I called it showHideArrow(nextIndex, prevBtn, nextBtn, slides);
    Copy linkTweet thisAlerts:
    @SempervivumMay 22.2019 — As you already converted the nodelist to an array you can easily get the index of the next slide by indexOf()
    const targetIndex = slides.indexOf(targetSlide);
    I implemented it like this and it works:

    https://codepen.io/Sempervivum/pen/MdrORZ?editors=0010
    Copy linkTweet thisAlerts:
    @codyhillauthorMay 22.2019 — @Sempervivum#1603943 Jeez men , you are a magician ! . Some time coding is so hard xd , specially when you don't know what you are doing .
    ×

    Success!

    Help @codyhill spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.4,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...