Reduce JavaScript if statement - javascript

I need help, I have this function in javascript:
function getPosition(elementToFind, array) {
var i;
for (i = 0; i < array.length; i += 1) {
if (array == elementToFind) {
return i;
}
}
}
And then I have a cycle if that I would like to reduce:
if (
getPosition(1, arraySomething) == 0 &&
getPosition(2, arraySomething) == 1 &&
getPosition(3, arraySomething) == 2 &&
getPosition(4, arraySomething) == 3 &&
getPosition(5, arraySomething) == 4 &&
getPosition(6, arraySomething) == 5 &&
getPosition(7, arraySomething) == 6 &&
getPosition(8, arraySomething) == 7 &&
getPosition(9, arraySomething) == 8 &&
getPosition(10, arraySomething) == 9 &&
getPosition(11, arraySomething) == 10
) {
...code
}
How can I do it?

How about something like this
var isTrue = true;
for(var n = 1; n <= length; n ++){
if (getPosition(n, arraySomething) !== (n - 1)){
isTrue = false;
break;
}
}
if (isTrue){
...
}else{
...
}
?

Related

Array.prototype.filter() expects a value to be returned at the end of arrow function array-callback-return

I've seen similar questions here, but I can't quite figure out how to silence this error for my case with several conditions involved.
This is the .filter() object:
orders.filter((order) => {
if (filterId === 0) {
return order.status === 0 && order.type <= 3;
} else if (filterId === 1) {
return order.status === 0 && order.type === 0;
} else if (filterId === 2) {
return order.status === 0 && order.type === 1;
} else if (filterId === 3) {
return order.status === 0 && order.type === 2;
}
}).map(...);
consider switch your approach to switch instead chain of if else.
orders.filter((order) => {
let value = 0;
switch(filtedId){
case 0:
value = order.status === 0 && order.type <= 3;
case 1:
value = order.status === 0 && order.type === 0;
case 2:
value = order.status === 0 && order.type === 1;
case 3:
value =order.status === 0 && order.type === 2;
default:
value = 0
}
return value
}).map(...);

Why doesn't && in one if state work with this problem?

I first tried writing this code using one if-statement because I thought that it made sense since the index has to be even and the element has to be even and same with odd numbers and index, but it did not work. This code does not work:
1. function isSpecialArray(arr) {
//iterate through array
for(var i = 0; i < arr.length; i++){
if((i % 2 === 0 && arr[i] % 2 !== 0) && (i % 2 === 1 && arr[i] % 2 !== 1)){
return false;
}
}
return true;
}
console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3]));
but, works with this code:
2.
function isSpecialArray(arr) {
//iterate through array
for(var i = 0; i < arr.length; i++){
// console.log(arr[i] % 2 === 0 && i % 2 !== 0);
if(i % 2 === 0 ){
if(arr[i] % 2 !== 0){
return false
}
}
if(i % 2 == 1){
if(arr[i] % 2 !== 1){
return false;
}
}
}
return true;
}
Your first code is carrying out a different sort of logic than your second. Spelling out your second:
function isSpecialArray(arr) {
//iterate through array
for (var i = 0; i < arr.length; i++) {
// first section
if (i % 2 === 0) {
if (arr[i] % 2 !== 0) {
return false
}
}
// second section
if (i % 2 == 1) {
if (arr[i] % 2 !== 1) {
return false;
}
}
}
return true;
}
On each iteration:
In the first section, you check if the index and the element are even. If so, you return false. Otherwise, you check:
In the second section, you check if the index and the element are odd. If so, you return false.
It's essentially OR logic there. So you should put || in your first code instead of &&:
if((i % 2 === 0 && arr[i] % 2 !== 0) && (i % 2 === 1 && arr[i] % 2 !== 1)){
should be
if((i % 2 === 0 && arr[i] % 2 !== 0) || (i % 2 === 1 && arr[i] % 2 !== 1)){
// ^^
Your existing first code checks if the index is even and the value there is odd and that the index is odd and that the value there is even - which can never be true.
1. function isSpecialArray(arr) {
//iterate through array
for(var i = 0; i < arr.length; i++){
// the error was that you shouldve aded an OR between the two statements not an AND
if((i % 2 === 0 && arr[i] % 2 !== 0) || (i % 2 === 1 && arr[i] % 2 !== 1)){
return false;
}
}
return true;
}
console.log(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3]));

expected '0th' to equal '0'

function numberToOrdinal(i) {
var j = i % 10,
k = i % 100;
if (j == 0 && k == 100) {
return '0th';
}
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}
It's not passing this test, what's wrong?
should handle single digits
expected '0th' to equal '0'
if you gonna devide a unknown input you wanna check if the input given might be zero. Else there might be an devisionbyzero exception thrown.
function numberToOrdinal(i) {
if(i === 0){ // since we devide i we wanna check if it might be zero.
return i ;
}
var j = i % 10,
k = i % 100;
//if (j == 0 && k == 100) { didnt seem to be doing anything
//return '0th';
//}
if (j == 1 && k != 11) {
return i + "st";
}
if (j == 2 && k != 12) {
return i + "nd";
}
if (j == 3 && k != 13) {
return i + "rd";
}
return i + "th";
}
console.log(numberToOrdinal(0));
you could just change these lines
if (j == 0 && k == 100) {
return '0th';
}
to be
if (i == 0) {
return i
}
If you want your function to return 0 when 0 is passed to the function, you will need to enter a condition to handle that, such as:
if (i === 0) {
return 0;
}
You have to enter another condition to handle it
function numberToOrdinal(i) {
var j = i % 10,
k = i % 100;
if (i === 0) {
return i;
}
if (j == 1 && k != 11) {
return `${i}st`;
}
if (j == 2 && k != 12) {
return `${i}nd`;
}
if (j == 3 && k != 13) {
return `${i}rd`;
}
return `${i}th`;
}

