Home > other >  If statement do not work with modulo. Can't get the console.log() to write out anything. I am u
If statement do not work with modulo. Can't get the console.log() to write out anything. I am u

Time:10-13

My task is like this:

You will see the array 'numbers'. Use a for loop to go through that array. For each element in the array, print the number to the console using console.log. If the number is divisible by 3, instead of the number print the string 'apple' to the console. Should the number be divisible by 5, print the string 'cake' to the console instead of the number.

In the first task, I used a for loop to show every number from 1-20, so I can't seem to understand why I can't get this to work. I am maybe thinking I have to implement a new for loop in this task too.

My code is like this:

console.log('PART 3')
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

if (numbers % 3){
    console.log('eple');
}

else if(numbers % 5){
    console.log('kake');
}

CodePudding user response:

You cannot use modulo (Reminder Operator %) on an array.
You're missing a for loop (as stated by the task requirement) in order to iterate over its values:

console.log('PART 3')
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

for (let number of numbers) {
  if (number % 3){
      console.log(number   ' eple');
  }

  else if(number % 5){
      console.log(number   ' kake');
  }
}

CodePudding user response:

In order to find out if a number is a factor of another number:

number % 3 === 0

That translates as: If a number divided by 3 has a remainder of 0...

You don't even have a for loop

for (let i=0; i < array.length; i  ) {...}

There are 3 expressions of a for loop statement

Expression Example Description
Initial
let i=0
Define variable at the beginning
Condition
i < array.length
When variable no longer less than the quantity of elements within the array stop loop
Increment
i  
Variable increments by 1

Flow control statements can be only in a certain order:

if (a > b) {...}
// OR
if (a > b) {...}
else {...}
// OR
if (a > b) {...}
else if (a === b) {...}
// ...more else if(...){...} if required
else {...}

Never do this

if (a > b) {...}
else if (a === b) {...}

// This will create an array of 1 - 20 programatically
const array = [...Array(20)].map((_, i) => i   1);
console.log(JSON.stringify(array));

for (let i = 0; i < array.length; i  ) {
  let number = array[i];
  if (number % 3 === 0) {
    console.log("APPLE");
  } else if (number % 5 === 0) {
    console.log("CAKE");
  } else {
    console.log(number);
  }
}

CodePudding user response:

console.log('PART 3')
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
if ((numbers.length-4)%3){
    console.log('eple');
}

else if((numbers.length-4)%5){
    console.log('kake');
}

i don't know if this will answer your question but just maybe something for you to work with. cheers

  • Related