Laravel Check All Multiple Requests Boolean Variable's value to True - request

I made an if condition for checking request boolean in laravel controller method update, it worked but the line seems too long and i want make the code in short line. And this is my code that i wrote:
if( $request->jamlak == 1
&& $request->kontrak == 1
&& $request->jamuk == 1
&& $request->sprin_pc == 1
&& $request->pc == 1
&& $request->izin_bekal == 1
&& $request->sprin_komisi == 1
&& $request->bek == 1
&& $request->komisi == 1
&& $request->bagudang== 1
&& $request->pem_gudang == 1
&& $request->bast == 1
&& $request->lpp == 1
&& $request->pemerataan == 1){
$validatedData['is_complete'] = 1;
}
else {
$validatedData['is_complete'] = 0;
}
Task::where('id', $task->id)
->update($validatedData);
return redirect('/admin/tasks')->with('success', 'New post has been updated!');
So about this code i want to make $validatedData['is_complete'] turns to be 1(true) after it checks all of requests 1 True, Is there any chance to make it way more efficient line?

Related

How to make sure that the randomized coordinates will not fall into specific coordinates in a 2D array? The language is C

I am creating a game in C, and I want to randomly place blocks and foods inside a 2d array. I have randomized the x and y coordinates of the blocks and the food into 1d arrays, i.e. foodCol[] and foodRow[]; blockCol[] and blockRow[]. However, there comes a possibility that the blocks and foods would fall into the same coordinates. I also want them not to to be placed on the coordinate (1, 1) and beside each other, except diagonally. I came up with this code, but it does not work and maybe there are other intuitive approach.
Here is the code:
for (foodCount = 0; foodCount < randDim; foodCount++){
for (foodCount2 = 0; foodCount2 < randDim; foodCount2++){
while ((foodCol[foodCount] == blockCol[foodCount2] && foodRow[foodCount] == blockRow[foodCount2]) ||
(foodCol[foodCount] == blockCol[foodCount2]+1 && foodRow[foodCount] == blockRow[foodCount2]) ||
(foodCol[foodCount] == blockCol[foodCount2]-1 && foodRow[foodCount] == blockRow[foodCount2]) ||
(foodCol[foodCount] == blockCol[foodCount2] && foodRow[foodCount] == blockRow[foodCount2]+1) ||
(foodCol[foodCount] == blockCol[foodCount2] && foodRow[foodCount] == blockRow[foodCount2]-1) ||
(foodRow[foodCount] == 1 && foodCol[foodCount] == 1) ||
(foodRow[foodCount] == 2 && foodCol[foodCount] == 1) ||
(foodRow[foodCount] == 1 && foodCol[foodCount] == 2) ||
(foodRow[foodCount] == 2 && foodCol[foodCount] == 2)){
foodNum2 = (rand()%9)+1;
shuffle(&foodCol[foodCount2], &foodCol[foodNum2]);
shuffle(&foodRow[foodCount2], &foodRow[foodNum2]);
}
}
}
Remarks: the shuffle function is just a basic function of shuffling 1d arrays. Also, the foodcount and randdim are irrelevant to my problem.
Thanks!

How to make conditional statements more compact in C?

