"Eloquent Javascript" recursion exercise [closed] - javascript

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 6 years ago.
Improve this question
The objective is not to use any of build in functions related to math to determine "isEven", but what is isEven(-n) suppose to do, and for those pondering what (n-2) does is that it subtracts a variable to a point of arriving to 1 or 0 (I don't belief that the book explains what the exercise is, so just looked at the answer).
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);
}

Looks like that just turns any negative number into a positive number, so for instance:
-10, 10, 8, 6, 4, 2,0 => even

Related

How to Create a program that calculates the count of numbers that is divisible to 3 and 4 between 1 and 100? [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 1 year ago.
Improve this question
it is hard to find a solution to this question, so I have a short way to do that. To find the count of numbers that's divisible to 3 and 4 between 1 and 100 we have a formula in Math: |b - a|: LCM (Lowest Common Multiple) + 1. I have all the stuff that is needed to create this program but I had a few issues with my code so I couldn't finish it. Can someone help me?
var totalCount3=0;
var totalCount4=0;
for(var i=1;i<=100;i++){
if(i % 3 == 0){
totalCount3 += 1;
}
if(i%4 == 0){
totalCount4 += 1;
}
}
console.log(totalCount3);
console.log(totalCount4);
The range of values you are checking is very limited (1-100) so you can use a bruteforce approach. You can simply scanning all values from to 100 and then check if each value is divisible using the module operator. I suppose you use javascript:
var counter = 0;
for(var i = 1; i<=100; i++){
if((i%3==0)||(i%4==0)){
counter++
}
}
console.log(counter);

How to split the number in js [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have only one number like "100000"
I wan't to split this number same as [20000,20000,20000,20000,20000] in array
so if number is 138000 then it should be like [20000,20000,20000,20000,20000,20000,18000]
How to do this?
let num = 118000,
num_array = [];
while (num > 0) {
if (num >= 20000) {
num_array.push(20000);
num -= 20000;
} else {
num_array.push(num);
break;
}
}
console.log(num_array);
I think this is what you want, please post what you tried, so that it helps to understand where you failed.

Simple Javascript validation function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I do not have any JavaScript experience and would like some assistance in creating a function as I am not sure on how to do it.
I would like to create a Function that validates that x between 1 and 17 it can also be equal to 1 and 17
If the value is not valid a simple messagebox/alert could be used to notify the user
I know this is a really stupid question but thank you in advance
A function like that would look something like:
function validateNumber(n) {
var ok = n >= 1 && n <= 17;
if (!ok) alert('The value is not valid. Values from 1 to 17 are allowed.');
return ok;
}
You can call it, and you get the status back if you want to use it for something:
if (validateNumber(x)) {
// the number was valid
} else {
// the number wasn't valid
}

Returns the cube of the sum of an input and 2 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm taking online class to learn JS and I'm stuck. The quiz is asking me to:
Returns the cube of the sum of an input and 2.
So I coded this:
var returnCubed = function(num){ return (num + 2) * 3;};
But it doesn't pass. What else can I do to take in a parameter, add two to it, then cube it? Or am I not understanding the directions?
Try
var returnCubed = function(num){
return (num+2) * (num+2) * (num+2);
};
*3 will always multiply with 3.
Hope this can solve your problem.

How to target a specific part of string? [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 9 years ago.
Improve this question
returns the index where pattern starts in string (or -1 if not found).
The search is to be case sensitive if the 3rd parameter is true otherwise it is case insensitive.
Examples
index("abAB12","AB",true) returns 2 but index("abAB12","AB",false) returns 0
index("abAB12","BA",true) returns -1 and index("abAB12","BA",false) returns 1
Maybe something like:
var str = "abAB12";
var i = str.indexOf("AB");
if (i < str.length / 2) {
//stuff
} else {
//other stuff
}

Categories