Can someone pls help me solve this issue [closed] - javascript

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 7 years ago.
Improve this question
Create a function fizzBuzz to return 'Fizz', 'Buzz', 'FizzBuzz', or the argument it receives, all depending on the argument of the function, a number that is divisible by, 3, 5, or both 3 and 5, respectively.
When the number is not divisible by 3 or 5, the number itself should be returned

There are several ways to solve this exercise. One possible fizzBuzz function:
function fizzBuzz(number){
return number % 15 == 0 ? "FizzBuzz" : number % 5 == 0 ? "Buzz" :
number % 3 == 0 ? "Fizz" : number;
};
This is how to test it:
alert(fizzBuzz(10));
alert(fizzBuzz(60));
alert(fizzBuzz(6));
alert(fizzBuzz(7));
I recommend trying the W3Schools JavaScript tutorial, it is easy to understand for beginners.

for (var i=1; i <= 20; i++) {
if (i % 15 == 0)
console.log("FizzBuzz");
else if (i % 3 == 0)
console.log("Fizz");
else if (i % 5 == 0)
console.log("Buzz");
else
console.log(i);
}
from here: https://gist.github.com/jaysonrowe/1592432

Related

Multiple conditions with logical AND operator in 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 1 year ago.
Improve this question
I'm new to JavaScript and trying to implement a logical AND statement such that the field has a character limit greater than 0 and less than or equal to 100. Where's my try:
document.getElementById('order-content').onkeyup = () => {
if (document.getElementById('order-content').value.length > 0 &&
document.getElementById('order-content').value.length <= 100 ) {
document.getElementById('order-button').disabled = false;
} else {
document.getElementById('order-button').disabled = true;
}
This is not working and the syntax is undoubtedly incorrect. Can anyone help me with the way to implement this properly?
Thanks!
You can try something like this:
The requirements of the length are length > 0 and length <= 100. The expression (length > 0 && length <= 100 ) will evaluate to true if the length passes the requirements. The exclamation point reverses the boolean.
document.getElementById('order-content').onkeyup = () => {
let length = document.getElementById('order-content').value.length;
if (!(length > 0 && length <= 100 )){
document.getElementById('order-button').disabled = false;
} else {
document.getElementById('order-button').disabled = true;
}

simplify function to a single return statement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm a bit confused as I was asked to simplify this function down to a single return statement, and I'm not exactly sure how I would do that as I'm not sure what the paradigm is for angular / typescript with this.
get remainingSpend() {
if (this.spend >= 0) {
if (this.spend- this.organization.total <= 0) {return '';}
return this.spend - this.organization.total;
} else {
return '';
}
}
You can simplify it with a ternary operator like so:
get remainingSpend(){
return this.spend < 0 || this.spend <= this.organization.total ? ''
: this.spend - this.organization.total
}
get remainingSpend() {
return (this.spend >= 0 && this.spend > this.organization.total) ?
this.spend - this.organization.total : ''
}

How to check for two conditions using && operator [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 5 years ago.
Improve this question
I'm trying to use 1 If statement to check that both values are not zero
if ((minvalue !== 0) && (maxvalue !== 0)) {
// Both are not 0
}
else
{
// Both values are 0
}
I can get it to work by using two if statements
if ((minvalue !== 0){
if(maxvalue !== 0){
// Both values are not zero
}
}
But I'm not sure how to do it in one If.
This will also work
if (minvalue || maxvalue) {
// Both are not 0
}else {
// Both values are 0
}
Edit :
If you example doesn't work, you should consider doing
console.log(minvalue,maxvalue);
Your code works, so that's your minvalue and maxvalue which are wrong. Might be strings
Your first code block should be fine. If you want to get a bit more clever about it you could instead test that the product of both values is not zero (as anything multiplied by zero will be zero).
const minvalue = 1
const maxvalue = 3
if (minvalue * maxvalue !== 0) {
console.log('foo!') // foo!
} else {
console.log('bar...') // [not hit]
}
Also, stylistically, it's considered bad practice to leave hanging curly braces. Move your else and subsequent opening block curly brace up a line (as above).

while loop javascript logic [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Can anybody explain this while loop for me? It part of the roman numeral challenge on free code camp - it's not mine, I did my own but it was much more code. I'm trying understand this one to improve my own. Anyway, the while loop is totally throwing me. Any help would be awesome.
function romans(num){
var roman = "";
var romanNumerals = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"];
var numbers = [1000,900,500,400,100,90,50,40,10,9,5,4,1];
for (var i=0; i<numbers.length; i++) {
//If the num was 5 then then it would read 5 greater than 1000?
while (num >= numbers[i]) {
roman = roman + romanNumerals[i]
//5 minus 1000?
num = num - numbers[i]
}
}
return roman;
}
console.log(romans(5))
num starts as the desired input, then you find the numbers that add up to num, starting at the biggest roman numeral (numbers[0]).
You repeat with the while loop because you might need multiple copies of the letter (i.e. "III" == 3)
romans(5) will not trigger the while loop until numbers[10] (because as you say 5 >= 1000 == false,) at this point you append the character V to roman, and subtract 5 from num. The while loop will never again trigger since num is now 0.
Try thinking through it with romans(3001) and you will see the while loop trigger 3 times for M/1000, then get skipped 10 times, then trigger once for I/1
function romans(num){
var roman = "";
var romanNumerals=["M","CM","D","CD","C","XC","L","XL","X",
"IX","V","IV","I"];
var numbers = [1000,900,500,400,100,90,50,40,10,9,5,4,1];
for (var i=0; i<numbers.length; i++){
//If the num was 5 then then it would read 5 greater than 1000?
console.log("comparing: " + numbers[i]);
console.log(" with: " + num);
while(num >= numbers[i]){
console.log("entered");
roman = roman + romanNumerals[i]
//5 minus 1000?
num = num - numbers[i]
console.log('subtracting: ' + numbers[i]);
}
}
return roman
}
console.log(romans(44))
It will go down the line of numbers until it finds one that is smaller: for example 44:
In the for it'll go through the numbers array until it finds a number then 44 (during the iterations before the while loop is never entered).
When it finds a number smaller then 44 it enters the while loop and takes the romanNumerals at the position of i in that iteration, in this case 40. Then it lowers the original number by 40 (44-40=4) and then exits the loop going back into the for to search for a number lower then 4.

javascript next number on multiples of three [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 7 years ago.
Improve this question
I have a number for example 4, i want to get next number on multiples of 3
Multiples of three: [3,6,9,12,15,18,21,24,27,30,...]
the result must be 6
i'm looking for a javascript function
something like this:
function (myNum) { //myNum = 4;
var multiples = [3,6,9,12,15,18,21,24,27,30];
var result;
// do something!!
return result; // returns 6
}
thanks
I suggest another solution:
function getNext(num, dep){
return (((num % dep) ? dep:0) - num % dep) + num;
}
document.write(getNext(4, 3));//6
//document.write(getNext(200, 7));//203
Updated: You can use this method for finding next number on multiples of any number
There are a lot of ways you can achieve this. Here is an easy one. Increment the number until you get a multiple of three.
function multipleOfThree(num){
while(num % 3 != 0)
num++;
return num;
}
You must try before asking a question. If you stuck at somewhere then it is good to ask questions with problem. By the way here what you can try:
function multiple(number) {
return number % 3 === 0 ? ((number/3) * 3) : parseInt((number/3) + 1) * 3;
}

Categories