I have a code snippet that uses if-else if-else block. I am wondering any potential ways to shorten the lengthy conditional statement else if (cardLength == 16) && (numberArray[0] == 5 && (numberArray[1] == 1 || numberArray[1] == 2 || numberArray[1] == 3 || numberArray[1] == 4 || numberArray[1] == 5)), for instance, without changing the logic. In python, I can do in this way: if (cardLength == 16) and (numberArray[0:2] in range(51,56)). Are there any specific syntax sugar that I can use fo this purpose in C?
if (cardLength == 15) &&
(numberArray[0] == 3 && (numberArray[1] == 4 || numberArray[1] == 7))
{
printf("AMEX\n");
}
else if (cardLength == 16) &&
(numberArray[0] == 5 && (numberArray[1] == 1 || numberArray[1] == 2 || numberArray[1] == 3 || numberArray[1] == 4 || numberArray[1] == 5))
{
printf("MASTERCARD\n");
}
else if (cardLength == 13) && (numberArray[0] == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
Put it into a (static) function. Dont think about performance/optimisation yet.
static char *card_leng_type2string( unsigned len, int *arr)
{
if (len == 15 && arr[0] == 3 && arr[1] == 4 ) return "AMEX";
if (len == 15 && arr[0] == 3 && arr[1] == 7 ) return "AMEX";
if (len == 16 && arr[0] == 5 && arr[1] == 1 ) return "MASTERCARD";
if (len == 16 && arr[0] == 5 && arr[1] == 2 ) return "MASTERCARD";
if (len == 16 && arr[0] == 5 && arr[1] == 3 ) return "MASTERCARD";
if (len == 16 && arr[0] == 5 && arr[1] == 4 ) return "MASTERCARD";
if (len == 16 && arr[0] == 5 && arr[1] == 5 ) return "MASTERCARD";
if (len == 13 && arr[0] == 4) return "VISA";
return "INVALID";
}
Now your calling code could just do:
printf("%s\n", card_leng_type2string(cardLength, numberArray) );
You could convert the first two digits of the numberArray into a new number like this
num = numberArray[0]*10 + numberArray[1]
and then use that into the conditional statements to make them more readable
int num = numberArray[0]*10 + numberArray[1]
if ((cardLength == 15) && ((num == 37) || (num == 34)))
{
printf("AMEX\n");
}
else if ((cardLength == 16) && ((num >= 51) && (num <= 55)))
{
printf("MASTERCARD\n");
}
else if ((cardLength == 13) && ((num >= 40) && (num <= 49)))
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
return 0;
}
Assuming that entries in numberArray are in range 0-9, you could use strchr function. This returns non-NULL if a given string contains a specific character.
Replace:
numberArray[1] == 4 || numberArray[1] == 7 || numberArray[1] == 9
with
strchr("479", '0' + numberArray[1])
If numberArray was an array of character then the check could be simplified to strchr("479", numberArray[1])
If you want the logic intact and only want modification for readbility, you could try adding a preprocessor directive gloabally.
This would replace the text or inline function anywhere you use it in the program.
Example
#define AMEX_CONDITION (cardLength == 15) && (numberArray[0] == 3 && (numberArray[1] == 4 || numberArray[1] == 7))
and use it in your if as
if(AMEX_CONDITION){
printf("AMEX");
}

C Operator priority at CLA certification exam

Currently, I'm taking C language certification course at cppinstitute.org.
In one of it's quizzes there is a question as below to recognize output.
int i = 1,j= 1;
int w1,w2;
w1 = (i>0) && (j<0) || (i<0) &&(j>0);
w2 = (i<=0) || (j>=0) && (i>=0) || (j<=0);
printf("%d",w1 == w2);
I think the program should print 0 to the screen, but the quiz accepts printing 1 as the answer.
Am I correct?if not ,where I'm wrong?
Thanks in advance!I'm a beginner.
Here, && higher precedence than || operator.
So,
w1 = (i>0) && (j<0) || (i<0) &&(j>0);
= 1 && 0 || 0 && 1;
= 0 || 0
= 0
And
w2 = (i<=0) || (j>=0) && (i>=0) || (j<=0);
= 0 || 1 && 1 || 0
= 0 || 1 || 0
= 1 || 0
= 1
So, w1 == w2 become false. So, correct output is 0.

If else statement troubles in C