Unexpected token, expected :

I want to work with the ternary-operator but I get the following error message:
" Unexpected token, expected : "
Why is that?
This is my first code:
const GetUp = (num) => {
for (let i = 1; i <= num; i++) {
if (i % 3 === 0) {
console.log('Get')
}
if (i % 5 === 0) {
console.log('Up')
}
if (i % 3 === 0 && i % 5 === 0) {
console.log('GetUp')
} else {
console.log(i)
}
}
}
GetUp(200)
This is my recent code:
const SetRuc = (num) => {
for (let i = 1; i <= num; i++) {
(i % 3 === 0) ? console.log('Set')
(i % 5 === 0) ? console.log('Ruc')
(i % 3 === 0 && i % 5 === 0) ? console.log('SetRuc') : console.log(i)
}
}
SetRuc(100)
use && for shothand if without else
add semicolumns ; to let it know that it's the end of the instruction, otherwise it will evaluate the three lines as one instruction.
const SetRuc = (num) => {
for (let i = 1; i <= num; i++) {
(i % 3 === 0) && console.log('Set');
(i % 5 === 0) && console.log('Ruc');
(i % 3 === 0 && i % 5 === 0) ? console.log('SetRuc') : console.log(i);
}
}
SetRuc(100)
EG this:
(i % 3 === 0) ? console.log('Set')
provides no : option for the ?. If you don't want anything to happen in the event that the ? check is false, you can simply provide an empty object, or undefined:
(i % 3 === 0) ? console.log('Set') : {}
If you don't want to do anything in case of false result in ternary operator. you can just say something like statement ? 'expression' : null
just mention null in there. Something like
const SetRuc = (num) => {
for (let i = 1; i <= num; i++) {
(i % 3 === 0) ? console.log('Set') : null;
(i % 5 === 0) ? console.log('Ruc') : null;
(i % 3 === 0 && i % 5 === 0) ? console.log('SetRuc') : console.log(i);
}
}
SetRuc(100)
You misuse the ternary operator, the syntax is:
condition ? expr1 : expr1
Assuming that expr1 will be executed if the condition is true, otherwise expr2 will.
So you may want this:
const SetRuc = (num) => {
for (let i = 1; i <= num; i++) {
(i % 3 === 0) ? console.log('Set') :
(i % 5 === 0) ? console.log('Ruc') :
(i % 3 === 0 && i % 5 === 0) ? console.log('SetRuc') : console.log(i)
}
}
SetRuc(100)
const SetRuc = (num) => {
for (let i = 1; i <= num; i++) {
(i % 3 === 0) ? console.log('Set') :
(i % 5 === 0) ? console.log('Ruc') :
(i % 3 === 0 && i % 5 === 0) ? console.log('SetRuc') : console.log(i)
}
}
SetRuc(100)
you have missed : after console.log('Set') and console.log('Ruc')

Else/If Loop JavaScript

I'm really novice to all of this, and I'm learning it in class. My assignment is to write an Else/If Loop where we display 1-100. If the integer is divisible by 3 display "play", if divisible by 4 display "ball", and if divisible by 3 and 4 display "Play Ball", anything else is just the integer.
I have my code working here, but I can't get my 3 and 4 to display "Play Ball", unless I run it by itself. Hope that makes sense, here's what I have:
for (var i = 1; i <= 100; i++) {
if (i % 3 === 0) {
console.log('Play');
} else if (i % 4 === 0) {
console.log('Ball');
} else if (i % 3 === 0 && i % 4 === 0) {
console.log('Play Ball');
} else {
console.log(i);
}
}
You just need to move thisi % 3 === 0 && i % 4 === 0 condition to the top:
for (var i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 4 === 0) {
console.log('Play Ball');
} else if (i % 3 === 0) {
console.log('Play');
} else if (i % 4 === 0) {
console.log('Ball');
} else {
console.log(i);
}
}
Since the first if statement will always be true when the third statement (with the &&) will be true, you'll never make it to the third statement. Try reordering as such:
for (var i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 4 === 0) {
console.log('Play Ball');
} else if (i % 4 === 0) {
console.log('Ball');
} else if (i % 3 === 0) {
console.log('Play');
} else {
console.log(i);
}
}
You can move the check for divisible by both 3 and 4 to the top:
for (var i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 4 === 0) {
console.log('Play Ball');
} else if (i % 3 === 0) {
console.log('Play');
} else if (i % 4 === 0) {
console.log('Ball');
} else {
console.log(i);
}
}
3 and 4 display
Above statement means, when BOTH condition satisfies then only you need to perform some task. In this scenario you need to use &&
if ( (i % 3 === 0) && (i % 4 === 0) ) {
console.log('Play Ball');
}
When to use else and/or else if
Let's take an example, you have a number, which is not divisible by 3 and 4 and you are not concern about that number. Then, you simply use else to fall all those category into it.
Now, there is another scenario, when you have entered number, which is again not divisible by 3 and 4. But, you might want see, if it is only divisible by 3 or 4. Then, you use else if where you can put the condition to check.
if ( (i % 3 === 0) && (i % 4 === 0) ) {
console.log('Play Ball');
}
else if ( i % 3 === 0) {
console.log('Play');
}
else if ( i % 4 === 0) {
console.log('Ball');
}

Categories