Javascript function not working, what is wrong? [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I am learning javascript, now I'm trying to do mini-coding challenges. I cannot figure out what needs to change:
let roll = function() {
let roll1;
let roll2;
roll1 = parseInt(Math.random() * 6) + 1;
return roll1;
roll2 = parseInt(Math.random() * 6) + 1;
return roll2;
document.getElementById('dice').innerHTML = roll1 + "and " + roll2;
}

Your function roll is returning your roll1 and roll2 before it gets to update the html. Upon being executed return roll1 exits the function and returns the value of role1. The lines of code after it never get called. What you need to do is just remove the returns from the function. when getElementById() is called that will, in a way, act as you returning the variables because it will update the html to display your result.
let roll = function() {
let roll1;
let roll2;
roll1 = parseInt(Math.random() * 6) + 1;
roll2 = parseInt(Math.random() * 6) + 1;
document.getElementById('dice').innerHTML = roll1 + "and " + roll2;
}

Related

Why is my JavaScript code not accepted as the right answer? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I am trying to do this Javascript exercise: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/counting-cards
I am wondering why the solution below is not an accepted answer:
let count = 0;
function cc(card) {
// Only change code below this line
const low = [2, 3, 4, 5, 6];
const high = [10, 'J', 'Q', 'K', 'A'];
if (low.includes(card)) {
count += 1;
}
else if (high.includes(card)) {
count -= 1;
}
let decision;
if (count > 0) {decision = "Bet"}
else {decision = "Hold"}
return count + decision;
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
When I am comparing it to accepted answers I don't see what they are doing differently. One thing that is not clear to me in the assignment is that should return be called every time or only after the last function call (cc('A');).
Add a space between count and decision
return count + " " + decision;
You are giving an answer in the wrong format. Just missing the space between count and decision.
Incorrect:return count + decision;
Correct:return count +" "+ decision;

Why does random number generator not show on html page? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to get a random number of customers to "have gone on the website today". Why does the number not show on the page. I am sorry if this is something obvious but I am very new.
var customerNumber = document.getElementById("customer-number")
customerNumber.textcontent = randomCustomerNumber()
function randomCustomerNumber(){
var i = Math.floor(Math.random() * 36)
return(i)
}
<div class="customers-today">
<h4>You are customer number <span id="customer-number"></span> today.</h4>
</div>
element.textcontent => element.textContent
Also you should prob change return(i) to return i;, both work but the first one makes it seem like return is a function.
You need to use textContent instead of textcontent :
var customerNumber = document.getElementById("customer-number")
customerNumber.textContent = randomCustomerNumber()
function randomCustomerNumber(){
var i = Math.floor(Math.random() * 36)
return(i)
}
<div class="customers-today">
<h4>You are customer number <span id="customer-number"></span> today.</h4>
</div>
Try doing:
var customerNumber = document.getElementById("customer-number");
customerNumber.innerHTML = randomCustomerNumber();
function randomCustomerNumber(){
var i = Math.floor(Math.random() * 36);
return i;
}

How could i write this random number generating button/div syntax in another way? [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 3 years ago.
Improve this question
I have the following code:
// Reference to the <div> which displays the random number:
var rndDiv = document.getElementById('rndNum')
// Reference to the <button> which generates the random number:
var rndBtn = document.getElementById('rnd')
// Generating the random number through 'click' eventlistener:
rndBtn.addEventListener('click', function intRnd() {
var n = Math.floor((Math.random() * 10) + 1);
console.log(n)
rndDiv.innerHTML = n
})
how could/should i write this code differently, how would you write it? Would you use, for example, arrow functions? let instead of var? I'm just curious. Also i'm the total opposite of a 'pro'-coder, just a beginner, and would like to read your code to this solution.
Thanks for taking your time and reading my post!
Here you go ... !
IIFE
Arrow Function
Let
(function() {
let rndBtn = document.getElementById('rnd');
rndBtn.addEventListener('click', () => {
let rndDiv = document.getElementById('rndNum');
rndDiv.innerHTML = Math.floor((Math.random() * 10) + 1);
});
})();
<button id="rnd">Click</button>
<div id="rndNum"></div>
Here is another way
const randomNumGenerator = () => Math.floor((Math.random() * 10) + 1);
const randomNumDiv = document.getElementById('rndNum');
document.getElementById('rnd').addEventListener('click', () => {
randomNumDiv.innerHTML = randomNumGenerator();
});

Javascript: I am trying to write a calculate function that will "add" two numbers and print the result out as a string, but my syntax isn't working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to write a calculate function that will "add" two numbers and print the result out as a string, but my syntax isn't working. Can you please look at my code and tell me how I can fix it? Later I would like to add more mathematical functions like divide and multiple that can be plugged in. Thanks.
var add = function(x,y){
return x+y;
};
var calculate = function(string,x,y){
if(string === "add"){
var result = console.log(x + "+ " y + "= " + add(x,y));
return result;
}
};
calculate("add",5,6);
You have missed a + sign before y.
This:
var result = console.log(x + "+ " y + "= " + add(x,y));
must be
var result = console.log(x + "+ " + y + "= " + add(x,y));
Anyway, your function does not look correct. console.log returns nothing, and your result variable will always be evaluated to undefined, and function will always return undefined as well.
Also, your calculate function does actually calculate and output result which can be not that transparent for someone who calls this method and breaks single-responsibility principle.
Probably, it is a better idea to separate calculation and presentation:
function add(x,y){
return x+y;
}
function calculate(action,x,y){
if(action === "add") {
return x + "+ " + y + "= " + add(x,y);
}
}
var result = calculate("add",5,6);
console.log(result);
You may also want to replace if (action === "add") with switch statement in the future when you will have more than two actions :)

Error in my 'if' statement containing break (wrox ch 3 exercise 4) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Trying to figure out this times table function...It's not working as there seems to be a problem with the if statement containing break. Any help would be much appreciated.
function writeTimesTable (startNumber, endNumber, multNumber) {
for (;startNumber <= endNumber; startNumber++) {
console.log(startNumber + " * " + multNumber + " = " + startNumber * multNumber + "</br>")
}
}
/* writeTimesTable(3,4,5) */
var timesTable;
while ( (timesTable = prompt("Enter the times table", -1)) != -1)
while (isNaN(timesTable) == true) {
timesTable = prompt(timesTable + " is not a valid number, please retry", -1);
};
if (timesTable == -1) {
break;
};
console.log("<br />The " + timesTable + " times table<br/>)");
writeTimesTable(timesTable, 1, 12);
you're not using brackets in your outer loop. without brackets only the first statement is executed in the loop.
while ( (timesTable = prompt("Enter the times table", -1)) != -1){
while (isNaN(timesTable) == true) {
timesTable = prompt(timesTable + " is not a valid number, please retry", -1);
}
if (timesTable == -1) {
break;
}
}

Categories