Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

320
Views
How to increment/decrement variable value only once while in a loop?

I need to decrement var "left" by 1 and only once instead of having it go through a loop and decrement if conditions pass true. But conditions are valid only if they are in a loop. How would I do this?

 let key = e.key.toLowerCase()
    for (i = 0; i < word.length; i++) {
        if (word[i] == key) {
            if (guessed[i] != key) {
                console.log(e.key)
                guessed[i] = key
            } else {
                console.log('This is where i want left--')
            }
        }
    }
    left--;  //Need this to decrement only once
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Store whether left has been decremented in a variable:

let key = e.key.toLowerCase()
let decrementedLeft = false;
for (i = 0; i < word.length; i++) {
    if (word[i] == key) {
        if (guessed[i] != key) {
            console.log(e.key)
            guessed[i] = key
        } else {
            if(!decrementedLeft){
                decrementedLeft = true;
                left--;
            }
        }
    }
}
if(!decrementedLeft){
    left--;
}
about 4 years ago · Juan Pablo Isaza Report

0

You can use this method:

 key = e.key.toLowerCase() 
Let decrement=false;
for (i = 0; i < word.length; i++) { 

if(decrement==false)
if (word[i] == key) { if (guessed[i] != key) { console.log(e.key) guessed[i] = key } else { left--;
decrement=true;} } }

Or you can just break out from the loop using break

about 4 years ago · Juan Pablo Isaza Report

0

Due to more conditions I decided so. Thank you for your answers! As a standard, got myself a few additional problems. Like first if{}else{} in the loop is executing both if and else instead of one or the other... So confusing.

    let correct = 0;
    let key = e.key.toLowerCase()
    for (i = 0; i < word.length; i++) {
        if (word[i] == key) {
            if (guessed[i] != key) {
                guessed[i] = key
                correct = 1;
                console.log(e.key)
            } else {
                console.log('Guessing same key')
                correct = 1;
            }
        }
    }
    if (correct != 1) {
        left--;
        console.log('Decrementing')
        tries.innerHTML += `<span>${key} </span>`
        correct = 0
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!