Home > OS >  Javascript function with for loop, prompt ask user how many numbers, the number of prompts shows up
Javascript function with for loop, prompt ask user how many numbers, the number of prompts shows up

Time:01-06

I am brand new with javascript and I have a problem that i can't figure out.

My first prompt ask the user how many numbers he wants to give in. If he give in "3" then 3 prompts shows up to ask him to put a random number he wants.

if there is no number but a letter or word it should show "wrong" cancelled or don't wrote anything.

        function controleAantal(aantal) {
        if ( aantal === " ") {
            return "U heeft niks ingegeven" // user didn't wrote anything
        }

        else if(aantal == null){
            return "Uw hebt geannuleerd" // user cancelled
        }

        else if( aantal > 0) {
            return aantal // display number that user has given
        }

        else {
            return "Dit is geen correcte ingave "   0 // This is not correct number
        }
    }
    

        let vragen = Number(prompt("Hoeveel getallen wilt uw ingeven?"));

        let getallen = [];
        let vraag2 = controleAantal(getallen)

        for (let i = 0; i < vragen; i  ) {
        getallen.push(Number(prompt(`Geef in jouw nummer ${i   1} in.`)));



        document.write(`<p> ${vraag2[i]}</p>`);
        }

CodePudding user response:

There are quite some issues with your code. You mix up several variables and should not use a mixed return value, since then you cannot distinguish between error case or normal case. You may want to try the solution below. Also consider to check the input of your actual numbers in the loop with a somewhat similar approach.

function controleAantal(aantal) {
  if(aantal === null) { // no input
    return { error: "Uw hebt geannuleerd" } // user cancelled
  }
  else if ( !aantal.trim().length ) { // trimmed string is empty
    return { error: "U heeft niks ingegeven" } // user didn't wrote anything
  }
  else if(  aantal > 0) { // casted number is greater than 0
    return { aantal } // display number that user has given
  }
  else {
    return { error: "Dit is geen correcte ingave" } // This is not correct number
  }
}
    

let vragen = prompt("Hoeveel getallen wilt uw ingeven?");
let vraag2 = controleAantal(vragen)

if (vraag2.error) {
  document.write(`<p> ${vraag2.error} </p>`)
} else {
  let getallen = [];

  for (let i = 0; i < vraag2.aantal; i  ) {
    getallen.push(Number(prompt(`Geef in jouw nummer ${i   1} in.`)));

    document.write(`<p> ${getallen[i]}</p>`);
  }
}

CodePudding user response:

Try this:

function controleAantal(aantal) {
    if ( aantal === "") {
      return "U heeft niks ingegeven" // user didn't wrote anything
    }

    else if(aantal == null){
      return "Uw hebt geannuleerd" // user cancelled
    }

    else if( aantal > 0) {
      return aantal // display number that user has given
    }

    else {
      return "Dit is geen correcte ingave "   0 // This is not correct number
    }
  }


  let input1 = prompt("Hoeveel getallen wilt uw ingeven?");
  let vragen = Number(input1);
    console.log(`input 1 [${input1}], number ${vragen}`, input1)
  if (isNaN(vragen) || vragen<1) {
    document.write(`<p> input1 : [${input1}], translated text [${controleAantal(input1)}]</p>`);
  } else {

    // let vraag2 = controleAantal(getallen)

    for (let i = 0; i < vragen; i  ) {
      // getallen.push(Number(prompt(`Geef in jouw nummer ${i   1} in.`)));
      let input = prompt(`Geef in jouw nummer ${i   1} in.`);


      // document.write(`<p> ${vraag2[i]}</p>`);
      document.write(`<p> ${i} : input[${input}] tranlated text ${controleAantal(input)}</p>`);
    }
  }

The problem is caused by Number(x), it will return 0 if x is blank string('' or ' ' with any length).

Use isNaN() to check number, NaN stands for Not a Number.

CodePudding user response:

Welcome @kenny to SO.

There are several solutions to your issue but I would give you the simple one that fits your code.

  1. Your function accepted the primitive parameter aantal which can be :

    • string ( empty )
    • null
    • number
  2. You call that function with an array variable named gettalen ! !

  3. Your checks if/else expressions are against a primitive variable not an array

  4. The expected result whatever your input is NaN :

    (Number(prompt(`Geef in jouw nummer ${i 1} in.`)))

    So your function returns the last check Dit is geen correcte ingave

  5. If I iterate 3 times I get D i t.

You should ask GOOGLE:

  1. How to check against empty value?
  2. How to check against a number value?
  3. How to check against a null value?

The solution

function controleAantal(aantal) {
  if (aantal === '') {
    return 'U heeft niks ingegeven'; // user didn't wrote anything
  }
  if (aantal === null) {
    return 'Uw hebt geannuleerd'; // user cancelled
  }
  if (!Number.isNaN(aantal) && aantal > 0) {
    return aantal; // display number that user has given
  }
  return 'Dit is geen correcte ingave '; // This is not correct number
}

let vragen = Number(prompt('Hoeveel getallen wilt uw ingeven?'));

let getallen = [];

for (let i = 0; i < vragen; i  ) {
  const value = prompt(`Geef in jouw nummer ${i   1} in.`);
  const controlled = controleAantal(value);
  getallen.push(controlled);
  document.write(`<p> ${getallen[i]}</p>`);
}

  • Related