Why is my JavaScript code not accepted as the right answer? [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 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;

Related

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;
}

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

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;
}

Value not assigned in while loop [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
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

Reverse The Odd Words in the sentence [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 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);

how to find no. of similar characters in two strings in javascript [closed]

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 need to find the no. of similar characters between two strings using JS/PHP
Example
str1: Mack
str2: Michelle
Similar Characters: "M" "C"
similar character count: 2
I'll do this:
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
var counter = 0;
var min = Math.min(str1.length, str2.length);
for(var i = 0; i < min; i++)
{
if(str1.charAt(i) === star2.charAt(i))
counter++;
}
alert("Result: "+counter);
After your precision, here is my solution:
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
var counter = 0, find = -1;
for(var i = 0; i < str1.length; i++)
{
find = str2.indexOf(str1.charAt(i));
if(find > -1)
{
counter++;
str2 = str2.substr(0, find) + str2.substr(find + 1);
}
}
alert("Result: "+counter);
It works with the 2 examples you gave us:
Michellle and Michelle = 8
Sneha and naveen = 3
Use similar_text() function
<?php
echo "character count: ". similar_text("Mack","Michelle");
?>
Output
character count: 2
I'd need more examples to make sure this works, but using regular expressions might work for you:
function similar_text_regex(str1, str2){
var regEx = new RegExp('['+str2+']', 'gi');
var matchCnt = str1.match(regEx2).length;
return matchCnt;
}
console.log(similar_text_regex('Michelle', 'Michellle')); // --> 8
console.log(similar_text_regex('Jason', 'Jane')); // --> 3
console.log(similar_text_regex("sneha","naveen")); // --> 3

Categories