Begginer java task, any help appreciated [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 9 days ago.
Improve this question
How do i solve this practice task?
"On the editor to your right you find a variable named charmanderLevel, to which a value between 1 and 100 will be assigned.
Using else if statements print to the console which evolution of Charmander corresponds to that experience level. Consider an else statement if the experience level ever go above 100 that should print 'Charizard is as good as it gets'.
Here's a chart with the evolution which corresponds to each level:
Charmander - 0 to 15
Charmeleon - 16 to 35
Charizard - 36 to 100"
var charmanderLevel = Math.ceil(Math.random() * 100);
if (charmanderLevel <= 15 && charmanderLevel>=0){
console.log('Charmander');}
else if (charmanderLevel <= 35 && charmanderLevel >= 16){
console.log('Charmeleon');}
else if (charmanderLevel <=100 && charmanderLevel >=36){
console.log('Charizard');}
else if (charmanderLevel > 100){
console.log('Charizard is as good as it gets');
}
This is the code i wrote but the task says:
Code is incorrect
"That is not enough, you need to check the Charmander experience against each level"
Appreciate any tips :)

You don't need the double check, you can easy solve it, first condition which fire and after it stops.
if(charmanderLevel > 100){
console.log('Charizard is as good as it gets');
}
else if(charmanderLevel >=36){
console.log('Charizard');
}
else if(charmanderLevel >=16){
console.log('Charmeleon');
}
else if(charmanderLevel >=0){
console.log('Charmander');
}

Related

Cant seem to get this code right please help :)) [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 2 days ago.
Improve this question
confirm("Ready to play???!!!");
var age = prompt("How old are you?");
console.log(age);
if (age >= 21) {
console.log("you can go in and have drinks :)");
}
if (age >= 19) {
console.log("go in but you cant drink");
} else if (age < 19) {
console.log("sorry cant let you in!");
}
When I typed in 23, it will both log the first one (age >= 21) and the second one (age >= 19).
I want it only show the first one, how could I do that?

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);

"Eloquent Javascript" recursion exercise [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 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

A small issue with javascript [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 8 years ago.
Improve this question
I created a simple fuzzbuzz in javascript (see below). I however would like to include the following:
if the number starts with a 1 (so fe 11) "ping" should be added. So 15 should be FizzBuzzPing etc...
Any thoughts?
function fizzBuzz() {
for (var i=1; i <= 20; i++)
{
if (i % 15 == 0)
alert("FizzBuzz");
else if (i % 3 == 0)
alert("Fizz");
else if (i % 5 == 0)
alert("Buzz");
else
alert(i);
}
}
$(document).ready(function(){
$('#clickMe').click(function(){
fizzBuzz();
});
});
Convert the number to a string and take the first index of the string:
var digit = (''+i)[0];
Or, the alternative
var digit = i.toString()[0];
Then check if digit is equal to 1 or not and add things or not accordingly.
For a future reference: Spend some time searching for a solution to your problems, don't ask questions unless you've spent some time making sure an answer does not exist to your question. A similar question has been answered many times before. Maybe it's not about fuzzbuzz but you should be able to find two different answers to two different questions and be able to combine then into your solution.

Using JavaScript which of these code chunks is best for real world use? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
This is just an example but there are a bunch of similar scenarios sprinkled throughout the code so it would be nice to choose the best solution and stick with it. The first is probably quickier but the second takes up less space*
*less space when reading/fixing code and less space when downloading.
if (imageCountCurrent <= 0){
$('#next_arrow').addClass('disabled');
}
else{
$('#next_arrow').removeClass('disabled');
}
if (imageCountCurrent >= imageCount - 5){
$('#prev_arrow').addClass('disabled');
}
else{
$('#prev_arrow').removeClass('disabled');
}
$('#.arrow').removeClass('disabled');
if (imageCountCurrent <= 0){
$('#next_arrow').addClass('disabled');
}
if (imageCountCurrent >= imageCount - 5){
$('#prev_arrow').addClass('disabled');
}
You may want .toggleClass instead, which eliminates the need for verbose if/else and addClass/removeClass constructs:
$('#next_arrow').toggleClass('disabled', imageCountCurrent <= 0);
$('#prev_arrow').toggleClass('disabled', imageCountCurrent >= imageCount - 5);

Categories