Switch with array and function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Switch with array and function

how can I create a function that find if each value in the array are divisible by 2 or 3 or 4 or 5. (using switch) https://code.sololearn.com/c62wZ1V1jNXy/?ref=app

21st Dec 2022, 11:16 PM
Hana Kallab
Hana Kallab - avatar
9 Answers
+ 2
switch statements requires constants in their casing however there is a work around by using a for loops and pre checking if it divisable by a index and use that index in the casing like my code below https://code.sololearn.com/cMBd435JMYQ5/?ref=app
21st Dec 2022, 11:43 PM
Vanessa Nilsson
+ 3
Thanks guys, I solved the problem and modified the code
22nd Dec 2022, 3:40 PM
Hana Kallab
Hana Kallab - avatar
+ 2
I didnt see functions part haha but you just copy paste that and return how you desire depeding on the divisable.
21st Dec 2022, 11:46 PM
Vanessa Nilsson
+ 2
Explain more about the problem
22nd Dec 2022, 8:55 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
what part is not working?
22nd Dec 2022, 10:36 AM
Bob_Li
Bob_Li - avatar
+ 1
it is easier if you are allowed to use libraries, the code becomes more flexible too. switch case is not necessary here and even makes the code rigid and hard to modify later on. https://code.sololearn.com/ctH6376oNB95/?ref=app
22nd Dec 2022, 2:12 PM
Bob_Li
Bob_Li - avatar
0
is switch really necessary or is it part of a lesson on switch usage? https://code.sololearn.com/c7qHP22nbLSc/?ref=app
22nd Dec 2022, 2:20 AM
Bob_Li
Bob_Li - avatar
0
not necessary but yes it is part of a lesson on switch usage Bob_Li
22nd Dec 2022, 4:05 AM
Hana Kallab
Hana Kallab - avatar
0
Bob_Li why it is not work in this code ?
22nd Dec 2022, 8:15 AM
Hana Kallab
Hana Kallab - avatar