+ 2

Optimization needed

If you had any idea how to optimise this code or logic gate The switch statement You can share https://code.sololearn.com/WHGAevI76h85/?ref=app https://code.sololearn.com/WHGAevI76h85/?ref=app

27th Jun 2022, 7:16 AM
Shubh
Shubh - avatar
13 ответов
+ 1
function code(bool1, bool2, bool3) { let bools = [bool1, bool2, bool3] console.log( bools) let execute = 6, result="" let mode = bool1? 0: bool2? 1: bool3? 2: 0 for(var x=0; x < execute; x++) { mode = nextMode( mode, bools) result += mode +"," } console.log( result) } function nextMode( mode, bools) { let i = mode do { i = nextWrap( i, bools.length) } while (! bools[i] && i != mode) return i } function nextWrap(num, over) { if (++num == over) num = 0 return num }
29th Jun 2022, 6:29 AM
zemiak
+ 2
Emerson Prado bro what am asking isn't related to any course of this application Am working on a game engine which used javascript as coding language N am not that great in js N have to finish the module till 12th of july So that's why Askin help for some really complicated problems ain't that bad ^^ thx btw
29th Jun 2022, 12:53 PM
Shubh
Shubh - avatar
+ 2
zemiak yea it's a game code, behaviour of guns Just trying to make an all in one script to simulate any kind of gun including animations/mechanics That above part was the selective fire of a gun.. Thank you for the soln. It helped a lot
29th Jun 2022, 12:57 PM
Shubh
Shubh - avatar
+ 2
Shubh It doesn't need to be related to SoloLearn. Just related to learning how to code. Since you have a short deadline, why don't you try my hints?
29th Jun 2022, 2:28 PM
Emerson Prado
Emerson Prado - avatar
+ 1
1. Change the outer switch statements to if blocks (I'm surprised you used switch here...) 2. Change the inner switch statements to arithmetic operations
27th Jun 2022, 5:46 PM
Emerson Prado
Emerson Prado - avatar
+ 1
There's no sense in testing if true is true, no matter if testing with if or switch. But notice you test, in reality, the 3 booleans. So just test them in the if and elif statements. switch(true) ; case(condition) is redundant.
27th Jun 2022, 6:12 PM
Emerson Prado
Emerson Prado - avatar
+ 1
The inner switch statements just change an int value, in a very simple way. For example, the first one just increases it, rolling back to zero when it reaches 3. How easy this could be done with addition and modulo? I bet you can do everything with addition, subtraction and modulo.
27th Jun 2022, 6:17 PM
Emerson Prado
Emerson Prado - avatar
+ 1
check by this code( true, true, true) code( true, true, false) code( true, false, true) code( true, false, false) code( false, true, true) code( false, true, false) code( false, false, true) code( false, false, false)
29th Jun 2022, 6:30 AM
zemiak
+ 1
zemiak Pls don't give finished code as answer. This is a learning app, and solving the problem is part of the process. Prefer giving hints for the OP to find the solution.
29th Jun 2022, 9:47 AM
Emerson Prado
Emerson Prado - avatar
+ 1
I agree but it depends.. from other thread it seems as partial code of a game not unfinished exercise, he has his own solution Comparing solutions is also good for learning, I also learn something at this problem and can share it, Look at javaprogrammingforums.com, is dead because moderators blocks help from others progranmers
29th Jun 2022, 10:07 AM
zemiak
0
If true? I tried making some logic gates with each case but the work got really complicated so shifted these simple flow of logic gates
27th Jun 2022, 5:51 PM
Shubh
Shubh - avatar
0
Can you please tell me a example of arithmetic eq.
27th Jun 2022, 5:52 PM
Shubh
Shubh - avatar
1st Jul 2022, 2:41 AM
Shubh
Shubh - avatar