Loop outcome separated by whitespace - javascript

I am trying to display all non-divisibles by 3 and 7 with whitespace between each number.
However I am a bit struggling with the whitespaces. This is how my outcome should look like: "1 2 4 5 8 10"!
I appreciate your help!
let input = 10;
let i = " ";
for(var num = 1; num <= input; num++ ){
if(num % 3 != 0 && num % 7 != 0){
i = i + num;
}
}
console.log(i);

You need to add spaces while modifying i. Also, remember to remove trailing whitespace at the and of the resulting string using .trimEnd():
let input = 10;
let i = "";
for(var num = 1; num <= input; num++ ){
if(num % 3 != 0 && num % 7 != 0){
i = i + num + " "; // <-- here
}
}
i = i.trimEnd(); // <-- and here
console.log(i);

let input = 10;
let i = " ";
for(var num = 1; num <= input; num++ ){
if(num % 3 != 0 && num % 7 != 0){
i = i + num + " ";
}
}
console.log(i);

Related

how to get an output of next `ten` odd and even numbers after entering any random number in javascript

I want to have a logic where if I enter an even number I want next 10 even numbers to be printed and If I enter an odd number, I want next 10 odd numbers to be printed. How should I rectify this logic inside a function. If someone can please help me rectifying the logic which was answered.
JS
function oddEven() {
var input = prompt("");
for (let x = 1; x <= 10; x++) {
console.log(input + x * 2);
}
}
oddEven()
This should work for any number
const input = 2
for (let x = 1; x <= 10; x += 1) {
console.log(input + x * 2)
}
i hope this help
function printTen(input){
let list = []
let number = input
while(list.length <= 10){
number++
if(input % 2 === 0 && number % 2 === 0){
console.log(number + " is even");
list.push(number)
}else if(input % 2 !== 0 && number % 2 !== 0) {
console.log(number + " is odd");
list.push(number)
}
}
}
printTen(9)
let inputval = 2;
for (let x = 1; x <= 10; x++) {
console.log(inputval + x * 2)
}

Javascript numbers - split/slice Prime

I am completely new to Javascript and trying to solve a simple problem now for more than two weeks and still not getting it(please help).
TASK ::::
Read a 4 digit Number e.g. 5678
Write a function
Split/separate the numbers and than build (5678, 567, 56, 5), than check if the numbers(5678, 567, 56, 5) are Prime numbers.
Give in Console/Result if 5678 a prime number or not, 567 a prime number or not and so on.
Check "if all numbers are Prime" than show result "All prime" if not show result "Not all prime".
Trying to solve the problem with (if else) but not really getting it, because i know very less about Javascript (arrays, string, split, slice) yet.
please help me understand. Thanks.
var a = 123456789;
var b = a.toString().length; //<<--->> ANTWORT: 9
document.write('ANTWORT: ',a );
for (i=0; i<b; i++) {
var x = a.toString().slice(0, -i);
document.write(x, ",");
}
function isPrime{
for(var i = 2; i < a; i++);
if(num % i === 0) return false;
return num > 1;
}
//integer is a string at the moment
integer = prompt("Enter a integer: ");
//initialize array for dictionary
dictArray = [];
stuff = document.getElementById("stuff");
//loop through all values of the string
for (var i = integer.length; i > 0; i--)
{
//take a substring from 0 to the ith char and turn it into an int
num = parseInt(integer.substring(0, i));
//add a dictionary to the array the tells what the number is
//and if it was prime or not as a bool
dictArray.push({"num": num, "prime": isprime(num)});
(dictArray[integer.length - i]["prime"]) ? stuff.innerHTML += "<br>" + num + " is prime." : stuff.innerHTML += "<br>" + num + " is not prime.";
}
function isprime(num)
{
if (num <= 3) return num >= 1;
if ((num % 2 === 0) || (num % 3 === 0)) return false;
let count = 5;
while (Math.pow(count, 2) <= num) {
if (num % count === 0 || num % (count + 2) === 0) return false;
count += 6;
}
return true;
}
//print the array
(dictArray.find(x => !x.prime) == undefined) ? stuff.innerHTML += "<br>All prime!" : stuff.innerHTML += "<br>Not all prime!";
//console.log(dictArray);
<div id="stuff">
</div>

Logical operator with different types

