I am newbie here in more way than one, so please go easy on me :)
Here is a problem I was tasked with solving using javascript:
Print out the numbers from 1 - 20.
The rules:
For numbers divisible by 3, print out "Fizz".
For numbers divisible by 5, print out "Buzz".
For numbers divisible by both 3 and 5, print out "FizzBuzz" in the console.
Otherwise, just print out the number.
Here was my first attempt at approaching it:
var numberArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
for(var i = 0; i < numberArray.length; i++){
if(i % 3 === 0 && i % 5 === 0){
console.log("FizzBuzz");
}
else if(i % 3 === 0 && i % 5 !== 0){
console.log("Fizz");
}
else if(i % 3 !== 0 && i % 5 === 0){
console.log("Buzz");
}
else {
console.log(numberArray[i]);
}
}
This returned the following incorrect values:
FizzBuzz
2
3
Fizz
5
Buzz
Fizz
8
9
Fizz
Buzz
12
Fizz
14
15
FizzBuzz
17
18
Fizz
20
I then took a different approach which DID result in the correct answer:
var fizBuzz = function() {
for (i = 1; i < 21; i++) {
if (i % 3 === 0 && i%5 === 0) {
console.log ("FizzBuzz");
} else if (i % 3 === 0) {
console.log ("Fizz");
} else if (i % 5 === 0) {
console.log ("Buzz");
} else {
console.log (i);
}
}
};
fizBuzz();
Would someone be willing to help me understand what was wrong about the first approach? This is really bothering me :)
The problem is you were checking the divisibility of i (which starts at 0) rather than of numberArray[i] (which starts at 1).
Related
I've made a sequence of numbers from 0 to 20 and I want to change the sequence so once it comes up with a multiple of 3 and 5 it logs 'FizzBuzz' to the terminal then carries on with the rest of the numbers up to 20. My problem is once I have changed the number to the string the rest of the terms in the sequence come up with NaN. I know the problem with my code is that I'm changing the number to a string and you cannot perform addition to a string which is why NaN comes up. I'm pretty new to this so any thoughts on how to do complete this would be greatly appreciated. I've tried using .append() but I'm pretty sure I'm using it incorrectly.
My code thus far;
var increment = function(number)
{
for (var i = 1; i <= 20; i++)
{
console.log(number++)
if ((number % 3 === 0) && (number % 5 === 0))
{
number = "FizzBuzz"
console.log("FizzBuzz");
}
else if (number % 3 === 0)
{
console.log("Fizz");
}
else if (number % 5 === 0)
{
console.log("Buzz");
}
else
{}
}
}
increment(1)
When you find a multiple of 3 and 5, you are setting number to "FizzBuzz", which does not have a ++ operator. On the next iteration, you call ++ on number, which is now "FizzBuzz", so it logs NaN.
If you don't set number to "FizzBuzz" it should work fine.
This would do it
var increment = function()
{
for (var i = 1; i <= 20; i++)
{
if ((i % 3 === 0) && (i % 5 === 0))
{
console.log("FizzBuzz");
}
else if (i % 3 === 0)
{
console.log("Fizz");
}
else if (i % 5 === 0)
{
console.log("Buzz");
}
else{
console.log(i)
}
}
}
increment()
There's no need to pass the number parameter, since your for loop is already set to increment by 1 (i++).
please I'm stuck in this question below since yesterday. Below is the question:
Write a program that uses console.log to print all the numbers from 1
to 100, with two exceptions. For numbers divisible by 3, print "Fizz"
instead of the number, and for numbers divisible by 5 (and not 3), print
"Buzz" instead.
When you have that working, modify your program to print "FizzBuzz",
for numbers that are divisible by both 3 and 5 (and still print "Fizz" or
"Buzz" for numbers divisible by only one of those).
I only got the first two conditions but not the the third. I don't know how to go about it anymore, I've tried many options. Below is my code:
<html>
<head/head>
<body>
<script type="text/javascript">
for (i = 1; i <= 100; i++)
if (i % 3 == 0) {
document.write("Fizz");
document.write("<br />");
} else if (i % 5 == 0 && i % 3 != 0) {
document.write("Buzz");
document.write("<br />");
} else if (i % 3 && 5 == 0 && i % 3 != 0 && i % 5 != 0) {
document.write("FizzBuzz");
document.write("<br />");
} else {
document.write(+i);
document.write("<br />");
}
</script>
</body>
</html>
Check the most specific (FizzBuzz) condition first.
function fizzBuzz() {
for(var i = 1; i <= 100; i++){
if(i % 5 === 0 && i % 3 === 0){
console.log('FizzBuzz');
} else if(i % 3 === 0){
console.log('Fizz');
} else if(i % 5 === 0){
console.log('Buzz');
} else {
console.log(i);
}
}
}
here is an updated version of your code, I keep it as you write it with some changes, I made it work without touch it's logic, you can see that the problem was in the first comparison and in the second "if else" (5 will never be equal to 0). you can optimize the code more than that, good luck.
<html>
<head/head>
<body>
<script type="text/javascript">
for (i = 1; i <= 100; i++)
if (i % 3 == 0 && i % 5 != 0) {
document.write("Fizz");
document.write("<br />");
} else if (i % 5 == 0 && i % 3 != 0) {
document.write("Buzz");
document.write("<br />");
} else if (i % 3 == 0 && i % 5 == 0) {
document.write("FizzBuzz");
document.write("<br />");
} else {
document.write(+i);
document.write("<br />");
}
</script>
</body>
</html>
Since everyone is contributing, I might as well give you an interesting solution:
var i = 101;
while(i --> 0){ // as i goes to 0... wat
var state = !!(i % 3) << 1 | !!(i % 5), // compute state?
output = ["FizzBuzz", "Fizz", "Buzz", i]; // hmm...
console.log(output[state]); // output correct string
}
1st - Instead of document.write use console.log like the question says
2nd - You have a syntax error in the head section. it should be <head></head>
3rd - for the 1st part of the question all you need is this:
for (i = 1; i <= 100; i++) {
// if i is divisible by 3
if (i % 3 == 0) {
console.log("Fizz");
}
// if i is divisible by 5 (no need to check for 3 again)
else if (i % 5 == 0) {
console.log("Buzz");
}
// else
else {
console.log(i);
}
}
4th - For the 2nd part you need to add an extra if on top of what you have already:
for (i = 1; i <= 100; i++) {
// if i is divisible by 3 and 5
if (i % 3 == 0 && i % 5 == 0) {
console.log("FizzBuzz");
}
// if i is divisible by 3
else if (i % 3 == 0) {
console.log("Fizz");
}
// if i is divisible by 5 (no need to check for 3 again)
else if (i % 5 == 0) {
console.log("Buzz");
}
// else
else {
console.log(i);
}
}
working fiddle: https://jsfiddle.net/tedmanowar/amapqcLL/
You can use a while loop, than use a nested if statements to check the conditions.
let number = 0;
while (number <= 100) {
if(number % 3 === 0 && number % 5 === 0){
console.log("FizzBuzz");
}else if(number % 3 === 0){
console.log("Fizz");
}else if(number % 5 === 0){
console.log("Buzz");
}else{
console.log(number);
}
number++;
}
This solution is the easiest and simplest. There are multiple ways to solve the question though.
for (n=1; n<=100; n++){
let output = "";
if(n % 3=== 0) output += "Fizz"
if(n % 5=== 0) output += "buzz"
console.log(output || n);
}
I don't recommend this answer - since it is very hard to maintain - but it does do it in very few lines. It also relies on the two numbers only having a common factor of 1.
for(let i=1; i<=100; i++) {
console.log(`${i%15?i%5?i%3?i:'Fizz':'Buzz':'FizzBuzz'}`)
}
This is using the ternary operator and backquote template strings.
You should just use a loop that starts at 1 and is less than 101 (so up to 100) and test for the %n === 0. In other words, make sure there is no remainder.
function startConsoleDemo(){
for(var i=1,r; i<101; i++){ // loop from 1 to 100
r = i; // default value of r
if(i % 3 === 0 && i % 5 === 0){ // if i/3 and 1/5 do not produce a remainder
r = 'FizzBuzz'; // reassign r
}
else if(i % 3 === 0){ // we knew i % 5 !== 0 so see if i/3 does not produce a remainder
r = 'Fizz'; // reassign r
}
else if(i % 5 === 0){ // we already knew i % 3 !== 0 - you know the drill
r = 'Buzz'; // reassign r
}
console.log(r); // console at each step of the loop no matter what
}
}
startConsoleDemo(); // without () you can use like a var then () later
I have a solution and I'm pretty sure it'll work for you.
for (let i = 1; i <= 100; i++) {
if (i % 3 === 0 && i % 5 === 0) {
document.write(`${i} FizzBuzz`);
} else if (i % 3 === 0 && i % 5 !== 0) {
document.write(`${i} Fizz`);
} else if (i % 5 === 0 && i % 3 !== 0) {
document.write(`${i} Buzz`);
} else {
document.write(`${i}`);
}
}
Write a javascript program that displays the numbers from 10 to 100. But for multiples of 4 print "Penny" instead of the number and for multiples of 6 print "Leonard". For numbers which are multiples of both 4 and 6 print "Bazzinga"
I know how to do two parts struggling to print 6 and 4;
function baZzinga (number) {
for (var number = 10; number <= 101; number++)
if(number % 4 == 0) {
console.log("penny");
}
else if (number % 6 == 0) {
console.log("Leonard");
} else if ( not sure what goes here) {
help help help
} else {
console.log(number");
}
You want the and condition first. Try this
var result = document.getElementById("result");
function baZzinga (number) {
for (var number = 10; number <= 101; number++) {
if (number % 4 == 0 && number % 6 == 0) {
result.innerHTML += "Bazinga";
}
else if(number % 4 == 0) {
result.innerHTML += "penny";
}
else if (number % 6 == 0) {
result.innerHTML += "Leonard";
}
else {
result.innerHTML += number;
}
}
}
baZzinga()
<p id="result"></p>
I changed console.log to result.innerHTML because I wanted to demonstrate it in a snippet.
I have a few comments on your code -- constructive criticism, I hope! First, you don't need the number parameter in your bazzinga function. Next, the indentation of the code you posted makes it hard to read. Finally, you should almost always use === instead of ==. The === tests for strict equality, whereas == tries to do some type conversions first (and can therefore produce unexpected results). See the official docs.
To answer you question: check for divisibility by 6 AND 8 first. That way, it will override the individual cases. I believe you want something like this:
function bazzinga() {
for (var number = 10; number <= 100; number++) {
if (number % 4 === 0 && number % 6 === 0) {
console.log("Bazzinga");
} else if (number % 4 === 0) {
console.log("Penny");
} else if (number % 6 === 0) {
console.log("Leonard");
}
}
}
Here is a solution using the format you posted:
for (var number = 10; number <= 100; number++) {
if(number % 4 === 0 && number % 6 === 0){
console.log("bazzinga");
} else if(number % 4 === 0) {
console.log("penny");
} else if (number % 6 === 0) {
console.log("Leonard");
} else {
console.log(number);
}
}
Or use the ternary operator to be even more succinct!
for (var i = 10; i <= 100; i++){
var penny = i % 4 === 0;
var leonard = i % 6 === 0;
console.log(penny ? (leonard ? "bazzinga" : "penny"): leonard ? "leonard" : i);
}
function process_num(num) {
return num % 4 == 0 ? num % 6 == 0 ? "Bazzinga" : "Penny" : num % 6 == 0 ? "Leonard" : num;
}
for (x = 10; x <= 100; x++) { console.log( x + ': is ', process_num(x)) }
Nested Ternary operator for conciseness
If it passes outer ternary test it is divisible by 4:
Enter into nested termary one to test if num is also divisible by 6 for the BaZzinga prize!!
If it fails the BaZzinga challenge, we know it previously passed the divisible by 4 test so print "penny"
Failing the outer ternary condition, we know it's not divisible by 4:
Enter nested ternary two to consider if divisible by 6. If so print "Leonard".
If not it's failed both the outer (div by 4) and inner (div by 6) so return the number unchanged.
Now that the logic is contained in the function, we can just create a for loop to iterate over the required numbers printing out the correct values.
This question already has answers here:
FizzBuzz program (details given) in Javascript [closed]
(27 answers)
Closed 4 years ago.
Here are Codecademy's instructions:
Print out the numbers from 1 - 20.
For numbers divisible by 3, print out "Fizz".
For numbers divisible by 5, print out "Buzz".
For numbers divisible by both 3 and 5, print out "FizzBuzz" in the
console.
Otherwise, just print out the number.
And here is my code:
for (i = 1; i <= 20; i++) {
if (i % 3 == 0) {
console.log("Fizz");
}
else if (i % 5 == 0) {
console.log("Buzz");
}
else if (i % 3 == 0 && i % 5 == 0) {
console.log("FizzBuzz");
}
else {
console.log(i);
}
}
The problem is that it won't print "FizzBuzz" for the number 15. It just prints "Fizz".
What am I missing here?
The else if only runs if all of the other statements so far have been false. Since i % 3 is true, the remaing else/if statements will never run. Try something like this:
for (i = 1; i <= 20; i++) {
if (i % 3 == 0 && i % 5 == 0) {
console.log("FizzBuzz");
}
else if (i % 5 == 0) {
console.log("Buzz");
}
else if (i % 3 == 0) {
console.log("Fizz");
}
else {
console.log(i);
}
}
If you want to use a nested conditional this seems to work.
for ( i=1 ; i < 21 ; i++){
if(i % 3 === 0) {
if(i % 5 === 0){
console.log("FizzBuzz");
}
else {
console.log("Fizz");
}
}
else if (i % 5 === 0) {
if ( i % 3 === 0){
console.log("FizzBuzz");
}
else{
console.log("Buzz");
}
}
else{
console.log(i);
}
}
straight forward fizzBuzz in javascript using tenary operators(a one-line shorthand for an if-else statement)
to learn about tenary operators go here
var i = 1;
while (i <= 20){
console.log((i % 3 === 0 && i % 5 === 0) ? "FizzBuzz" : (i % 3 === 0) ? "Fizz" : (i % 5 === 0 ? "Buzz" : i));
i++;
}
I am trying to iterate through the arrays in the numbers variable, and if a number can be divided by 3 I'm logging "fizz", if it can be divided by 5 I'm logging "buzz", and if a number can be divided by 3 + 5, or 15, I'm logging "fizzbuzz"
Here is the working code:
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
for (var i = 1; i <= numbers.length; i++) {
if (i % 15 === 0) {
console.log("FizzBuzz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else {
console.log(i);
}
};
Here is my original code, which doesn't log "fizzbuzz"
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
for (var i = 1; i <= numbers.length; i++) {
if (i % 5 === 0) {
console.log("Buzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 15 === 0) {
console.log("FizzBuzz");
} else {
console.log(i);
}
};
Why does the (i % 15 === 0) condition need to precede the other two conditions? Shouldn't it not matter?
The number 15 is divisible by both 3 and 5. If you don't test it first, then you'll never get there.
So let's take 30 as an example. If you check 15 first, you'll see that it's divisible by 15. However, if you check either 5 or 3 first, it'll be flagged as being divisible by either of those.
After one of your conditions evaluates as true, you break out of that if-block and don't evaluate following else-if/else statements. If you want the rest of them to evaluate you can make them if statements instead of else-if's.
It's because firstly the computer checks for the first statement and then the others as you use the ELSE IF which means, to check the statement after the first one is false. Use IF for all instead.