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
So in the following code a= -1 so the statement in the while loop should be -1, but the while loop is never entered. I have looped through but don't understand why the while loop is never executed. I am sure this is frustratingly simple.
function translatePigLatin(str) {
var newStr = str.split("");
var vowels = ["a","e","i","o","u"];
var i = 0;
var a = vowels.indexOf(newStr[0]);
while (vowels.indexOf(newStr[i]) != -1) {
i = i++;
}
if(i===0){
return str + "way";
}
else{
str = str.substr(i) + str.substr(0,i) + "way";
}
//return str;
}
translatePigLatin("consonant");
In the while loop you are checking vowels.indexOf(newStr[i]) != -1 which mean that if vowels.indexOf(newStr[i]) is not -1 then while loop should bve executed. and as you say the expression results in -1 the loop should not be executed which is a expected behaviour.
Change the condition to
vowels.indexOf(newStr[i]) == -1 if you want to execute the while loop is the value returned is -1
Related
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;
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'm executing this code, although it produces an error.
function getUserPosts(){
$.get("../php/PostHandler.php", function(data) {
var posts = JSON.parse(data);
alert(posts);
for(var x = lastNumPosts; x <= posts.length; x++){
var postLine = "<div class='user-post'><div class='user-p-info'><img src='imgs/email-icon2.png'><h2> "+ posts[x].name +" </h2><br> <small>Posted Tuesday 23/10</small></div><div class='user-p-content'><p>" + posts[x].pContent + "</p></div></div>";
$('.post-zone').prepend(postLine);
}
});
}
TypeError: posts[x] is undefined
Any solutions?
Change this line:
for (var x = lastNumPosts; x < posts.length; x++) {
Notice the < instead of <=.
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 7 months ago.
Improve this question
I am trying to reverse the odd words in the sentence. Its working fine. But its appending undefined in the front.
var str = "get busy living or get busy dying.";
var newstr = str.split(" "), result;
for(i=0;i<newstr.length;i++){
if(i%2 !== 0){
result += newstr[i].split("").reverse().join("");
result += ' ';
} else {
result += newstr[i];
result += ' ';
}
}
Output is
undefinedget ysub living ro get ysub dying.
Can some one point me where i am going wrong!!!!!
You're not initializing result, so the first += adds something to an undefined value.
Just declare and initialize result:
var result = "";
You could also do it more concisely:
var str = "get busy living or get busy dying.";
var result = str.split(" ").map(function(word, i) {
return i % 2 == 0 ? word : word.split("").reverse().join(""); }).join(" ");
alert(result);
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
When I run this with any value for customerNumber, it always returns the first alert message in the if statement. What is wrong here?
var customerNumbers = 13;
var winningNumbers = [];
winningNumbers.push(12, 17, 24, 37, 38, 43);
var match = false;
for (i=0; i<winningNumbers.length; i++) {
if (winningNumbers[i] == customerNumbers) {
match = true;
}
}
if (match = true) {
alert("This Week's Winning Numbers are:\n" + winningNumbers.toString() +
"\nThe Customer's Number is:\n" + customerNumbers + "\nWe have a match and a winner!");
}
else {
alert("This Week's Winning Numbers are:\n" + winningNumbers.toString() +
"\nThe Customer's Number is:\n" + customerNumbers + "\nSorry, you are not a winner this week.");
}
You are not comparing the boolean match to true your are assigning true to the boolean match. You need to use a double or triple =.
match = true // Sets match to true
match == true // Compares match to true
match === true // Strictly compares match to true
So add some more equal signs and it should work.
Do not use the assignment operator(=) in the if statement. Use the equality operator (===).
if (match === true)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a simple code gotten from the internet and it did not answer what I really wanted as output. I have two input fields; one for the input and another for the output and they are processed through this function:
<script type="text/javascript">
function AnEventHasOccurred() {
var x = document.getElementById("onkeyup").value
if (x >= "100") {
document.getElementById("eventlog").value = "" +
return x = ['Generalities'];
}
}
</script>
What I'm really needing is that when I enter numbers below 100, output must be Generalities. I haven't got it correctly. And I went here to ask some help. Thanks.
You're never outputting your value back into the output field. All you're doing is returning the value. You need to set the value of your output field to "Generalities".
Example
var input = document.getElementById("onkeyup").value;
// You should be giving your elements meaningful IDs.
if(+input < 100) {
document.getElementById("output").value = 'Generalities';
// Assumes an output field called "output".
}
Try this:
function AnEventHasOccurred() {
var x = document.getElementById("onkeyup").value;
if (x < 100){
document.getElementById("eventlog").value = "Generalities";
}
}
I see a few errors. Check this out for comparison:
function AnEventHasOccurred() {
// should probably save the elements to variables
// since you'll be checking and changing the values
var x = document.getElementById("onkeyup");
var y = document.getElementById("eventlog");
// should be 100, not "100"
if (x.value < 100) {
y.value = "Generalities";
} else {
y.value = "";
}
}
This should work fine. Check it out on jsfiddle.
More Recommendations
Your return statement doesn't correspond with your "output": it
does nothing valuable in this case.
You check or set the value of an input by getting the element and
using its value key.
You should put semi-colons at the end of most javascript lines, with the exceptions generally being curly brackets {}, comments // and /* */, and empty lines.