I'm trying to solve the famous FizzBuzz quiz but I decided to use the logical operator or instead of else to provide fullback.
for (var num = 1; num <= 100; num++) {
var output;
if (num % 5 === 0 && num % 3 === 0) {
output = "FizzBuzz";
} else if (num % 5 === 0) {
output = "Buzz";
} else if (num % 3 === 0) {
output = "Fizz";
}
console.log(output || num);
}
This was supposed to print all the numbers from 1 to 100, with some exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5, print "Buzz" instead and "FizzBuzz", for numbers that are divisible by both 3 and 5.
But it doesn't print any numbers.
The output declaration could be num like :
var output = num;
So you don"t have to use the || operator and just print the output directly :
console.log(output);
for (var num = 1; num <= 100; num++) {
var output = num;
if (num % 5 === 0 && num % 3 === 0) {
output = "FizzBuzz";
} else if (num % 5 === 0) {
output = "Buzz";
} else if (num % 3 === 0) {
output = "Fizz";
}
console.log(output);
}
I will say that Zakaria's answer is correct, but for exposure's sake, here is my answer
for (var i = 1; i <= 100; i++) {
var output = "";
if (!(i % 3)) output += "Fizz";
if (!(i % 5)) output += "Buzz";
console.log(output || i);
}
My logic here:
set the output value to be equal to "", which evaluates to a falsey value.
If a number is divisible by 3, then i % 3 will be 0, this is also a falsey value so we flip it by using the ! operator. Assume that i=9, then ! (i%3) = !(9%3) = !(0) = !(false) = true.
Therefore, if !(i%3) becomes true we append our empty string with "Fizz", then we use the same sort of logic for i%5, but instead appending "Buzz"
Note the order of these two if statements is important -- flip them around and you'll get BuzzFizz instead of FizzBuzz.
If output is not the empty string we set it to originally, output || i will return the value of output, giving us "Fizz", "Buzz", or "FizzBuzz" depending.
If ouput is empty, then output || i will return the value for i
Use let to fix the output's scope:
for (var num = 1; num <= 100; num++) {
let output;
if (num % 5 === 0 && num % 3 === 0) {
output = "FizzBuzz";
} else if (num % 5 === 0) {
output = "Buzz";
} else if (num % 3 === 0) {
output = "Fizz";
}
console.log(output || num);
}
Also, the || could be removed if you initialize output with num:
for (var num = 1; num <= 100; num++) {
let output = num;
if (num % 5 === 0 && num % 3 === 0) {
output = "FizzBuzz";
} else if (num % 5 === 0) {
output = "Buzz";
} else if (num % 3 === 0) {
output = "Fizz";
}
console.log(output);
}

Format numbers in comma separated value

I am using javascript to format the number with commas , it was working very fine.
But now the problem is if a value is comming in negative for example : -792004
It is returning the output like : -,792,004 that is comma is in the start.
How can I modify this method ?
Here is my code :
function Comma(number) {
number = '' + number;
if (number.length > 3) {
var mod = number.length % 3;
var output = (mod > 0 ? (number.substring(0, mod)) : '');
for (i = 0; i < Math.floor(number.length / 3); i++) {
if ((mod == 0) && (i == 0))
output += number.substring(mod + 3 * i, mod + 3 * i + 3);
else
output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
}
return (output);
} else return number;
}
The simplest way I know which will helps you is toLocaleString() method on number:
var x = 10033001;
var y = -10033001;
console.log(x.toLocaleString(), y.toLocaleString());
But for correction of your code, you can remove number sign with Math.abs and add it after with Math.sign.
var sign = Math.sign(number);
number = Math.abs(number);
// Do the conversion
return (sign < 0) ? ("-" + output) : output;
Try this:
const comma = function(number) {
const prefix = number < 0 ? '-' : ''
number = String(Math.abs(number))
if (number.length > 3) {
const mod = number.length % 3
let output = (mod > 0 ? (number.substring(0,mod)) : '')
for (let i = 0; i < Math.floor(number.length / 3); i++) {
if (mod === 0 && i === 0)
output += number.substring(mod+ 3 * i, mod + 3 * i + 3)
else
output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
}
return prefix + output
} else {
return prefix + number
}
}
If the number is negative, it assigns - to prefix. Then it changes number to its absolute value (Math.abs(number)). In the end it returns value with prefix.

Euler #2 Fibonacci Javascript function will only return zero after crunching the numbers

I wrote a function to solve Euler #2 in Javascript for adding all the even Fibonacci numbers up to 4,000,000. However, when I run my function, the chrome dev tool keeps giving me zero as the answer. I am not sure why.
function DoEverything() {
oldnum = 0;
num = 1;
total = 0;
result = addFibNumbers(num, oldnum);
console.log(result);
}
function addFibNumbers(num, oldnum) {
while(num < 4000000) {
if (num % 2 == 0) {
newnum = num + oldnum;
total += newnum;
oldnum = num;
num = newnum;
}
return total;
}
}
DoEverything();
The reason its returning 0:
result = addFibNumbers(num, oldnum);//num=1,oldNum=0
//function
while(num < 4000000) { //num is 1, so it enters while
if (num % 2 == 0) {// 1 % 2 == 1, so skip this if
return total;// this ends the function, returning total=0 as nothing was changed
I guess you are looking to do this:
while(num < 4000000) {
newnum = num + oldnum;
if (newnum % 2 == 0 && newnum < 4000000) {
total += newnum;
}
oldnum = num;
num = newnum;
}
return total;
I would guess it is your while loop
Change this:
while(num < 4000000) {
if (num % 2 == 0) {
newnum = num + oldnum;
total += newnum;
oldnum = num;
num = newnum;
}
return total;
}
to this:
while(num < 4000000) {
if (num % 2 == 0) {
newnum = num + oldnum;
total += newnum;
oldnum = num;
num = newnum;
}
}
return total;
Your while loop is useless with a return in it and no if statement to control it's use.
In addition to modifying your while statement inside of addFibNumbers() like so:
function addFibNumbers(num, oldnum) {
while(num < 4000000) {
newnum = oldnum + num;
if (oldnum % 2 == 0) {
total += oldnum;
}
oldnum = num;
num = newnum;
}
return total;
}
you will also need to initialize the first two Fibonacci terms to 1 and 2:
oldnum = 1; and num = 2;

Categories