6.6.5 Codehs level up [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
Improve this question
I messed by code up somehow but i dont know how and/or what to fix, it needs to increase each level and print it but it does not and it crashes on lvl3
function main() {
const NUM_LEVELS = 3;
let level = readLine("What Level are you on? ");
if (level == 1)
playLevel1();
else if (level == 2)
playLevel2();
else if (level == 3)
playLevel3();
while (level >= 3)
console.log(level);
level++;
}
//create playLevel1 function
function playLevel1() {
let level = 1
console.log("You are on level " + level);
console.log("You run through the dark and scary cave!");
console.log("You level up!");
console.log("****");
playLevel2();
}
//create playLevel2 function
function playLevel2() {
let level = 2
console.log("You are on level " + level);
console.log("You make it through the fiery lava world!");
console.log("You level up!");
console.log("****");
playLevel3();
}
//create playLevel 3 function
function playLevel3() {
let level = 3
console.log("You are on level " + level);
console.log("You jump from cloud to cloud in the sky world!");
console.log("You level up!");
console.log("****");
console.log("YOU WIN THE GAME! Thanks for playing!")
}
main();
i tried rewording, redoing let's, and i tried redoing the while loop but im kinda lost here

Related

Javascript execute a function of the number of indexes in two arrays are equal? [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 3 years ago.
Improve this question
I'm trying to execute a function when two arrays have an equal length that is greater than zero.
My program assigns the class of 'letter' to each list item in the phrase div. If a button press matches a letter in that div, that letter gets assigned the class 'show' which reveals it to the user.
I don't understand why my win condition isn't being met. Is there something wrong with the way I'm comparing the length of the two indexes?
I've included my codePen here: https://codepen.io/Azo3307/pen/vPjwxr
My checkWin() function is on line 100, and then it is called on line 143 after each button press is triggered.
// Check win condition
function checkWin() {
let showClass = document.getElementsByClassName('show');
let lettersClass = document.getElementsByClassName('letters');
if (showClass.length == lettersClass.length && showClass.length > 0 && lettersClass.length > 0) {
console.log('you win');
addElement('win', `You Win!`, `"${phraseArray}" is correct! `);
} else if (missed == 5) {
console.log('you lose');
addElement('lose', 'Game Over!', 'Better luck next time!');
}
}
Any help is much appreciated. Thank you.
This is because you're asking for let document.getElementsByClassName('letters') instead of letter:
<li class="letter show">t</li>
Second point, if showClass.length == lettersClass.length and showClass.length > 0, you don't need to test lettersClass.length > 0 ;)
function checkWin() {
let showClass = document.getElementsByClassName('show');
let lettersClass = document.getElementsByClassName('letter');
if (showClass.length == lettersClass.length && showClass.length > 0) {
console.log('you win');
addElement('win', `You Win!`, `"${phraseArray}" is correct! `);
} else if (missed == 5) {
console.log('you lose');
addElement('lose', 'Game Over!', 'Better luck next time!');
}
}
Last one, it's easy to know the text before trying anything just by selecting all the block ;) You should display:none instead of put it transparent.
Line 102:
let lettersClass = document.getElementsByClassName('letters');
rename to
let lettersClass = document.getElementsByClassName('letter');
Looks like just small typo. Btw code looks great, thumbs up.

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

Count up to 1000000 with javascript with img tag [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I got an image of all number from 0 to 9. I want to count up from 0 to 1000000 with img tag. For example if number == 1 position of img tag would be top = 100px
var count = 0;
var counter = function() {
if (count == 1000) {
count = +1;
$(".number-inner").text(count);
}
count++;
$(".number-inner").text(count);
setTimeout(counter, 10)
}
setTimeout(counter, 1000)
Any ideas?
Hi you need to cut your image to 9 images, so it will be each image have a number then you can name the image with number, for example the image who had number 4 name it 4.png .... Here is my code and project folder to see what i did
code:
var i=0, j=0;
var count = setInterval(function(){
if(i!=9){
i++;
}
else{
j++;
i=0;
}
$('.stop').html(j+''+i).hide();
$('.counter').html('<img src='+j+'.png><img src='+i+'.png>');
if($('.stop').html()==14)
clearInterval(count);
},1000);
just remove 14 to 10000 ( or whatever you want )
image:
result ( the counter stop at number 14 ):
hope my answer usefull

best way to compare 2 variables with 4 values each [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have 2 variables... both of them can get a status (0,1,2,"xxx")
What would be the best way to compare these variables? Or are a lot of if's and else if's the best way?
if((Status1 && Status2) == 2){
connectorImage = "connector.png";
}
else if((Status1 && Status2) == 1){
connectorImage = "connector_grey.png";
}
else if(Status1 == 1 && Status2 == 2){
connectorImage = "connector_greytogreen.png";
}...
You could try something like
if(Status1 == Status2){
//Do something
}else if(Status1 < Status2){
//Do something else
}else if(Status2 > Status1){
// Do something else
}
This should work, unless if all 16 combinations require a unique action. Then you're stuck with lots of if's and else's
it depends on what exactly you need to compare.
e.g.: If you need to get the correct image to a status i would use something like this:
function getImageByStatus(int status) {
switch (status) {
case 1:
return "connector.png";
case 2:
return "connector_grey.png";
}
return "";
}
In case you have more complex comparisons, your only way might be some if-statements
You can construct connector image from statuses:
var connectorImage = status1 + "_" + status2 + ".png";
and rename you images to something like "0_0.png", "1_0.png", etc.
Or rename your status to something more meaningfull
Or create a mapping between combined statues and connector names:
var statusesToConnectorImages = {
"0_0" : "connector.png",
"0_1" : "connector_grey.png"
}
var connectorImage = statusesToConnectorImages[status1 + "_" + status2];

Simple clock that counts down from 30 seconds and executes a function afterward [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a game that gives a time limit and I need to display a countdown clock for the users and stop the game once the time is up such as 30 seconds. How can I do this in javascript?
Use setInterval to set up a timer. Within this timer, you can update some text in your page and when the time is up, you can call whatever function you want:
var timeLeft = 30;
var elem = document.getElementById('some_div');
var timerId = setInterval(countdown, 1000);
function countdown() {
if (timeLeft == -1) {
clearTimeout(timerId);
doSomething();
} else {
elem.innerHTML = timeLeft + ' seconds remaining';
timeLeft--;
}
}
<div id="some_div">
</div>
Check out setTimeout and setInterval:
http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
You can use setTimeout() function for this like :-
let timeElm = document.getElementById('timeElm');
let timer = function(x) {
if(x === 0) {
return;
}
timeElm.innerHTML = x;
return setTimeout(() => {timer(--x)}, 1000)
}
timer(30);
In this, we use recursion and gave it a setTimeout() of 1000ms for reducing the time.

Categories