I am trying to input a seat number of "15" into this function and get the char value of 'A'. However, for some reason every time I input a number that should be a type 'A'(because its remainder doesn't equal any of the aforementioned values) it gets stuck in the 'M' else if statement. I really do not understand why and would like some help if you have time :)
char whatTypeOfSeat(int seatNumber){
if((seatNumber % 6) == 0 || seatNumber % 6 == 1 || seatNumber == 1) {
typeOfSeat = 'W';
}
else if((seatNumber % 6) == 2 || (seatNumber % 6) == 5|| seatNumber == 5,2 ) {
typeOfSeat = 'M';
}
else {
typeOfSeat = 'A';
}
return typeOfSeat;
}
This does not do what you think it does:
seatNumber == 5,2
If you want to check against both values, you need separate conditionals
else if((seatNumber % 6) == 2 || (seatNumber % 6) == 5|| seatNumber == 5 || seatNumber == 2 ) {

What is the order in a for loop? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm trying to solve Problem 5 of project euler and I the answer I keep getting is wrong:
#include <stdio.h>
main()
{
int num;
int x = 0;
for (num = 20; x == 0; num++)
{
if ((num%1) == 0 && (num%2) == 0 && (num%3) == 0 && (num%4) == 0 && (num%5) == 0 && (num%6) == 0 && (num%7) == 0 && (num%8) == 0 && (num%9) == 0 && (num%10) == 0 && (num%11) == 0 && (num%12) == 0 && (num%13) == 0 && (num%14) == 0 && (num%15) == 0 && (num%16) == 0 && (num%17) == 0 && (num%18) == 0 && (num%19) == 0 && (num%20) == 0)
x = 1;
}
printf("%d %d", num, x);
}
My program keeps printing out 232792561 (I am aware that I'm printing x, this is simply for troubleshooting purposes).
The verbatim output I'm getting is: 232792561 1.
I did some research and I found that the correct answer to the problem is 232792560.
I am now beginning to believe that the problem lies in the for loop.
What does the loop do first, the iteration (num++) or the test (x == 0)?
A for loop can be converted to an equivalent while loop:
for (num = 20; x == 0; num++) {
// do stuff
}
is the same as
num = 20;
while (x == 0) {
// do stuff, then
num++;
}
So first the condition is checked, then the loop body is executed, then the increment.
(And yes, as others suggested, if you break; out of the loop when you need, you'll need the correct result, since break; immediately jumps out of the loop, thus the incrementing statement isn't executed for the last time.)
After the loop body has been executed (if at all, because first the initialisation code is run, then the condition checked to see whether the body is entered),
first the update code is run
then the condition is checked.
So after you set x to 1, num is incremented once more.
Instead of setting x to 1 to end the loop, you could simply break;, that would exit the loop without running the update code.
Your loop is needlessly complex and can be simplified using the while loop construct if you prefer. You can also get rid of the unnecessary variable x
int main() {
int num = 20;
while (!((num%1) == 0 && (num%2) == 0 && (num%3) == 0 && (num%4) == 0 && (num%5) == 0 && (num%6) == 0 && (num%7) == 0 && (num%8) == 0 && (num%9) == 0 && (num%10) == 0 && (num%11) == 0 && (num%12) == 0 && (num%13) == 0 && (num%14) == 0 && (num%15) == 0 && (num%16) == 0 && (num%17) == 0 && (num%18) == 0 && (num%19) == 0 && (num%20) == 0))
{
++num;
}
printf("%d\n", num);
return 0;
}
To answer why your for loop was giving incorrect answer:
Although you were setting x = 1 for the correct value of num, you were checking the condition of the for loop ONLY in the next iteration of the loop (i.e. after the num++ statement is executed), and hence your value of num was offset by 1.
As many people suggested, you could use the break statement to terminate the for loop execution so that the value of num is what you want it to be when the control reaches outside the loop.
Step 1: Initialization (num = 20)
Step 2: Test
Step 3: Iteration
Step 4: Test
Step 5: Iteration
and so on.
If I were you, I would choose while loop with condition as yours (I mean the long one inside for loop) with body of incrementing.

Resources