Calculating A Sample Test Score in JavaScript [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 5 years ago.
Improve this question
I am asked the following questions and came up with a solution but I can't seem to figure out how the other conditions are met or satisfied.
function scoreTest(correct, questions) {
var percent;
// Only change code below this line
percent = (correct*questions); // My logic
// Only change code above this line
return percent;
}
// Change the inputs below to test your code
scoreTest(18,20);
scoreTest(20,25) should return a number //Output met
scoreTest(47,50) should return a value of 94
scoreTest(16,20) should return a value of 80
scoreTest(8,10) should return a value of 80 //Output met
scoreTest(97,100) should return a value of 97
scoreTest(1,50) should return a value of 2

You are so close. Percentages are calculated like this (numberCorrect / TotalQuestions) * 100. This will give you a percentage value.
Given this, re-factor your function to output a percentage:
function scoreTest(correct, questions) {
var percent;
// Only change code below this line
percent = (correct/questions) * 100; // Actual percentage value
// Only change code above this line
return percent;
}
// Change the inputs below to test your code
scoreTest(18,20);
scoreTest(20,25) //should return a number //Output met
scoreTest(47,50) //should return a value of 94
scoreTest(16,20) //should return a value of 80
scoreTest(8,10) //should return a value of 80 //Output met
scoreTest(97,100) //should return a value of 97
scoreTest(1,50) //should return a value of 2

Related

Python list how to identify the given number is in the range Challenge Question [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 1 year ago.
Improve this question
In python am having the list like
mylist = [100 30 400 340, 230, 160, 25]
Given number is : 239
My aim is i have to identify this 239(given number) is in the range of or inbetween of the list each lement with +10 and - 10
Example i have to take the first value from list to verify that 243 is in between 100+10 and 100-10 Not that result false
same way have to compare to 30+10 , 30-10 then again 400+10 and 400-10 same like all
if any one of that inbetween the range i have to return as true if not match with any one then return false
How to do that
Here 239 in between 230+10 and 230-10 so no the result i will get as true
How to do this ??
Thank you in avance
You may iterate on the element of your list, and for each test the expression value-10 < x and x < value+10 that can be written in once in python value-10 < x < value+10
def search_numbers(values, x):
for value in values:
if value - 10 < x < value + 10:
print(f"Ok: {value}-10 < {x} < {value}+10")
return True
return False
mylist = [100, 30, 400, 340, 230, 160, 25]
print(search_numbers(mylist, 239)) # True
print(search_numbers(mylist, 243)) # False

Get last "ordered" number [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 3 years ago.
Improve this question
I've been struggling with the definition of a function. It must take a positive number N as a parameter and return the last ordered number. By "ordered" number I mean that every digit follow each other.
Example1 : Takes 1000 as a parameter and returns 789.
Example2 : Takes 500 as a parameter and returns 456.
Here's working code. Although I don't like just giving you the answer. This is something you have to practice and learn how to do on your own. Take the time to understand what I did. Also, this solution can be improved a lot, just something that I did quickly and works.
The algorithm in action:
function calc() {
//Get the number in the text input
var nbr = parseInt(document.getElementById("number").value, 10);
//Loop starting from the top
for (var i = nbr; i > 0; i--) {
if (isOrderedNumber(i)) {
document.getElementById("result").innerText = i;
return;
}
}
document.getElementById("result").innerText = "None found";
}
function isOrderedNumber(number) {
var digits = number.toString().split('');
//Loops in the digits of the number
for (var i = 0; i < digits.length - 1; i++) {
//Check if the current number+1 is not equal to the next number.
if (parseInt(digits[i]) + 1 !== parseInt(digits[i + 1])) {
return false;
}
}
return true;
}
<input id="number" type="text" />
<button onclick="calc()">Find last ordered number</button>
<br/>
<br/>
<span id="result"></span>
In you case, instead of using html element you would receive "nbr" by parameter instead and would return the value instead of putting the value in the html element. Ask if you have any questions on how this works.

Masking number at a particular position [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 5 years ago.
Improve this question
I have a input field with a maximum length of 15 character.
i want to do something like this
original number :784198012345671
should be: 784********5671
The process to achieving what you want varies depending on whether:
you want to mask the value after the it has been entered or
you want to mask the value during typing.
If you want to do this, after the value has been entered the following should do the trick.
Snippet:
var
/* The original value. */
n = "784198012345671",
/* The masked value. */
masked = n.slice(0, 3) + Array(8).join("*") + n.slice(-4);
/* Log the result. */
console.log(masked);
If, instead, you want to mask the input during typing, things get more complicated.
Snippet:
$(".newsocial").on("keyup", function(e) {
/* Turn the value into an array of characters. */
var value = this.value.split("");
/* Iterate over every character. */
value.forEach(function (char, index) {
/* Replace the character with a placeholder when appropriate. */
if (index >= 3 && index <= 10) value[index] = "*";
});
/* Turn the array of chars into a string & assign it to the value of the input. */
this.value = value.join("");
})
<!--- HTML --->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" maxlength="15" id="input-card" class="newsocial">
const original = "784198012345671",
obfuscated = original.substr(0, 3) + "*".repeat(original.length - 7) + original.substr(-4);
You may use substr to get the number groups at the beginning and the end out of the number and then use repeat to fill the asterisks inbetween.
Convert your number to string and use string.prototype.substring and string.prototype.repeate to build parts:
var number = 784198012345671;
number = number.toString();
var res = number.substring(0, 3) + '*'.repeat(8) + number.substring(11, 15);
console.log(res);
Replace Integer to String
Iterate the string and replace the position with *:
let number = 123455666777888;
let stringedNum = number.toString();
for(i=0;i<stringedNum.length;i++) {
if(i>5 && i<10) { // change this line as per your need
console.log(i)
stringedNum = stringedNum.replace(stringedNum[i],'*');
}
}
console.log(stringedNum)

javascript simple bmi calculator doesnt work as intended [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
var userweight = prompt ("What is your weight?");
var userheight = prompt ("What is your height in meter?");
var bmi = function (userweight,userheight){
return userweight/(userheight*userheight);
};
var bmi= function(calc){
if(calc<=18.4){
return("you are thin");
}
else if (18.5<=calc<=24.9){
return("you are normal");
}
else if (25.0<=calc<=29.9){
return("you are fat");
}
else if (calc>=30.0){
return("you have obesity");
}
};
bmi(userweight,userheight);
I tried to make a bmi calculator. It calculates your bmi . But it had problems in if/else part. It only shows "You are normal" no matter what bmi it is
At the first, you override the first bmi-formula function assignment with your if/else statements.
You should rename one of these functions:
var userweight = prompt("What is your weight?");
var userheight = prompt("What is your height in meter?");
var formula = function(userweight,userheight){
return userweight / (userheight * userheight);
};
var bmi = function(userweight, userheight){
var calc = formula(userweight, userheight);
if (calc <= 18.4) {
return "you are thin";
}
if (calc >= 18.5 && calc <= 24.9) {
return "you are normal";
}
if (calc >= 25.0 && calc <= 29.9) {
return "you are fat";
}
if (calc >= 30.0) {
return "you have obesity";
}
};
alert( bmi(userweight, userheight) );
The second thing is, that your condition to return the "normal"-part (18.5<=calc<=24.9) is falsey, you could do it like this: calc >= 18.5 && calc <= 24.9. (The same for the "fat"-part)
The last note, as you can see, I've removed the else-keywords since we return a value after if, it doesn't matter what then comes.
You declare the bmi function expression
var bmi = function (userweight,userheight){
return userweight/(userheight*userheight);
};
but the subsequent declaration later on overwrites it completely. So when you call
bmi(userweight, userheight)
Javascript takes the passed in userweight and assigns it to calc. The BMI isn't calculated at all, your first function is never called.
userheight is never used; JavaScript doesn't care if you pass in too many arguments.
Give your functions different names e.g. renaming your first function to calculateBMI and the second one to adviseOnBMI (or whatever you fancy) and rerun it.
console.log is your friend when debugging - use it to output to the Developer Console in your browser of choice.

Variable.match for amount > 999.99 [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 8 years ago.
Improve this question
For example :
Var1 = 289.56
I use this formula :
foundStr = Var1.match("[0-9]+\.[0-9]+");
Price( parseFloat(foundStr).toFixed(2) );
But when Var1 > 999.99 (Example : 2,356.21)
What is the script find the string ?
foundStr = Var1.match(??);
Thank you
You already have a numeric variable, why are you messing with strings?
var number1 = 289.56;
if (number1 > 999.99) {
// do whatever
}
If you're trying to round, use Math.floor instead:
var number1 = 289.56485345734593453;
var roundedNumber1 = Math.floor(number1 * 10) / 10; // two decimal points
I think you just want to remove the commas and check if its a float, but its hard to tell based off your question. How about something like this:
var Var1 = "1,234.567";
var parsed = parseFloat(Var1.replace(",",""), 10);
if (isNaN(parsed)) {
// its not a valid number, so deal with it as needed
}
else {
// parsed now holds your Number, so use it
}
This approach will work regardless of if the number is >= 1000.
var Var1 = "2,356.21";
foundStr = String(Var1.match(/([0-9]{1,3},)?[0-9]{0,3}\.[0-9]+/g)).replace(/,/g, "");
var result = parseFloat(foundStr).toFixed(2);

Categories

Resources