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 8 years ago.
Improve this question
I created a simple fuzzbuzz in javascript (see below). I however would like to include the following:
if the number starts with a 1 (so fe 11) "ping" should be added. So 15 should be FizzBuzzPing etc...
Any thoughts?
function fizzBuzz() {
for (var i=1; i <= 20; i++)
{
if (i % 15 == 0)
alert("FizzBuzz");
else if (i % 3 == 0)
alert("Fizz");
else if (i % 5 == 0)
alert("Buzz");
else
alert(i);
}
}
$(document).ready(function(){
$('#clickMe').click(function(){
fizzBuzz();
});
});
Convert the number to a string and take the first index of the string:
var digit = (''+i)[0];
Or, the alternative
var digit = i.toString()[0];
Then check if digit is equal to 1 or not and add things or not accordingly.
For a future reference: Spend some time searching for a solution to your problems, don't ask questions unless you've spent some time making sure an answer does not exist to your question. A similar question has been answered many times before. Maybe it's not about fuzzbuzz but you should be able to find two different answers to two different questions and be able to combine then into your solution.
Related
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 4 years ago.
Improve this question
I wrote a for loop in JavaScript which is simple and straight forward. But, it is skipping the 2nd index. I don't understand why?
Below is my code snippet :
if($scope.usersChoice.length == $scope.correctAnswers.length){
for(var p=0;p<$scope.usersChoice.length;p++) {
if($scope.usersChoice[p] == $scope.correctAnswers[p]){
$scope.score++;
}
}
}
Here the length is 10.
How do you know it is skipping the 2nd index? Because it should'nt.
Can you show us your arrays ?
By the way, you can use forEach instead of for loop in this case, since you don't seem to need to break your loop:
if($scope.usersChoice.length == $scope.correctAnswers.length){
$scope.usersChoics.forEach($choice,$index=>{
if($choice === $scope.correctAnswers[$index]) $scope.score++;
});
}
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 6 years ago.
Improve this question
If you had an array with 5 items, what would be the best way to check if those items are in order? Say 1-5 or 6-11?
EDIT
Knowing a loop will work, I'm wondering if there might be an alternative solution.
Loop through, if the current is less than the previous return false.
function consecutive(arr)
{
for(var i = 1; i < arr.length; i++){
if(arr[i] < arr[i-1]) return false;
}
return true;
}
console.log(consecutive([10,11,45,100]));
console.log(consecutive([10,3,11,35]));
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
I have a situation where I get 4, 5 or 6 images/tiles.
Depending on the number of tiles, I need to format the images on the webpage.
Like this http://prntscr.com/9y75dw
If it's five images, I have to format it in such a way that two images in the first row and three images in the second row. Can you help me with the logic?
Well I don't see a technique, maybe I am missing to do that more appropriately or in a generic way but since the description in less and number of images given are random, I don't know how this will work.
var imageLength = $('img').length;
var newLength = 0, differenceLength=0;
if(imageLength%2==0){
//incase of even number
//Do what you like here eg: $('img').css('width', '50%');
}
else{
// incase of odd number
newLength = Math.round(imageLength/2); //dividing number into two parts.
differenceLength = imageLength - newLength; //difference to put smaller above and greater below.
$('parent-div img:nth-child(1)').nextUntil('img:nth-child('+differenceLength+')').wrapAll('<div></div>') //wraps into a container div
}
Although this is just one way. You might have already realized a lot of logic by now.
PS: I have randomly written this code so take it as a logic for help. Not sure whether this will work.
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 7 years ago.
Improve this question
Practice Problem 1
Parameters: The function accepts two positive integers N and M.
Return Value: The function returns the product of N and M. For example, if the integers 5 and 8 are supplied to
that function, it should return the product of 5 and 8—it should return 40.
Additional
Requirements:
Do this without using the multiplication operator (*). Hint: Multiplication is just a series of addition
operations.
function mult(N, M) {
return N / (1 / M);
}
Since this is a basic excercise, I think that this answer is not expected (but maybe you'll get bonus points if you can explain it), even though it does the math without *.
function mult(N,M){
var a = new Array(N);
return a.join(""+M).split("").reduce((x,y)=>(parseInt(x)+parseInt(y)))+M
}
Note: This does not work for N < 3. No time to correct it.
OK. Look. It sounds like you're quite young so I think giving you the benefit of the doubt is alright here. So you know for the future: Stackoverflow isn't a site that you can just drop a homework question and expect people to do the work for you.
We sometimes do help with homework questions but only if it looks like you've at least attempted to answer the question yourself, by showing us some code that you've written. If you want to use SO in the future you might find the help section useful, particularly the section on how to write a good question.
OK, lecture over.
What the question is asking about is how to use a simple for loop to add some numbers together:
function getProduct(num1, num2) {
// set total to zero
// we'll be adding to this number in the loop
var total = 0;
// i is the index, l is the number of times we
// iterate over the loop, in this case 8 (num2)
for (var i = 0, l = num2; i < l; i++) {
// for each loop iteration, add 5 to the total
total += num1;
}
// finally return the total
return total;
}
getProduct(5, 8); // 40
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 7 years ago.
Improve this question
How to use a while loop to generate random numbers until a certain number is reached? Help please
This code will generate integer numbers from 1 until 50.
var number = 50;
var x = 0;
while(x !== number){
x = Math.floor((Math.random() * 50) + 1);
console.log(x);
}
Considering there is a random number generator random you will just do as shown below
do
{
x=Math.random();
x=x*range;
if(x is desired number )
break;
else
{
print the number.
}
}while(1)
print -( desired number found).
NOTE: As you are beginner you can try the following when designing these codes..
First find what is the input and what is the output.
Then try to draw a flow chart of the whole thing ( You can try running it yourself as if you are the computer)
Then all the branch and loop are converted to if-else or for-while loop.
Check the code and match input and output.
This is a basic program structure..Instead of asking in forums about common structures try reading some books.
Hint 1: There is a function in javascript called Math.random() return a random number between 0 (inclusive) and 1 (exclusive):
Hint 2: check whether the values are equal. Use === in javascript.
Hint 3: check for if else, break and do while, while loop.
Hint 4: If you are given a random number 0<=x<1 then how do you generate a random number using in range [a,b) using the abobe geerated number? Ans: Use b*x.
hope this helps.