+ 2
Please someone check this code for me and tell me what's wrong?
function exercice1(chiffre) { (chiffre === 0) ? 'zéro': (chiffre ===1) ? 'un': (chiffre === 2) ? 'deux'; }
9 odpowiedzi
+ 10
Either use return keyword with if else statement or use turnery operator .
Ex. If (condition)
{ return value ;}
Else{return value;}
Or
If(condition)? return value if true :return value if false;
+ 5
You have to add return for a function in order to get the returned value
function exercice1(chiffre) {
return (chiffre === 0) ? 'zero':
(chiffre === 1) ? 'un':
(chiffre === 2) ? 'deux':
null; // null for chiffre >2
}
console.log(exercice1(0))
console.log(exercice1(1))
console.log(exercice1(2))
https://code.sololearn.com/WfeqfY9oIli1/?ref=app
+ 3
In Es6:
const exercice1 = chiffre =>
(chiffre === 0) ?'zéro'
:(chiffre === 1) ?'un'
:(chiffre === 2) ?'deux'
:"???";
for(let i=0; i<=3; i++)
console.log(exercice1(i));
+ 2
If you are checking for only three conditions; then remove the last tenary operator. Something like this 👇;
function exercice1(chiffre) {
(chiffre === 0) ? 'zéro':
(chiffre ===1) ? 'un': 'deux';
}
The last line (chiffre===2)?'deux';
must have two values 'deux' : ... ;
whereas you provided only one
Or you can simply create an array like this 👇
var numbre =["zero", "une", "deux",....];
and in the function; maybe you are returning the number in words; you do
return numbre[chiffre];
+ 2
Solo challenger
+ 2
Calviղ ☺
+ 1
Add a return value Mamadou Aliou Diallo also you can join our group if you want to see full explaination of the above code
0
Will some tell me how to cancel this cause I don't know what the heck this is. I upload by mistake. I need to cancel. Tank you.