If someone could help me, I would really appreciate it... I've had this problem for a few days: I have a function called translate like this, with which I want to translate some numbers into a word, see:
function translate(Num){
switch(Num){
case 0: document.write('primer');
break;
case 1: document.write('segundo');
break;
case 2: document.write('tercer');
break;
case 3: document.write('cuarto');
break;
case 4: document.write('quinto');
break;
}
}
And I have this other function, where I want the user to enter numbers to collect them into a vector. Here I use the translate function to translate the subscript of the for loop according to the place of each number entered, see:
function askNums(Nums){
var i;
for(i = 0; i < Nums.length; i++){
var NumsVector;
NumsVector = parseInt(prompt(' Por favor ingrese el '+translate(i)+' número: ',' '));
Nums[i] = NumsVector;
}
}
When executing this code:
var Nums;
Nums = new Array(5);
askNums(Nums);
There is no first, second, third, etc. which would be the execution of the translate function ... Have I done something wrong?
In your case, what you are trying to do with the function
translate
is to return a string that can be concatenated with the string of theprompt
.To do this,
translate
you should not do adocument.write
, but return the string .I hope it works.