Writing recursive function to identify even/odd [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am working an assignment for Eloquent JavaScript asking you to write a recursive rule to test for 'evenness' of a number. I am always getting false as a return. Please someone help identify my error.
function isEven(num){
if(num===0){
return(true);
}else if (num === 1){
return(false);
}else {
num = num - 2;
return(find(num));
}
}
};
console.log(isEven(12));

The whole point of recursion if to call the function within itself. You are really close, but you call find in your function rather than isEven(num - 2)
function isEven(num){
if(num===0){
return(true);
}else if (num === 1){
return(false);
}else {
return(isEven(num - 2));
}
}
console.log(isEven(12));

function isEven(n) {
if (n === 0) return true;
if (n === 1) return false;
return isEven(n - 2);
}
However, it's very inefficient. Better to use something like this (e.g. in a real code):
function isEven(n) {
return n % 2 === 0;
}

following will solve your problem
<html>
<head>
<script language="javascript">
function isOdd(x) {
return !isEven(x);
}
function isEven(x) {
if(x===0) {
return true;
} else {
return isOdd(x-1);
}
}
alert(isOdd(6));
</script>
</head>
<body>
</body>
</html>

Related

Why some parts of this code are red in VS Code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I just started to learn JavaScript. I have this script which should generate composite numbers in my separate HTML file, but I don't really understand why some parts are highlighted red when I moved script to .js file.
So my question is why there are some parts that are highlighted and how I'm suppose to write it correctly? I need to use strict in this script. I know that when using strict mode I need to declare variables, objects etc.
since you are working in a javascript file.
remove the script tags
you only need script tags in other file types like html etc
function checkComposite(num) {
var arr = [];
if( var num == 1 ) {
return false;
}
else if ( var num == 2) {
return false;
}
for (var x = 2; x < num; x++) {
if(num % x == 0) {
return true;
}
}
return false;
}
function compositeNumbers() {
num = Number(document.getElementsById('number').value);
for(var j = 1; j < num; j++) {
if(checkComposite(j)) {
arr.push(j);
}
}
document.getElementsById('result').innerHTML = arr;
}

Getting Undefined for this Javascript Function and Not Sure why [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
Okay. So, I'm quite a newbie to JS, but not to programming in general. I've written a little bit of a recursive function (don't mind the logic here), that works perfectly in Python but returns undefined in JavaScript. This may totally be a noob thing here, but I can't really find the source of this issue, especially as it works fine in Python.
I had initially thought it was a control flow issue but I wrote other control flow functions that worked fine. Also, right before the final return statement, I console.log the array and the values are returned. Any help will be appreciated
path = []
let find_multiplier = (numb) => {
if (numb === 1) {
console.log("a result is possible for this number");
console.log(path)
return path;
} else if (numb > 1) {
if (numb % 3 === 0) {
path.push("multiply by 3");
find_multiplier(numb / 3);
} else {
path.push("add 5");
find_multiplier(numb - 5);
}
} else {
return false
}
};
console.log(find_multiplier(13)); // returns undefined. should return values.
console.log(find_multiplier(15)); // returns undefined. should return false.
The problem here is that 2 of your 4 conditions does not return anything.
You can fix this by adding the return keyword in front of your recursive call.
path = []
let find_multiplier = (numb) => {
if (numb === 1) {
console.log("a result is possible for this number");
console.log(path)
return path;
} else if (numb > 1) {
if (numb % 3 === 0) {
path.push("multiply by 3");
return find_multiplier(numb / 3); //here
} else {
path.push("add 5");
return find_multiplier(numb - 5); //here
}
} else {
return false
}
};
console.log(find_multiplier(13)); // returns undefined. should return values.
console.log(find_multiplier(15)); // returns undefined. should return false.
I believe two of your cases are missing return statements:
if (numb % 3 === 0){
path.push("multiply by 3");
return find_multiplier(numb / 3); // <-- add return here
} else {
path.push("add 5");
return find_multiplier(numb - 5); // <-- and here
}
However, you are also mutating a variable (path) outside of the scope of your function, so I would guess even they way you have it currently, path may actually contain the result you're looking for. I would choose to either mutate the out-of-scope path array and not try to also return it, or declare the variable in your function and return that, with a preference to the latter.

How do I relate recursion to the remainder operator? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm using Eloquent JavaScript to learn JavaScript. I am a beginner and I would like to understand how this exercise works, it is about recursion and relating in to the remainder operator. I don't understand the comparison and I really would love to know how it works.
function isEven(n) {
if (n == 0) return true;
else if (n == 1) return false;
else if (n < 0) return isEven(-n);
else return isEven(n - 2);
}
console.log(isEven(50)) //true;
console.log(isEven(75)) //false;
console.log(isEven(-1)) //false;
I tried testing -2 in the log and it prints true, why does it do that?
I don't fully understand recursion or JavaScript that much, I would like it if this example is explained to me like I am 5.
Labeling the different decisions:
function isEven (n) {
if (n == 0) return true; // 1
else if (n == 1) return false; // 2
else if (n < 0) return isEven(-n); // 3
else return isEven(n - 2); // 4
}
when you call isEven(-2) it then calls isEven(2) according to // 3 which then calls calls isEven(0) according to // 4 which then returns true according to // 1

I need to sort out this algorithm [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
could you please your help.
how to answer this question ?
I need your help thinks
function factorial(n) {
if (n === 0) {
return 1;
} else {
return n * n ;
}
}
factorial(6)
this is not right.. i need to use writing code with Loop javascript.
Get the factorial of n. The inheritance of n is the number 1 through n multiplied by all numbers.
example)
answer should be like this
factorial(4); // => 24
factorial(5); // => 120
One way is to use a simple for loop.
var answer = 1;
for(var i = 2; i <= n; i++)
{
answer *= i;
}
return answer;
You can also do this recursively by multiplying your input by the result of the previous value.
function factorial(n){
if(n <= 1) return 1;
return n * factorial(n-1);
}

Javascript for loop with if statement not reaching the else if statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
why wont this reach the else if and return (i + 0) / 2? Also, why wont the alert give me i + 0 for a 2 digit value? (ie: 10, 20, 30, 40, etc. Any help would be appreciated.
var key= "OSN0MSA9991UNAAM8ELDPBD9F57BD6PU6BVBN54CDLEGDSUSNS";
var x = 0;
if (key[20] != "P" || key[18] != "P") {
x = 0;
for (i=0;i<10;i++) {
if (key[26] == i) {
x = i + 0;
alert(x);
}
};
} else if (key[20] == "P") {
for (i=9;i>-1;i--) {
if (key[26] == i) {
x = (i + 0) / 2;
alert(x);
}
};
};
your value at key[18] is "L" so if condition is always true and you will get an alert with value 7
It's not hitting the "else if" I believe because your array starts at 0, and the key[20] is in fact a P, so it's going to always fall in to the first condition and not hit the else if. EDIT: My mistake, misread. You could alert out the key[20] and key[18] to see what it thinks those values are.
Your issue is with the key[18]. Since you have an OR and key[18] = L (hence not P)..

Categories

Resources