I want to prompt the user for a number. If the number is less than 10, I want to prompt the user to choose another number.
let question = Number(prompt("Please pick a number"));
console.log(question);
I started my code like the image above, this console logs the response as a number and not a string.
When I format the (while, or do while loop) I get problems and my console log starts running infinitely.
After researching this, there are two things that I don't understand,
Why is it running infinitely?
Secondly, I don't know how to get the prompt to repeat itself.
Thank you
I tried research W3, and other sites but couldn't find an example that included a prompt.
First of all thanks for responding, and here I think I can answer.
let question = Number(prompt("Please pick a number"));
while (question < 10){
question = Number(prompt("Please pick a number"))
}
I made a variable equal to a number value for my string
"Please pick a number."
In the while loop, it is self explanatory, the part that was confusing me earlier was not knowing what to write within the curly brackets. When I wrote question by itself, the computer froze.
Then after asking my teacher, she said to add another prompt.
So we added question = Number(prompt("Please pick a number")).
This repeated the loop and asked the same string while converting it to a number which allowed it to be checked again to see if the value is less than 10.
Thank you for helping me with my question.
try this code
let question = "Pick a number ";
let answer =Number(prompt(question));
while (answer < 10){
answer = Number(prompt(question));
}
Here I am storing the input value from the prompt to the variable named answer
while-loop will keep calling the prompt until the value of the answer is greater than or equal to 10
Hope this solves your problem
Related
I'm getting an error with my code below. I am trying to make a calculator, and I am using strings as input and typecasting in order to solve. The calculator was working fine, but now I am trying to add an ans button to allow for having equations based on previous answer. I am having an issue with the substrings now, and in equations where ans comes first like 'ans+88" I get NaN as a result and 88 turns into undefined. But when I switch the substring parameters it messes it up for other types of equations as described in the comments below.
link to repository
else if(equation.indexOf("+") != -1){
//this finds the the index of +
let pInd = equation.indexOf("+")
/*before I added this, it worked properly, firstValue would get the firstvalue
secondvalue would get the second value and it added correctly
the point of the if statement is to replace ans with props.prevResult which is
the previous answer from the previous entered equation. After it replaces it
removes the whitespace*/
if(equation.includes("ᴀɴs")){
equation = equation.replace(/ᴀɴs/gi, String(props.prevResult))
//my attempt to remove whitespace since i thought it might be the issue
// but now I'm pretty sure the issue is with substring.
equation = equation.replace(/\s/g, '')
}
equation = String(equation)
firstVal = equation.substring(0,pInd)
//this line is where the error seems to come from
secondVal = equation.substring(pInd+1,equation.length)
/* when ans is first such as in 'ans + 9" 9 becomes undefined ans stays
correct. I tried replacing it with pInd+1, equation.length-1, but that
made it so that equations like '9+ans' were cut short,
and regular ('1+55') equations also broke.*/
result = parseFloat(firstVal)+parseFloat(secondVal)
props.setInput(result)
props.setPrevResult(result)
setResultGot(true)
}
The solution #james gave solved the issue, the position of the + sign was obviously going to change after I replaced ans, so that line just had to be moved to after the replacement, silly mistake, I appreciate the help.
I'm really new to Javascript, kinda just learned a little earlier today and been messing around with it, but I'm running into a few issues her and there. I'd appreciate help from some people that know their way around the code.
What's the best way to search a string for multiple words? I'm not completely sure how to explain what I mean, so I'll include my current test code and try to explain. I'm making an attached script to pull text from a text based game online, converting it to lowercase, and defining variables for the use of a money system that changes the input text. Once changes are made, I'm re-inputting the modified text into the game as a return.
let money = 0;
const modifier = (text) => {
let modifiedText = text;
const lowered = text.toLowerCase();
let moneyChange = 0;
// The text passed in is either the user's input or players output to modify.
if(lowered.includes('take their money') || lowered.includes('take ' + 'money')) {
moneyChange = (Math.floor(Math.random() * 500));
if ((moneyChange) > 1) {
console.log(moneyChange);
money += moneyChange;
modifiedText = `You find ${moneyChange} Credits. You now have ${money} Credits`;
} else {
modifiedText = 'You find nothing.';
console.log(modifiedText);
}
}
console.log(modifiedText);
// You must return an object with the text property defined.
return {text: modifiedText};
}
modifier(text);
Currently, as you can see, I have to specifically type "Take their money" or "Take money" as an action before the text pulled is recognized as me taking money from someone or taking some in general. My main issue is that with how the game works, it's somewhat impossible to guess exactly how the input or output is going to come out. The way it works is that the game takes your character's action or speech that you type out, processes it via AI into it's own action or dialogue and generates procedural story to make more sense with the setting so that the player only has to type a vague idea of what's going to happen.
Here's an example:
There's a dead man on the street in front of you.
>loot him
You loot the man, digging through his pockets. You take some money from his wallet, but find nothing else.
The > is my only input and the rest is completely AI generated. My script looks through the AI result and , so I could look for every possible result, from "take his money" to "take her money" and so forth, but that's a little too much to bother with if there's an easier way. If I could have it search the result for specific words that may not be in the normal order and/or with other words in between. Like, it must contain the words "take" and "money" so that if the game says "You find some money, along with a gun. You take both", it recognizes that I'm taking the money. As well as the fact that I still need to write code for every single other time I do anything with money, such as buying things, and if I have to write every possible thing it's going to be a pain.
I know that it would be easier if this code was integrated into the game, but due to AI limitations, that kinda breaks how it works and it goes a little crazy... Any sort of help you can give me will be a help.
If you're looking for a way to search a string which includes multiple sub-phrases, you can use string.includes() in a loop like shown below:
function containsWords(string, words) {
for (let i=0, len=string.length; i<len; i++) {
if (!string.includes(words[i])) {
return false;
}
}
return true;
}
However you also mentioned
search the result for specific words that may not be in the normal order and/or with other words in between
Which immediately brings to mind regex, a text and string matching technology. You can easily find tutorials for regex online, and this live tester is nice too.
I'll quickly build a search string to match "take *** money", where any word can be *** as a quick introduction and example to regex:
/take .+ money/g
Here it matches the specific string take , then .+ matches one or more characters (the middle pronoun eg him/her), then matches money.
I'm trying to insert a number in a prompt and scroll the screen using a for loop with if and else statements, and not success. The code behaves in way that when the loop iterates, the alert seems to pop in the process, and not as a result in the end "a pop up" saying there is no such number in the database to reach for, or the final result if there is the right number according to what was in the range(of numbers possible into entry) and without the pop up iterating, neither a pop up popping at least once when not wanted. The way I want the code to behave is, if there is no matching number then the alert should pop up. I would accept even a array in exchange of a better answer to the code I mentioned bellow, so that I am not restricted to a few tools(sort of say) and everything is welcome for the learning purpose. Though if there is a better solution but can be done with/without the tools I mentioned, I would pick the few tools(codes) I mentioned and it is what the preferable answer and just in case there is solution with it(the few codes I mentioned) or in case if not, I'm open to more tools available.
This is not even my best shot:
function off() {
var ik = prompt("type a number from 1 to 1000");
for(var i=0;i<1000;i++){
if(ik == i){
window.scrollTo(1366*i, 0);
break;
} else {
alert("");
}
}
}
Here is what my friend noticed while working on a project. He does not have any stackoverflow account so I am asking on behalf of my friend.
var a = 1.75/3;
so it gives
a = 0.5833333333333334
Now when I add 1 to variable a I get this:
1.5833333333333335
Notice the difference in the last digits
Similarly when I do following
0.5833333333333336+1
I get
1.5833333333333335
Now replacing the 6 with 7
it gives me
1.5833333333333337
I can't understand what is going here. Can anyone please explain this?
This is by far the strangest error I've ever seen.
In my program I have one variable called avgVolMix. It's a decimal variable, and is not NaN (console.log(avgVolMix) prints something like 0.3526246 to console). However, using the variable at all in an assignment statement causes it to corrupt whatever is trying to use it to NaN. Example:
console.log(avgVolMix); <- prints a working decimal
var moveRatio = 10 + avgVolMix * 10;
console.log(moveRatio); <- prints NaN
I seriously have no idea why this is happening. I've tried everything to fix it; I've converted it to a string and then back, rounded it to 2 decimal places, adding 0.0001 to it - nothing works! This is the only way I can get it "working" right now:
var temp = 0.0;
for(i = 0; i <= avgVolMix; i+=0.1)
temp = i;
This assigns a number that is close to avgVolMix to temp. However, as you can see, it's extremely bad programming. I should also note that this isn't just broken with this one variable, every variable that's associated with a library I'm using does this (I'm working on a music visualizer). Does anyone know why this might be happening?
Edit: I'm not actually able to access the code right now to test any of this stuff, and since this is a company project I'm not comfortable opening up a jsfiddle anyway. I was just wondering if anyone's ever experienced something like this. I can tell you that I got the library in question from here: http://gskinner.com/blog/archives/2011/03/music-visualizer-in-html5-js-with-source-code.html
If its showing the variable value as NaN. Then try converting the variable as parseInt(); method. Hope it works. Because I also faced such problem and solved when tried it.