How do I select a select set of numbers in a loop? - javascript

Had to walk away from this one for the weekend. Would love to know my nemesis is giving me the answer plus some extra stuff? i want it to display 0 - 10
for (let i=-1; i++< 100; i*5) {
if (i < 0) {
continue;
} else if ( i > 50) {
break;
} else {
console.log(i/5);
}
}

Languages like Python will drop the remainder when using the / operator.
JavaScript won't, but you can get the same result with Math.floor().
for (let i = -1; i++ < 100; i * 5) {
if (i < 0) {
continue;
} else if (i > 50) {
break;
} else {
console.log(Math.floor(i / 5));
}
}

Related

I have a for loop that print's numbers. But I don't want it to print a few numbers

So i have a for loop that prints numbers from 3333 to 9999 in JavaScript. I want it so that when my for loop prints the numbers, it checks for every number if it contains the number 0, 1, 2 or 5. This is the code I have written till now but it still doesn't work. Hope you can help me.
for (let i = 3333; i <= 9999; i++) {
if (i === 1) {
continue;
} else if (i === 0) {
continue;
} else if (i === 5) {
continue;
} else if (i === 2) {
continue;
} else {
console.log(i)
}
}
}
printNum();
A solution with regex would look like this
for (let i = 3333; i <= 9999; i++) {
if (/[0125]/.test(i)) {
continue;
} else {
console.log(i)
}
}
for (let i = 3333; i <= 9999; i++) {
if (i.toString().includes(0) || i.toString().includes(1) ||i.toString().includes(2) || i.toString().includes(5)) {
continue;
} else {
console.log(i)
}
}
the following code will work fine. It converts the number to a string and checks if it contains 0,1,2 or 5 and if it finds such a number it just continues without printing it to the console

Java Script - Give certain array elements different colour

I am trying to do something with JS but as per usual arrays prove to be the bane of my existence...
I have to loop through the numbers from 1 to 100 and print them in the HTML, every number that divides by 3 should show in the colour red while all other numbers should be black. I tried so many things and tried to find how to do it but could not figure it out. Could anyone, please, tell me what is the proper way to do it?
You can use the following code to get what you are looking for.
for (let i = 1; i < 101; i++) {
if(i % 3 == 0) {
console.log('THREE');
} else {
console.log(i)
}
}
If you need to write the values to a document, change the console.log to document.write
Put the THREE in some inline element and add css rule to change the color.
For printing the list the solution explained by Jack. (Did it differently because I could.)
const text = (new Array(100))
.fill('')
.map((_v, i) => (i % 3) === 0 ? `<b>THREE</b>` : i)
.join('<br/>');
document.write(`<p>${text}</p>`)
b {
color: red;
}
First, loop through the numbers 1 to 100:
for (var i = 1; i <= 100; i++) {
//Stuff will go here
}
Then, write the number i to HTML:
document.write(i);
Finally, add the if statement:
if (i % 3) {
document.write(i);
} else {
document.write("THREE");
}
Full code:
for (var i = 1; i <= 100; i++) {
if (i % 3) {
document.write(i + "<br>");
} else {
document.write("THREE<br>");
}
}
EDIT
Here's how you'd make THREE red:
for (var i = 1; i <= 100; i++) {
if (i % 3) {
document.write(i + "<br>");
} else {
document.write("<span style='color: red;'>THREE</span><br>");
}
}

How to create dynamic IF statement?

I want to make condition of IF statement dynamically in javascript
example :
function checkNumber(number) {
var dynamicStatement = 1000000; // IF statement stop 1 million
if(number <= 1000) {
return 1000;
} else if(number <= 2000)
return 2000;
} else if(number <= 3000)
return 3000;
} else if(number <= 4000)
return 4000;
} else if (...) {
return ...
} else if (number <= 1000000) {
return 1000000;
}
}
Please help, thank you.
You can use:
Math.ceil(number/1000)*1000;
This will return the value nearest to the multiplier with 1000. For eg. if number is 900 then it will return 1000, if number is 1050 it will return 2000 and so on.
function checkNumber(number, limit, step) {
for(let i = 0; i < limit; i = i+step) {
if(number <= i) {
return i;
}
}
throw "Invalid Number";
}
checkNumber(75006, 1000000, 1000)
That answer Bhojendra posted is simple and beautiful, it's probably the one that suits your situation best.
Here's another, using for-loop:
function checkNumber(number) {
var dynamicStatement = 1000000;
var thousands = dynamicStatement / 1000;
for(var i = 0; i<thousands; i++){
if(number <= thousands * i){
return thousands * i;
}
}
return -1;
}

If Else Statement Disaster [duplicate]

