Hi, i wonder how to tell in while() to keep doing while the variable is a string. I mean how do i say that the variable must be a string to keep “doing” ? If it is clear Thank you
This is how you would do it, but better don’t do it:
while(typeof arg === 'string') {
// this is very likely an infinite loop
}
You should rather do a typecheck inside the while loop like this:
const arr = [0, 'World!', 1, 'Hello'];
const stringArr = []
while(arr.length) {
const item = arr.pop();
if(typeof item === 'string') {
stringArr.push(item);
}
}
console.log(stringArr.join(' ')) // "Hello World!"
First thank you for all the instructions i’m not yet familiar with all the aspects of this code but it’ll give me some thing to work on :).
Aannd my mistake, i was talking about do{} / while ()…
In my case (which i erased sorry) i make a prompt in the do{} and in the while() i want to keep doing the prompt until it’s a number. So is there a keyword to put in while () to represent any strings ? Maybe NaN ?
Thanks again
It does not make much of a difference if you use a do while loop or a while loop.
Don’t do … while
do {
// this is very likely an infinite loop
}
while(typeof arg === 'string')
Do … while
const arr = [0, 'World!', 1, 'Hello'];
const stringArr = []
do {
const item = arr.pop();
if(typeof item === 'string') {
stringArr.push(item);
}
}
while(arr.length)
console.log(stringArr.join(' ')) // "Hello World!"
Haha not sure i understand all this code yet ^^ I’ll work on it but thank you for the answer.
Do you have pseudocode or a first approch to demonstrate what it actually is? I mean, how will it become a number?
I think it was this one :
var choice = Number(prompt("What do you want to do ?\n\n 1 - Addition\n 2 - Multiplication\n 3 - Subtraction\n 4 - Division\n"));
} while(choice != 1 && choice != 2 && choice != 3 && choice != 4)
Your variable choice holds a single value. Therefore no loop (neither while nor do … while) is needed. You can check for the type with a simple if clause.
Your variable choice is never going to be a string because you convert it to Number. If the input cannot be converted, Number() returns NaN. So there is no need to check if choice is of type string.
What do really want the user to deliver as an input? You‘re currently asking them for a string like „Addition“, where do you want to get the numbers from?
Can you describe in words, no code, what you want the program to do? From this code I can‘t really tell. Do you want the promt to be repeated until you get the desired input?
My bad i gave you the corrected answer.
The initial code was this one. It’s for a calculator. To be honest i don’t really remember exactly why i’ve asked in the beginning… but i’ve found the answer as you gave me yours
function addition(numberA, numberB) {
return numberA + numberB;
}
function multiplication(numberA, numberB) {
return numberA * numberB;
}
function subtraction(numberA, numberB) {
return numberA - numberB;
}
function division(numberA, numberB) {
if(numberB == 0) {
throw new Error("Can't divide by 0.");
}
return numberA / numberB;
}
do {
var choice = prompt("What do you want to do ?\n\n 1 - Addition\n 2 - Multiplication\n 3 - Subtraction\n 4 - Division\n"));
} while(choice != 1, 2, 3, 4)
do {
var firstNumber = Number(prompt("Type first number :"));
var secondNumber = Number(prompt("Type second number : "));
} while(isNaN(firstNumber) || isNaN(secondNumber))
try{
switch (choice) {
case 1:
var result = addition(firstNumber, secondNumber);
break;
case 2:
var result = multiplication(firstNumber, secondNumber);
break;
case 3:
var result = subtraction(firstNumber, secondNumber);
break;
case 4:
var result = division(firstNumber, secondNumber);
break;
default:
throw new Error("An error has occurred.");
}
alert("Outcome : " + result);
}
catch(error) {
alert(error);
}