+ 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'; }

5th Sep 2019, 12:16 AM
Mamadou Aliou Diallo
Mamadou Aliou Diallo - avatar
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;
5th Sep 2019, 4:20 PM
Ayushi Gujarati
Ayushi Gujarati - avatar
+ 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
5th Sep 2019, 12:37 AM
Calviղ
Calviղ - avatar
+ 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));
5th Sep 2019, 2:18 AM
Calviղ
Calviղ - avatar
+ 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];
5th Sep 2019, 12:20 AM
Baribor Saturday
Baribor Saturday - avatar
+ 2
Solo challenger
5th Sep 2019, 12:49 AM
George S Mulbah II
George S Mulbah II - avatar
+ 2
5th Sep 2019, 3:03 AM
Solo
Solo - avatar
+ 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
5th Sep 2019, 12:49 AM
George S Mulbah II
George S Mulbah II - avatar
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.
6th Sep 2019, 10:30 PM
Estella Gandar