This question already has answers here:
How does (A == B == C) comparison work in JavaScript?
(6 answers)
Closed 6 years ago.
today I decided I wanted to make a simple js code that would accept a number (in meters), and decide what the appropriate metric unit to use would be. The code turned out to be a little more complicated than I had expected, but I was able to figure out most of the bugs as I found them (even if it meant rearranging all of my code). However, when it came to my if/else statement I could not figure it out. If I put in a number that is less than 1 nothing happens. If I put in a number more than 9 it logs the same thing every time. The structure itself may need some work, but if someone could help me with the if/else statement I would be very thankful. Here is the code (init is called when the body loads):
function init() {
var x = prompt("How many meters?");
convertMetricMeters(x);
function convertDown(x) {
if (0.1 >= x >= 0.99) {
console.log("deci");
}
else if (0.100 >= x >= 0.999) {
console.log("centi");
}
else if (0.1000 >= x) {
console.log("milli");
}
else {
console.log("error");
}
}
function convertUp(x) {
if (1 <= x <= 99) {
console.log("deca");
}
else if (100 <= x <= 999) {
console.log("hecto");
}
else if (1000 <= x) {
console.log("kilo");
}
else {
console.log("error");
}
}
function convertMetricMeters(x) {
if (x < 1) {
convertDown(x);
}
else if (x > 9) {
convertUp(x);
}
else {
console.log("Appropriate Metric Unit");
}
}
}
Use && as AND operator in javascript
Convert these 100 <= x <= 999 to 100 <= x && x <= 999
You could simplify the check a bit and return if a condition is true.
function convertDown(x) {
if (x < 0.01) {
console.log("milli");
return;
}
if (x < 0.1) {
console.log("centi");
return;
}
if (x < 1) {
console.log("deci");
return;
}
console.log("error");
}
Your code has 2 sort of errors. One was simple to fix, that you have to add && between two conditions in if statement.
Now coming to the other part, the less than 1 items. It needed a different logic. Well, your maths seems to be needing bit attention. 0.1 is same as 0.100 and is same as 0.1000
I have updated the code to look for the number of digits after the decimal point and then console.log accordingly.
The updated code will be:
function init() {
var x = prompt("How many meters?");
convertMetricMeters(x);
function convertDown(x) {
// checks the number of digits after decimal point
decimals = (x.split('.')[1] || []).length
if (decimals == 1 || decimals == 2) {
console.log("deci");
}
else if (decimals == 3) {
console.log("centi");
}
else if (decimals == 4) {
console.log("milli");
}
else {
console.log("error");
}
}
function convertUp(x) {
if (1 <= x && x <= 99) {
console.log("deca");
}
else if (100 <= x && x <= 999) {
console.log("hecto");
}
else if (1000 <= x) {
console.log("kilo");
}
else {
console.log("error");
}
}
function convertMetricMeters(x) {
if (x < 1) {
convertDown(x);
}
else if (x > 9) {
convertUp(x);
}
else {
console.log("Appropriate Metric Unit");
}
}
}
Working jsfiddle example: https://jsfiddle.net/w7pf3moL/
A simplified version with only 1 method convert(float x):
function init() {
var x = prompt("How many meters?");
convertMetricMeters(x);
function convert(x) {
if (x < 0.01) console.log("milli");
else if (x < 0.1) console.log("centi");
else if (x < 1) console.log("deci");
else if (x < 10) console.log("meter");
else if (x < 100) console.log("deca");
else if (x < 1000) console.log("hecto");
else console.log("kilo");
}
function convertMetricMeters(x) {
if (x > 0) {
convert(x);
} else {
console.log("Appropriate Metric Unit");
}
}
}
init();

Problems with Modulo in JavaScript

Doing some practice runs on codecademy and came across the following problem: So the objective is to print "Fizz" if the numbers are divisible by 3. "Buzz" if the numbers are divisible by 5. And "FizzBuzz" if the numbers are divisible by both 3 and 5.
Here is my code, and I thought I had it right, but when I run it they tell me that my code is not 100% accurate. Looking to see any alternatives to this code, or what might be the issue...
Code:
for ( i = 0; i < 21; i++)
{
if (i % 3 == 0 )
{
console.log("Fizz");
}
if (i % 5 == 0)
{
console.log ("Buzz");
}
if ( i % 5 == 0 && i % 3 === 0)
{
console.log("FizzBuzz");
}
else
{
console.log(i);
}
}
You need to use else if to stop from other conditions executing:
for (var i = 1; i <= 20; 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);
}
}
It can easily be done in a one liner.
for (i = 1; i <= 20; i++) {
console.log(i%3?(i%5?i:'buzz'):(i%5?'fizz':'fizzbuzz'));
};
For a nice formatting also output i on each iteration:
for (i = 1; i < 21; ++i) {
console.log(i+": "+(i%3?(i%5?i:'buzz'):(i%5?'fizz':'fizzbuzz')));
};

Categories

Resources