Number comparison returning random values? [duplicate] - javascript

This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Why is one string greater than the other when comparing strings in JavaScript?
(5 answers)
Closed 1 year ago.
I have this javascript code that is working mostly as intended. It is two functions, the first prompts the user for two values on a button press, and the second compares them and displays an output on another button press.
It works almost perfectly, except the outputs don't make sense. It is supposed to output 'true' if inpOne is greater than inpTwo, and 'false' otherwise.
If both inputted numbers are less than 10 i.e. inpOne = 8, inpTwo = 1, it will output true, as it should. However, if inpOne = 20, and inpTwo = 3, it outputs 'false' which is incorrect. I've tried with many other values and it seems to be random to some extent... Can someone look over my code and tell me what I'm doing wrong? Thanks!
Edit: I realize it is three functions, the last function just displays the results to an HTML element...
let inpOne;
let inpTwo;
function promptForCompare() {
inpOne = prompt('Enter first number','');
inpTwo = prompt('Enter second number','');
}
function checkIfGreater() {
let valComp = inpOne > inpTwo ? true : false;
return displayResult(valComp), console.log(valComp);
}
function displayResult(input) {
if (input == true) {
document.getElementById("resDisp").innerHTML = "Number is greater!";
} else {
document.getElementById("resDisp").innerHTML = "Number is NOT greater!";
}
}

Related

How do I display messages using an array or object

I've created a random generator game and this is a RandomNumber() function that will display the message if the user keys in the correct or wrong number. Does anyone knows how to display the message using an array or object method? For example, "Your guess number 1 is 5 and it is too low", "Your guess number 2 is 23 and it is too high". So the maximum number a user have is 5 tries.
function RandomNumber()
{
if (!this.guesses)
{
this.guesses = 1;
}
else
{
this.guesses++;
if (this.guesses > 5)
{
document.getElementById("correct").innerHTML="No more tries";
document.getElementById("guessing").disabled=true;
return;
}
}
//get value from random number textbox
var lol=document.getElementById("guess").value;
//get new value from user
var ass=document.getElementById("textbox").value;
if(ass == lol)//if user guess correctly
{
document.getElementById("correct").innerHTML="Correct"
}
else if(ass!=lol)//if user guess wrongly
{
document.getElementById("correct").innerHTML="Not correct"
}
}
function getMessage(correctNumber, guess, guessNumber) {
const highOrLow = guess > correctNumber?"too high.":"too low.";
return `Your guess number ${guessNumber} is ${guess} and it is ${highOrLow}`;
}
const message = getMessage(3, 5, 1); // message now equals "Your guess number 1 is 5 and it is too high."
Explanation:
The first line const highOrLow = guess > correctNumber?"too high.":"too low."; creates the part of the string that says "too high" or "too low" using a ternary operator. If guess is too high then highOrLow will equal "too high.". It will equal "too low." otherwise. The second line uses javascripts string templating to create the final string. The ${variable} part of the string are replaced with the value of variable.
Keep in mind this function doesn't account for when "guess" and "correctNumber" are equal.
This doesn't use an array or object but there is no reason to use an array or object. If you really want to put it in an object then do:
var o = {message: getMessage(3, 5, 1)}
but I don't see why you would need to do that.

How can I get my code to check if I entered in an integer? [duplicate]

This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Validate that a string is a positive integer
(16 answers)
Closed 2 years ago.
My code isn't working on my JavaScript project. I want it to check if you inputted an integer or not, and if you did, check if you inputted a whole number. I expected it to accept the number "3" in my prompt and say it was an integer. Instead, everything I put in "isn't an integer." Can someone help? Here's a copy of my code:
function changeTime() {
var changeTimee = prompt("Enter in a new amount of seconds. Leave blank or press cancel to cancel.");
console.log("You entered: " + changeTimee);
if (Number.isInteger(changeTimee)) {
if (floor(changeTimee) === changeTimee) {
if (changeTimee === "" || changeTimee === null) {
console.log("Left Blank");
} else {
seconds = changeTimee;
}
}
} else {
alert("Please enter positive integer, not a string.");
}
console.log(seconds);
}

I'm trying to write a function that gets a string from a field in html and return it if the first and the last characters are equal [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
How to get first character of string?
(22 answers)
How can I get last characters of a string
(25 answers)
Closed 3 years ago.
I'm ought to do it without loops. with "if" and "else".
The user writes a random word in the text field at the html and the function needs to check if the first and last letters are equals.
If they are equal - the function will return the string without the first and the last characters.
if the characters are not equal - the function will return the written string by the user.
unction firstLastletter(){
let k = document.getElementsByClassName('text').value;
let other = 'changed the string';
if(k.slice(0) == k.slice(-1)){
return console.log(k);
}
else {
return console.log(other);
}
}
i know that i miss a lot.
but i have no clue how to implement the code in a right way.
many thanks..
Something like this?
function firstlastletter(s) {
var first = s.charAt(0);
var last = s.charAt(s.length-1);
if(first == last) return s.substring(1, s.length-1)
else return s;
}
https://jsfiddle.net/mte97hg1/19/

Regex only contain binary number and not more than 8 digit [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I expect true return value that match condition below :
Only binary number (0 or 1)
Length not more than 8
I'm new to regex, i have googling and read JS RegExp from https://www.w3schools.com/Js/js_regexp.asp but still i don't get it.
I've tried
/[0-1]$/
but still didn't match the condition above
I expect boolean return from regex test if there is contain no other number except 0 or 1 and length not more than 8.
data: {
binRegExp: /[0-1]$/,
isBinary: false,
binNum: '', // get value from user input
},
computed: {
inputCheck(){
return this.isBinary = this.binRegExp.test(this.binNum)
}
}
code above is vue js
If there is solution, please answer below. Thank you
this will takes 1 to 8 [1-0]
const regex = /^[0-1]{1,8}$/
const text = "1010010"
console.log(regex.test(text));
Try
/^[01]{1,8}$/
let strings = ['11110001','1111000111','11112001'];
strings.forEach(x=> /^[01]{1,8}$/.test(x) ? console.log(x,'pass') : console.log(x,'not pass'))

JavaScript check if number is whole [duplicate]

This question already has answers here:
How do I check that a number is float or integer?
(52 answers)
Closed 7 years ago.
im trying to check if a number is a whole after a calculation. What I have so far prints out how many times one number gets divided by another, but when the number is not whole it dose not print anything out. Heres my code:
function round() {
var percent = document.getElementById('percent_sale').value;
var perShare = document.getElementById('singleShare').value;
var result = (percent / perShare);
if(result % 1 == 0) {
document.getElementById('results1').innerHTML = ('Number of shares:'+result);
} else {
document.getElementById(results1).innerHTML = ('number of shares must ');
}
}
The values get input buy a user, and the percent for sale is say 50 and the single share is say 2.5 this would return 20 shares.
What I need is if I put in something like 50 for sale and 3.15 single share it tells the user to make equal number of shares as it would return 15.87
Any ideas where ive gone wrong?
Convert your number into string and then check if the string contains only numbers
var num = 15;
var n = num.toString();
This will convert it into string then this
String.prototype.isNumber = function(){return /^\d+$/.test(this);}
console.log("123123".isNumber()); // outputs true
console.log("+12".isNumber()); // outputs false
For further reference.Link StackOverFlow

Categories