so I'm doing this FizzBuzz challenge and I've made a mistake but cannot identify it. I'm wanting numbers from 1-100 to display on a web browser. The numbers that are divisible by 3 should say "fizz", the numbers divisible by 5 should say "buzz", and the numbers divisible by both 5and3 should say "FizzBuzz." I'm unable to get the browser to display the words, and for some reason it only displays the numbers.
function fizzbuzz() {
var display = document.getElementById('display');
var displayHTML = "";
for (i = 1; i < 101; 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)
}
displayHTML += "<p>" + i + "</p>";
}
display.innerHTML = displayHTML
}
<body onload="fizzbuzz()">
<div id="display">
</div>
</body>
That's because you are concatenating i, the counter to displayHTML here displayHTML += "<p>" + i + "</p>";
Instead save the value you want to print in a variable on conditional check and then concatenate it afterwards
var result = '';
for (i = 1; i < 101; i++) {
if (i % 3 === 0 && i % 5 === 0) {
result = "FizzBuzz";
} else if (i % 5 === 0) {
result = "buzz";
} else if (i % 3 === 0) {
result = "Fizz";
} else {
result = i;
}
displayHTML += "<p>" + result + "</p>";
When a condition is true, you can set the result in a variable and then print your displayHTML element.
function fizzbuzz() {
var display = document.getElementById('display');
var divisible = "";
var displayHTML = "";
for (i = 1; i < 101; i++) {
if (i % 3 === 0 && i % 5 === 0) {
divisible = "FizzBuzz";
} else if (i % 5 === 0) {
divisible = "Buzz";
} else if (i % 3 === 0) {
divisible = "Fizz";
} else {
divisible = "";
}
displayHTML += "<p>" + i + ":" + divisible + "</p>";
}
display.insertAdjacentHTML('afterend', displayHTML);
}
fizzbuzz();
const fancy = function(i, output) {
for (let i = 0; i <= 100; i++) {
let output = '';
if (i % 3 === 0) output += 'fizz';
if (i % 5 === 0) output += 'buzz';
console.log(output || i);
}
};
fancy();
Related
function sum2(num) {
let sum ;
let str = String(num)
for(let i = 0 ; i < str.length; i++){
if(i === 0) {
sum = parseInt(str[i]);
} else {
sum = sum + parseInt(str[i])
}
}
return sum;
}
debugger;
let output = sum2(1148);
console.log(output); // --> 14 = 1 + 1 + 4 + 8
output = sum2(-316);
console.log(output); // --> 4 = -3 + 1 + 6
i need to case num is -316, and num is 0
but it will be return 'NaN'..
what should i do..?
you need to take care of the '-' (minus)
function sum2(num) {
let sum = 0;
const str = String(num)
let multipler = 1;
for (let i = 0 ; i < str.length; i++) {
if (i === 0 && str[i] === "-") {
multipler = -1;
} else {
sum += multipler * parseInt(str[i], 10);
multipler = 1;
}
}
return sum;
}
console.log(sum2(1148)); // --> 14 = 1 + 1 + 4 + 8
console.log(sum2(-316)); // --> 4 = -3 + 1 + 6
Your code is fine but you also have to add a condition to check for the minus sign.
function sum2(num) {
let sum = 0;
let str = String(num)
for(let i = 0 ; i < str.length; i++){
if(i === 0) {
if(str[i] == '-'){
sum = sum - parseInt(str[i+1]);
i = i+1;
}else{
sum = parseInt(str[i]);
}
}else {
sum = sum + parseInt(str[i])
}
}
return sum;
}
debugger;
let output = sum2(1148);
console.log(output); // --> 14 = 1 + 1 + 4 + 8
output = sum2(-316);
console.log(output); // --> 4 = -3 + 1 + 6
JavaScript. Is there a way to get a for loop do things slightly differently on every second (fifth etc.) iteration from what it is doing "by default"?
for (var i = 0; i < n.length; i++) {
a += 5;
b += a;
}
What if I want to add to a not 5 but 10 on every second iteration and add to b not a but b itself on every fifth iteration of the loop? Is this doable? Thank you!
Use the modulus operator % to find out whether i is evenly divisible by the "every Nth" value.
This will run both clauses on the first (zeroth) iteration, since zero is so divisible; you might want i % 2 == 1 and i % 5 == 4 instead, depending.
for (var i = 0; i < n.length; i++) {
if(i % 2 == 0) {
a += 5;
}
if(i % 5 == 0) {
b += a;
}
}
You can do the following,
for (var i = 0; i < n.length; i++) {
if(i%2 === 0) {
a += 10;
} else {
a += 5;
}
if(i%5 === 0) {
b+= =b;
} else {
b += a;
}
}
maybe you can try this:
for (var i = 0; I <n.length; i++){
if( i + 1 / 2) {
a += 10;
b += a
}else if ( i + 1 / 5) {
a += 5;
b += b
} else {
a += 5;
b += a;
}
}
Hello everyone i have a problem with my code and i'm not sure how to do this , i need to write code that draw this in console:
Draw '*' in every even number
For that i need to use nested loops.
So far i have only this:
var n = 5;
var stars = '';
for (var i = 1; i <= n; i++) {
var starsline = '';
for (var j = 1; j <= n; j++) {
console.log(i + j);
}
if ( i % 2 === 0){
starsline += '2';
} else {
starsline += '1'
}
stars += starsline;
}
console.log(stars);
This numbers 2 and 1 are only to check if the number is even or odd.
Just a few things:
1) you got some weird bracket here:
/*}*/ if ( i % 2 === 0){
which causes a syntax error later on.
2) you actually log the right thing:
console.log(i + j)
but you dont use it. Just put that into your condition:
if((i + j) % 2 === 0)
and you are done :)
let size = 5, stars = "";
for (var row = 1; row <= size; row++) {
var starsline = "";
for (var col = 1; col <= size; col++){
if ((row + col) % 2 === 0){
starsline += '*';
} else {
starsline += ' ';
}
stars += starsline + "\n";
}
console.log(stars);
I think what you tried to do is something like this:
var n = 5;
var stars = '';
for (var i = 1; i <= n; i++) {
var starsline = '';
for (var j = 1; j <= n; j++){
if ( (i + j) % 2 === 0){
// used three spaces for consistency in the drawing
starsline += ' ';
} else {
starsline += ' * '
}
}
stars += starsline + '\n';
}
console.log(stars);
Try this:
var n = 5;
var stars = '';
for (var i = 1; i <= n; i++)
{
var starsline = '';//<-- reset the value of line
if ( i % 2 === 0)//<--this identifies which line will the stars be created
{
starsline += '* * *';//<--creating the stars on each line
}
else
{
starsline += ' * * ';//<--creating the stars on each line
}
stars += starsline+'\n';//<-- '\n' add line breaks for each lines
}
console.log(stars);//<-- print the stars
I'm not quite sure why my code is not working here. It is supposed to filter out odd and even numbers and put them into an array but I think my (lack of) understanding is on getting the numbers into the array how I want.
function oddAndEven(numbers) {
var odd = [];
var even = [];
for (num = 0; num < numbers.length; numbers++) {
if (numbers[num] % 2 == 0) {
even.push(numbers[num]);
} else if (numbers[num] % 2 == 1) {
odd.push(numbers[num]);
}
}
console.log(odd + "is odd and " + even + " is even");
}
iqTest(11221122);
function oddAndEven(numbers) {
var odd = [];
var even = [];
for (num = 0; num < numbers.length; num++) { // numbers is array, num is counter variable
if (numbers[num] % 2 == 0) {
even.push(numbers[num]);
} else if (numbers[num] % 2 == 1) {
odd.push(numbers[num]);
}
}
console.log(odd + " is odd and " + even + " is even");
}
oddAndEven([10,5,6,4,5]); // pass array as you are traversing though it
Incrementing the loop variable was not done in your case. Please try this.
function oddAndEven(numbers){
var odd = [];
var even = [];
for(num = 0; num < numbers.length; num++){
if(numbers[num] % 2 == 0){ //Even
even.push(numbers[num]);
}else { // Odd
odd.push(numbers[num]);
}
}
console.log(odd + "is odd and " + even + " is even");
}
Try this,
function oddAndEven(numbers) {
var odd = [];
var even = [];
for (num = 0; num < numbers.length; numbers++) {
if (numbers[num] % 2 == 0) {
even[num]=numbers[num];
} else if (numbers[num] % 2 == 1) {
odd[num]=numbers[num];
}
}
console.log(odd + "is odd and " + even + " is even");
}
You seem to improve your practice more.
function oddAndEven(numbers) {
var odd = [];
var even = [];
console.log ("length is " , numbers.length);
for (num = 0; num < numbers.length; num++) {
if (numbers[num] % 2 == 0) {
even.push(numbers[num]);
} else if (numbers[num] % 2 == 1) {
odd.push(numbers[num]);
}
}
console.log(odd + "is odd and " + even + " is even");
}
oddAndEven("82938411221122919239");
You missed num++, and you may want to send the input "82938411221122919239"
As A.T suggested, you shall send [8,2,9,....] as an input also.
function oddAndEven(numbers) {
var odd = [];
var even = [];
// a bit of optimization here, accessing `length` property only once
for (index = numbers.length - 1 ; index >= 0; index--) {
let current = numbers[index];
if (current%2 == 0) {
even.push(current);
} else {
odd.push(current);
}
}
console.log(odd + "is odd and " + even + " is even");
}
I want to list all of the factors and prime factors of two set of numbers.
Here is my code, but my prime factors are not right and the comma at the end of the lines are not needed.
Any help to correct the prime factor? 4 should be 2,2,2 but it comes to be 2.
var l = parseInt(prompt("What is your lowest number"));
var k = parseInt(prompt("What is your highest number"));
var results = '';
//function counts (incremental) the numbers
// display the numbers in bold
// numbers are display on the left of
function genFactors(num) {
var result = "";
var result1 = "";
for (var i = 2; i <= num; ++i) {
if (num % i == 0) {
result += i + ",";
result1 += genPrimeFactors(i);
}
}
results += '<b>' + num + '</b>: ' + result1 + '<br>';
return result;
}
document.writeln("<h1>All Factors</h1>");
document.writeln("<p>The factors of " + l + " and " + k + " are: " + ":
</p>");
for (var num = 1; num <= k; ++num) {
document.writeln("<b>" + num + "</b>" + ":" + genFactors(num) + "<br />");
}
// determine the prime numbers
function isPrime(num) {
for (var i = 2; i < num; i++) {
if (num % i === 0) {
return 0;
} else {
return 1;
}
}
}
//determine the prime factors
function genPrimeFactors(num) {
for (var i = 2; i <= num; i++) {
if (num % i != 0) {
while ((num / 2 == parseInt(num / 2)) && (num > 2)) {
num = num / 2;
return "";
}
if (isPrime(num)) {
return num + ',';
} else {
}
}
}
if (num == 2) {
return 2 + ',';
}
}
/*function
function genPrimeFactors(num) {
var result = "";
for (var i = 2; i <= num; ++i) {
for (var k = 2; k <= i; ++k) {
if (i % k != 0) {
}
result += i + ",";
}
}
return result;
}
*/output
document.writeln("<h1>Prime Factors</h1>");
document.writeln("<p>The prime factors of " + l + " and " + k + " are: " + ":</p>");
document.writeln(results);
You can cut out the comma with substring at the return, like this
function genFactors(num) {
var result = "";
var result1 = "";
for (var i = 2; i <= num; ++i) {
if (num % i == 0) {
result += i + ",";
result1 += genPrimeFactors(i);
}
}
result1.substring(0,result1.length-1);//maybe try this
results += '<b>' + num + '</b>: ' + result1 + '<br>';
return result.substring(0,result.length-1);
}