how to display something if the answer is 24 [closed] - javascript

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 1 year ago.
Improve this question
I need to create an output that displays "that's correct" if the input is 24 and "wrong" if the input is something else.
This is a school project
<script>
function getText(id){
var text = document.getElementById(id).value;
return text;
}
function showReply(id, reply){
document.getElementById(id).innerHTML = reply;
}
function reply(){
var textFromUser = getText("myTextField");
var reply;
if (reply == "24") {
reply = "that is correct";
} else {
reply = "wrong"
}
showReply("output", reply);
}
</script>
This is what I have so far.
As I am a beginner pls forgive me for my sloppy code. thx in advance

function getText(id){
var text = document.getElementById(id).value;
return text;
}
function showReply(id, reply){
document.getElementById(id).innerHTML = reply;
}
function reply(){
var reply = getText("myTextField");
if (reply == 24) {
reply = "that is correct";
} else {
reply = "wrong"
}
showReply("output", reply);
}
<input type="text" id="myTextField" onkeyup="reply()">
<p id="output"></p>

Related

i get undefined result when i want to get the value from input box [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 want to get the value from the input in the html code and edit it, then put it in the div, but I get undefined.
HTML
<input type="text" id='doller'>
<button onclick="convertUsdToRiyal()">convert usd to Riyal</button>
<div id="result"></div>
<script src="js1.js"></script>
JS
function convertUsdToRiyal() {
'use strict';
var amount = document.getElementById('doller').Value,
result = 3.75 * amount,
massage = document.getElementById('result');
massage.innerHTML = amount;**//here i get undefined**
}
The "value" attribute should be in lower case, because javascript is a case sensitive language. So the instruction will be :
var amount = document.getElementById("doller").value.
Here is the code in es6 (ECMAscript 2015) for the convertUsdToRiyal() function:
convertUsdToRiyal = () => {
let amount = document.getElementById("doller").value;
result = 3.75 * amount;
massage = document.getElementById("result");
massage.innerHTML = result;
};
or just simply:
convertUsdToRiyal = () => {
document.getElementById("result").innerHTML = 3.75*document.getElementById("doller").value;
};
document.getElementById('doller').Value should be changed to document.getElementById('doller').value.
Value should be lowercase.
/*global console*/
function convertUsdToRiyal() {
var amount = document.getElementById('doller').value,
result = 3.75 * amount,
massage = document.getElementById('result');
console.log(amount);
massage.innerHTML = result;
}
<input type="text" id='doller'>
<button onclick="convertUsdToRiyal()">convert usd to Riyal</button>
<div id ="result"></div>

Javascript unexpected outcome [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 doing an excercise that challenges me a bit, and I just thought that might be a good idea to ask for some help here. I know it might be very easy question for the majority of you, but as I am new to coding hope this won't botter you too much.
Below is my code associated with the excercise. The code shouldn't run in my opinion as the var weapon is not specified. The problem is that the code runs and the console visualises some outcome.
I would assume there is something wrong with my conditional statements, but might be wrong..
var room = 'gallery';
var suspect = 'Ms. Van Cleve';
var weapon = '';
var solved = false;
if (room === 'billiards room') {
weapon = 'pool stick';
if (suspect === 'Mr. Parkes')
solved = true;
} else if (room === 'gallery') {
weapon = 'trophy';
if (suspect === 'Ms. Van Cleve')
solved = true;
}
if (solved) {
console.log(suspect + ' did it in the ' + room + ' with the ' + weapon + '!');
}
//Outcome is "Ms. Van Cleve did it in the gallery with the trophy!"
Your code result does not depend on weapon. Have a look at what you have defined
var room = 'gallery';
var suspect = 'Ms. Van Cleve';
var weapon = '';
var solved = false;
and the else if part of your code
} else if (room === 'gallery') { // TRUE
weapon = 'trophy';
if (suspect === 'Ms. Van Cleve') // TRUE
solved = true; // so now solved is true
}
As solved is true you get console output because
if (solved) {
console.log(suspect + ' did it in the ' + room + ' with the ' + weapon + '!');
}

"Missing ) after..." on a line, Javascript [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
So I am creating a "Silly Story Generator" in Javascript and after fixing a few errors that popped up I encountered "SyntaxError: missing ) after argument list"
After reading more about it I learned that it occurs when there is an error with how a function is called. This might be a typo, a missing operator, or an unescaped string.
I checked my code and I cannot seem to find the mistake, string on line 38 looks okay.
Thank you.
randomize.addEventListener('click', result);
function result() {
if (customName.value !== '') {
let name = customName.value;
}
if (document.getElementById("uk").checked) {
let weight = Math.round(300);
let temperature = Math.round(94);
}
story.text = ""
story.style.visbility = 'visible';
var newStory = storyText;
let xItem = randomValueFromArray;
let yItem = randomValueFromArray;
let zItem = randomValueFromArray;
function newStory(buttonPress) {
newStory.contentString.replace("insertX", "insertY", "insertZ")
content.contentString.replace("xItem ", "yItem", "zItem");
}
}
Your Code is Badly formatted.
At newStory.contentString.replace("insertX", "insertY", "insertZ";)
You had a semi-colon inside the the parenthesis.
You are also missing two curly braces near the end.
I suggest getting a good IDE or using the formatting features that come with the one you use.
randomize.addEventListener('click', result);
function result() {
if (customName.value !== '') {
let name = customName.value;
}
if (document.getElementById("uk").checked) {
let weight = Math.round(300);
let temperature = Math.round(94);
}
story.text = ""
story.style.visbility = 'visible';
var newStory = storyText;
let xItem = randomValueFromArray;
let yItem = randomValueFromArray;
let zItem = randomValueFromArray;
function newStory(buttonPress) {
newStory.contentString.replace("insertX", "insertY", "insertZ")
content.contentString.replace("xItem ", "yItem", "zItem");
}
}
you have written a semicolon before the closing parentheses
newStory.contentString.replace("insertX", "insertY", "insertZ");

Trying to replace input value results in TypeError: input.replace is not a function [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 4 years ago.
Improve this question
For some reason this code is responding in a error of "input.replace is not a function" when submit button is pressed
window.onload = function() {
var input = document.getElementById('input');
var submit = document.getElementById('submit');
var newStr;
var placeholder = document.getElementById('placeholder');
submit.addEventListener("click", function(){
newStr = input.replace(/n/g , "m");
var newText = ("Sorry! Wrong Answer, the name is spelled United
Kingdom. Not:" + input)
document.getElementById("placeholder").textContent = newText;
});
}
var input = document.getElementById('input'); returns DOM element not value.
update it with.
var input = document.getElementById('input').value;
But move it inside of the function(){ because if you leave it being called at the start of the page loading you will be setting input to the value of the input field at the time the page loads and not the value at the time the submit button is clicked
Try changing it to this
window.onload = function() {
var input = document.getElementById('input');
var submit = document.getElementById('submit');
var newStr;
var placeholder = document.getElementById('placeholder');
submit.addEventListener("click", function(){
newStr = input.value.replace(/n/g , "m");
var newText = ("Sorry! Wrong Answer, the name is spelled United
Kingdom. Not:" + input.value)
document.getElementById("placeholder").textContent = newText;

Javascript "variable" is not defined [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 4 years ago.
Improve this question
I've written a script that takes grades from a user for different units and then adds them to an array, shows the grades and also an average.
When I run the script it works fine, but instead of an average, it displays 'grade' is not defined.
var subjects = ["CF:", "DaD:", "PoP:", "N&CS:", "SAD:", "APP:"];
var grades = ["", "", "", "", "", ""];
var result;
grades[0] = prompt("Please enter your marks for CF: ");
grades[1] = prompt("Please enter your marks for DaD: ");
grades[2] = prompt("Please enter your marks for PoP: ");
grades[3] = prompt("Please enter your marks for N&CS: ");
grades[4] = prompt("Please enter your marks for SAD: ");
grades[5] = prompt("Please enter your marks for APP: ");
console.log("Units and grades are: ");
console.log(subjects[0] +"\t"+ grades[0] );
console.log(subjects[1] +"\t"+ grades[1] );
console.log(subjects[2] +"\t"+ grades[2] );
console.log(subjects[3] +"\t"+ grades[3] );
console.log(subjects[4] +"\t"+ grades[4] );
console.log(subjects[5] +"\t"+ grades[5] );
result = grades[0] + grades[1] + grades[2] + grades[3] + grades[4] +
grade[5];
console.log("Average: " + "\t" + result / 6);
anyone have any ideas?
Apologies for the bad javascript, i'm very new to this.
In this code,
result = grades[0] + grades[1] + grades[2] + grades[3] + grades[4] + grade[5];
grade[5] must be grades[5]. You missed "s" point.

Categories