I want to stop if else chain from running do i use return?

I want to stop my script running if the number entered isn’t a number between 1 and 30, or isnt a number at all. I dont want the language prompt to appear.

//Prompt to ask for a number to translate
var val = prompt("What number do you want to translate?");
/*First determine if the input is a number or not, above 30, or below 1.
If it is not a number then close, if it is above 30 then close, if it is below 1 then close.
*/

 if(isNaN(val)) {
	alert ("Please input a number");
	}else if (val > 30) {
	alert ("Please input a number between 1 and 30");
    } else if (val < 1){
	alert ("Please input a number between 1 and 30");
	}

var language = prompt("What is the output language");

I think you can use the or pipe, instead of using two else if statements


Function myFunction (num){

If(num===NaN){
Prompt please enter a number}

Else if (val>30||val<1)
{Prompt  please enter a number less than 30 and greater than one
}
Return 
}
1 Like

Awesome.
How do I stop it from running if the input is outside of my parameters though :o

Do you have a link to the whole code?

That is all the code I have written :smile:

//Prompt to ask for a number to translate
var val = prompt("What number do you want to translate?");
/*First determine if the input is a number or not, above 30, or below 1.
If it is not a number then close, if it is above 30 then close, if it is below 1 then close.
*/

 if(isNaN(val)) {
	alert ("Please input a number");
	}else if (val>30||val<1) {
	alert ("Please input a number between 1 and 30");
	}

var language = prompt("What is the output language");

I edited what I had written above does that help?
I’m not sure exactly what you are trying to accomplish.
I think you’re trying to write a function, that stores a variable that the user inputs, is not a number and must be between 1 and 30?

Thanks for the advice. Sorry if I didn’t explain myself very well.

Yes you are right I am trying to set up a function. Our goal is to be able to translate an inputted number from 1 to 30 in either french or german. Here is my pseudo code which maybe explains it better:

Ask User ‘What number to translate’
IF user input is an integer
AND between ≥1 and 30≥
Continue to language selection
ELSE
IF not an integer show error message ‘Please use digits’ Close program
IF not between ≥1 and 30≥ show error message ‘Please type an integer number between 1 and 30’ Close program
Ask User ‘Please enter the language French / German’
IF user input is French or German
Continue to translation
ELSE
Show error message ‘Only French or German is allowed’ Close program
Show user translation
AND show message ‘Hit any key to exit’
IF user hits any key close program

Hi Happinessnz!

Does this do what you are looking for? You can return after your else if in numberPrompt() to escape before running langTranslateFunction() if the number doesn’t meet your requirements.

If your requirements are met your function for translating and returning the number runs.

JS

function numberPrompt(){
    let val = prompt("What number?");
    if(isNaN(val)){
    alert("please enter a number.");
    return;
    } else if(val>30||val<1){
    alert("please enter a number less than 30 & greater than 1.")
    return;
    }
    langTranslateFunction(val);
}

function langTranslateFunction(num){
    let language = prompt("What is the output language?");
    if(language.toLowerCase() === "german" || language.toLowerCase() === 'french') {
        const langCap = language.charAt(0).toUpperCase() + language.slice(1);
        alert(`Now input translate logic here for ${num} in ${langCap}`);
    } else {
        alert("Please choose French or German");
    }
}

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body onload="numberPrompt()">

</body>
<script src="scripts.js"></script>
</html>

Here is a fiddle for the code:
https://jsfiddle.net/conh3sa4/

1 Like

Thanks so much. I’ll work my way through it. Is it alright if I ask some questions later on?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Absolutely. I’m learning as well and won’t pretend I know a lot but I am happy to help as much as I can anytime :slight_smile:

Another approach:

function getInfo(question) {
  return prompt(question);
}

function numberPrompt() {
  const question = 'What number?';
  let num = getInfo(question);
  while (isNaN(num) || num  > 30 || num < 1) {
    alert("Please enter a number less than 30 & greater than 1.");
    num = getInfo(question);
  }
  langTranslateFunction(num);
}

function langTranslateFunction(num) {
  let language;
  while (!language) {
    language = getInfo('What is the output language?');
    if (language.toLowerCase() === "german" || language.toLowerCase() === 'french') {
      const langCap = language.charAt(0).toUpperCase() + language.slice(1);
      alert(`Now input translate logic here for ${num} in ${langCap}`);
    } else {
      language = null;
      alert("Please choose French or German");
    }
  }
}

Hi js buddy!

Please allow me to share with you a few steps I had made when I took the same challenge, and made it.

//Note: The intention of the code is to determine if an ‘entry’ is a number or not; if a number, does it fall within the valid range established?; else, prompt end-user that value entered is not a valid type.

//Step 1: Create a function declaration with an ‘entry’ parameter.

//Step 2: Use an if/else if/else chain statements (or, a chained conditional ‘ternary’ operator) to filter if an ‘entry’ is either one of three possibilities: 1) a valid number within range, 2) not a valid number - out of range, 3) not a number - type of data is not a number.

//Step 3: For each possibility, create a return value using a template literal for greater flexibility.

//Step 4: Test your code for each of those three possibilities. If your code is right, you shouldn’t have a problem with any possible type of data ‘entry’.

Happy Coding!

myQuote: “javaScript is sweeter the second time around!” @joMari

If I intend to use arrays to store the translations for the numbers 1 to 30 I will need to use two separate arrays right? One for german one for french. Would the best thing to do be to .shift the array so that the 0 is removed and then number can be used to determine the correct number is translated?
I need to put the arrays into a function for them to work right? Where would be the best place to put them?

I have written my code and seems to be working well but how do I return the translated result out of the function?

obviously

function () {
//mycode
return translatedResult
}

but how do i check it translatedResult outside of the function?

I am not understanding your question. What do you mean by “check it translatedResult outside of function”? Also, are you still updating the repl.it you posted above?

1 Like

</

function translate(number,lang) {
//Prompt to ask for a number to translate
var number = prompt("What number do you want to translate?");
/*First determine if the input is a number or not, below 1 pr above 30.
If it is not a number then close, if it is above 30 then close, if it is below 1 then close.
*/
 if(isNaN(number)) {
	alert ("Please input a number");
	return;
	}else if (number<1||number>30) {
	alert ("Please input a number between 1 and 30");
	return;
	}
var lang = prompt("What is the output language");

if (lang.toLowerCase() == "french") {
	alert(number + " in French is " + fren[number]);
	var translatedNumber = fren[number];
		alert("Press any key to exit.");
	}else if (lang.toLowerCase() == "german") {
		alert(number + " in German is "+ ger[number]);
	var translatedNumber = ger[number]
		alert("Press any key to exit.");
	} else {
	alert("Please input french or german");
	var translatedNumber = "incorrectInput"
	alert("Press any key to exit.");
	}
	return translatedNumber; //is this right?
}

is that last return going to bring the “translatedNumber” variable out of the function?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Yes, if you make a call to the function named translate, the value of translatedNumber will be returned, so you could access this value outside the function with something like:

var num = translate(30, 'french');  // num will be the same value that `translatedNumber` was before it was returned inside the `translate` function.

FYI - You only need one parameter for your translate function because number is assigned from using the prompt.

Hi HappinessNZ
I have a similar exercise and i am so confused, where did you put the code for the actual translation bit?