0

I am trying to use a function to change an input to what is set within the parameters. The checks work fine in determining if the input is correct, but the don't change the original inputs. I feel like it is something small I am missing, is anyone able to help? Here is my code:

var x = prompt("enter number");
var y = prompt("enter number");

function isThisRowNumberCorrect(number1){
if (isNaN(number1) === true){
    alert("Please choose a row number between 0 and 5!");
    number1 = prompt("Please choose a row number between 0 and 5.")-'';
}
if (number1 > 5){
    alert("Please choose a row number between 0 and 5!");
    number1 = prompt("Please choose a row number between 0 and 5.")-'';
}
if (number1 < 0){
    alert("Please choose a row number between 0 and 5!");
    number1 = prompt("Please choose a row number between 0 and 5.")-'';
}
number1 = number1;
}

function isThisColumnNumberCorrect(number2){
if (isNaN(number2) === true){
    alert("Please choose a column number between 0 and 5!");
    number2 = prompt("Please choose a column number between 0 and 5.")-'';
}
if (number2 > 5){
    alert("Please choose a column number between 0 and 5!");
    number2 = prompt("Please choose a column number between 0 and 5.")-'';
}
if (number2 < 0){
    alert("Please choose a column number between 0 and 5!");
    number2 = prompt("Please choose a column number between 0 and 5.")-'';
}

number2 = number2;
}
isThisRowNumberCorrect(x);
isThisColumnNumberCorrect(y);

console.log(x + "" + y);

2 Answers 2

2

The parameters of the two functions are primitive type. When passed into the function, JavaScript engine will create a new copy of the primitive parameters. So setting the value inside the function number2 = number2; will not change the value outside.

You need to return the new value from the function isThisRowNumberCorrect:

return number1;

And call the function like:

x = isThisRowNumberCorrect(x);
Sign up to request clarification or add additional context in comments.

2 Comments

See, I knew it was something simple, this is for a much much larger game code and its been bugging me the last few hours. Thank you
@Havamere My pleasure :)
0

You are reassigning the result of the prompt to your variable within your functions which then possibly miss conditions. Also, your functions don't return anything. They just create a global variables. Your editor should have a mild warning for assigning the argument to a variable with the same name. Although I advise against using prompt and alert you may want your code to look something like:

function promptRowExample(){
  var p = prompt('Please Choose a Row Between 0 and 5.');
  if(p < 0 || p > 5 || isNaN(p)){
    return promptRowExample();
  }
  return 'The Number '+p+' is a Valid Number!';
}
console.log(promptRowExample());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.