Javascript unexpected outcome [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 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 + '!');
}

Related

how to display something if the answer is 24 [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 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>

"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");

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.

JavaScript: Unexpected token [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 8 years ago.
Improve this question
Introduce the problem before you post any code:
I have a simple JavaScript code and it's giving me an unexpected token error. It's code that I'm trying to convert from PHP to JavaScript for use in a Phonegap application.
Specifically it is the line that declares the $ex_data variable. And it says the unexpected token is the section between the $system and $galaxy variables.
Code
for ($galaxy = 1; $galaxy < 21; $galaxy++){
for ($system = 1; $system < 601; $system++){
var $ex_data = '{"planet_id":-1,"sid":'.$system.',"language":"en","gid":'.$galaxy.'}';
var $url = "http://54.193.106.113/ING004/android1/WebServer/Web/sogame/newControl/nmUniverse/getUniverse?sign=".toUpperCase($sign);
}
}
CLEAR QUESTION:
How do I fix the line so that it is valid?
You are confusing PHP concatenation with JavaScript (C) concatenation.
for ($galaxy = 1; $galaxy < 21; $galaxy++) {
for ($system = 1; $system < 601; $system++) {
var $ex_data = '{"planet_id":-1,"sid":'
. $system . ',"language":"en","gid":' . $galaxy.'}';
var $url = "http://54.193.106.113/ING004/android1/WebServer/Web/sogame/newControl/nmUniverse/getUniverse?sign="
.toUpperCase($sign);
}
}
Change the ".s" to "+s".
var $ex_data = '{"planet_id":-1,"sid":'
+ $system + ',"language":"en","gid":' + $galaxy + '}';
or alternatively
var $ex_data = JSON.stringify({
"planet_id" : -1,
"sid" : $system,
"language" :"en",
"gid" : $galaxy
});
Also, as pointed out below, change .toUpperCase($sign); -> + $sign.toUpperCase();
var $ex_data = '{"planet_id":-1,"sid":' + $system + ',"language":"en","gid":' + $galaxy +'}';
var $url = "http://54.193.106.113/ING004/android1/WebServer/Web/sogame/newControl/nmUniverse/getUniverse?sign=" + $sign.toUpperCase();

Get values of Input[type=(type)] from specific class with jQuery [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 8 years ago.
Improve this question
I have this code that does not work:
kw _class = _keyword1;
var text = $("'input." + kw_class + "[type=text]'").val();
var val = $("'input." + kw_class + "[type=hidden]'").val();
Firefox console comes out with this:
`Syntax error, unrecognized expression: 'input._keyword1[type=text]'
I have tried at least three combos of this that are not working that I found from other questions.
Yes because you have ' ' inside of the selector. It should be:
var text = $("input." + kw_class + "[type=text]").val();
var val = $("input." + kw_class + "[type=hidden]").val();
you have extra '' in the selector
var text = $('input.' + kw_class + '[type=text]').val();
var val = $('input.' + kw_class + '[type=hidden]').val();

Categories