I am pretty new at this stuff, and I am solving a series of questions, but I've got lost in this one.
I have to verify if a number can be divided by other, and the answer must be true or false.
I got his
function solucao(numero, x) {
solucao(numero % x);
if (solucao === 0) {
resultado = true;
}else{
resultado = false;
}
}
But I'm getting runtime error and can't see whats is missing.
I am pretty new at this stuff, and I am solving a series of questions, but I've got lost in this one.
I have to verify if a number can be divided by other, and the answer must be true or false.
I got his
function solucao(numero, x) {
solucao(numero % x);
if (solucao === 0) {
resultado = true;
}else{
resultado = false;
}
}
But I'm getting runtime error and can't see whats is missing.
return !(numero % x);
It is that simple, nothing else is required. The reason this works is because 0
is a "falsy" value while any other number is a "truthy" value.
– Randy Casburn
Commented
Feb 21, 2021 at 17:34
So you want to check if a number numero
is divisible by x
. The modulo operator can help. Try this:
function solucao(numero, x){
if (numero % x == 0){
return true
}
else {
return false
}
}
function solucao(numero, x) {
let resultado;
if (numero % x === 0) {
resultado = true;
}else{
resultado = false;
}
return resultado;
}
I think you get confused at some point. You are calling the function, inside of itself. You should do like this, and also, declare the result variable.
I am sure this will help:
function checkIfDivided(){
// in this section the variables e from an html document
var number=parseInt(document.getElementById("number").value);
var divisor=parseInt(document.getElementById("divisor").value);
if(number%divisor==0)
return true;
else return false;
}
or
function checkIfDivided(number,divisor){
//in the function the variable are given as parameters
if(number%divisor==0)
return true;
else return false;
}
Looks like two things to me:
You haven't declared your 'resultado' variable ( this can be as simple as just typing 'let resultado;' without the single quotes
You haven't returned your 'resultado' variable after the if/else statement
Right now, your function is using an undeclared variable and not returning anything, so that is why you are getting an error. Fix the two above steps and you should be good! :)
You clearly understand that the modulus operator is the way to go. Using it we discover that 12
is divisible by 3
because 12 % 3
return zero. Zero is considered a "falsy" value while any other number is considered "truthy".
Given this then if 12 % 3
returns a "falsey" value (zero) we can't use the result directly. But what if we can "flip" false to true? We can, using the not
operator (!
).
Using the !
operator on the result of a math problem requires the use of parentheses around the math problem itself.
So the problem in code bees (12 % 3)
and to 'flip' it with the !
operator it bees
!(12 % 3)
.
This is proven below with:
console.log(!(12 % 3))
--> logs trueconsole.log(!(12 % 5))
--> logs falseThe function implementation of that is simple and also proven:
console.log(isDivisible(12,3));
--> logs trueconsole.log(isDivisible(12,5));
--> logs false
console.log(!(12 % 3))
console.log(!(12 % 5))
function isDivisible(number, x){
return !(number % x);
}
console.log(isDivisible(12,3));
console.log(isDivisible(12,5));
There is one other way to do so and i think its much cleaner.
console.log(Number.isInteger(10/2)) //true
console.log(Number.isInteger(4/2)) // false
//a must be greater than b
function check(a,b) {
console.log(Number.isInteger(a/b))
return Number.isInteger(a/b)
}
check(10,5)//true
check(8,3)//false
The error came because of the recursion ing from line solucao(numero % x);
. Since you already used the variable name "solucao", you could try assigning the modulus operation result to a different variable, for example: const solution = numero % x
. Next, we should declare variable resultado that could look like this:
let resultado;
// continue with your code
if (solution === 0) {
resultado = true;
} else {
resultado = false;
}
After that you also need to return the value of resultado, for example like that: return resultado;
Your full code could look like that:
function solucao(numero, x) {
const solution = numero % x;
let resultado;
if (solution === 0) {
resultado = true;
} else {
resultado = false;
}
return resultado;
}
Since the operation of modulus if a number is divisible returns 0, you could also return a shorter version that only checks if the result equals 0:
function solucao(numero, x) {
return numero % x === 0
}