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 6 days ago.
This post was edited and submitted for review 6 days ago.
Improve this question
I'm trying to solve this challenge about grading on hackerrank:
[https://www.hackerrank.com/challenges/grading/problem]
The challenge:
HackerLand University has the following grading policy:
Every student receives a grade in the inclusive range from 0 to 100.
Any grade less than 40 is a failing grade.
Sam is a professor at the university and likes to round each student’s according to these rules:
If the difference between the grade and the next multiple of 5 is
less than 3, round grade up to the next multiple of 5.
If the value of grade is less than 38, no rounding occurs as the
result will still be a failing grade.
Examples and goal:
round to (85–84 is less than 3),
do not round (result is less than 40),
do not round (60–57 is 3 or higher).
Given the initial value of grade for each of Sam’s n students, write code to automate the rounding process.
The function gradingStudents has the following parameter(s):
int grades[n]: the grades before rounding (eg.: [1,24,87,99,54])
And should return:
int[n]: the grades after rounding as appropriate (eg.: [1,24,87,100,55])
Here's what I did so far:
function gradingStudents(grades) {
let b = []
for (let i = 0; i < grades.length; i++) {
for (i of grades) {
if (i < 38) {
b.push(i)
} else {
if ((parseInt(i) % 5) < 3) {
b.push(parseInt(i))
} else {
let c = parseInt(i) + (5 - (parseInt(i) % 5))
b.push(c)
}
}
}
}
return b
}
MY PROBLEM/ QUESTION
My code passed 7 times out of 12 cases it was tested for.
Can someone spot what I'm doing wrong?
Related
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 6 years ago.
Improve this question
For each number between 1 and 100...
If the number is odd, print it out.
But if the number is even, print the word "Steven" instead.
When we run the program, we should see the following output:
1
Steven
3
Steven
5
Steven
...
97
Steven
99
Steven
The program needs to start at 1 (not 0) and the number 100 must be included - the last number should be 100, not 99.
I'm still learning Javascript, so my first instinct is to create an While Statement:
var Number
var Counter = 1;
While (Number == even
Number <= 100) {
print ("Steven");
} else {
print (Number);
Should this even be a while loop though? How can I fix this statement so that it prints out odd numbers and "Steven" in-place of even numbers?
Edit: Tried to run in JSFiddle but nothing happened.
In JavaScript -
Use a for loop and loop through 1 - 100. Check the remainder when dividing by two. If the remainder is 0, we know it's even, so console.log "Steven", otherwise the current number.
for (i = 1; i <= 100; i++){
if (i % 2 == 0) console.log("Steven")
else console.log(i)
}
Using the modulus operator should do the job
Using PHP:
for ($i=1; $i < 100; $i++):
if ($i % 2 == 0) {
echo "Stephen";
}
else {
echo $i;
}
endfor;
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 7 years ago.
Improve this question
Practice Problem 1
Parameters: The function accepts two positive integers N and M.
Return Value: The function returns the product of N and M. For example, if the integers 5 and 8 are supplied to
that function, it should return the product of 5 and 8—it should return 40.
Additional
Requirements:
Do this without using the multiplication operator (*). Hint: Multiplication is just a series of addition
operations.
function mult(N, M) {
return N / (1 / M);
}
Since this is a basic excercise, I think that this answer is not expected (but maybe you'll get bonus points if you can explain it), even though it does the math without *.
function mult(N,M){
var a = new Array(N);
return a.join(""+M).split("").reduce((x,y)=>(parseInt(x)+parseInt(y)))+M
}
Note: This does not work for N < 3. No time to correct it.
OK. Look. It sounds like you're quite young so I think giving you the benefit of the doubt is alright here. So you know for the future: Stackoverflow isn't a site that you can just drop a homework question and expect people to do the work for you.
We sometimes do help with homework questions but only if it looks like you've at least attempted to answer the question yourself, by showing us some code that you've written. If you want to use SO in the future you might find the help section useful, particularly the section on how to write a good question.
OK, lecture over.
What the question is asking about is how to use a simple for loop to add some numbers together:
function getProduct(num1, num2) {
// set total to zero
// we'll be adding to this number in the loop
var total = 0;
// i is the index, l is the number of times we
// iterate over the loop, in this case 8 (num2)
for (var i = 0, l = num2; i < l; i++) {
// for each loop iteration, add 5 to the total
total += num1;
}
// finally return the total
return total;
}
getProduct(5, 8); // 40
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 7 years ago.
Improve this question
How to use a while loop to generate random numbers until a certain number is reached? Help please
This code will generate integer numbers from 1 until 50.
var number = 50;
var x = 0;
while(x !== number){
x = Math.floor((Math.random() * 50) + 1);
console.log(x);
}
Considering there is a random number generator random you will just do as shown below
do
{
x=Math.random();
x=x*range;
if(x is desired number )
break;
else
{
print the number.
}
}while(1)
print -( desired number found).
NOTE: As you are beginner you can try the following when designing these codes..
First find what is the input and what is the output.
Then try to draw a flow chart of the whole thing ( You can try running it yourself as if you are the computer)
Then all the branch and loop are converted to if-else or for-while loop.
Check the code and match input and output.
This is a basic program structure..Instead of asking in forums about common structures try reading some books.
Hint 1: There is a function in javascript called Math.random() return a random number between 0 (inclusive) and 1 (exclusive):
Hint 2: check whether the values are equal. Use === in javascript.
Hint 3: check for if else, break and do while, while loop.
Hint 4: If you are given a random number 0<=x<1 then how do you generate a random number using in range [a,b) using the abobe geerated number? Ans: Use b*x.
hope this helps.
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
I created a simple fuzzbuzz in javascript (see below). I however would like to include the following:
if the number starts with a 1 (so fe 11) "ping" should be added. So 15 should be FizzBuzzPing etc...
Any thoughts?
function fizzBuzz() {
for (var i=1; i <= 20; i++)
{
if (i % 15 == 0)
alert("FizzBuzz");
else if (i % 3 == 0)
alert("Fizz");
else if (i % 5 == 0)
alert("Buzz");
else
alert(i);
}
}
$(document).ready(function(){
$('#clickMe').click(function(){
fizzBuzz();
});
});
Convert the number to a string and take the first index of the string:
var digit = (''+i)[0];
Or, the alternative
var digit = i.toString()[0];
Then check if digit is equal to 1 or not and add things or not accordingly.
For a future reference: Spend some time searching for a solution to your problems, don't ask questions unless you've spent some time making sure an answer does not exist to your question. A similar question has been answered many times before. Maybe it's not about fuzzbuzz but you should be able to find two different answers to two different questions and be able to combine then into your solution.
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
if(MathRScore > 29.9 && MathRScore < 34.9 ) MathScore = 10*Math.round((MathRScore*45/5+(535*35-580*30)/5)/10);
I've inherited this tiny line of exception code for a scoring algorithm I'm maintaining. I'm not sure what it does exactly. Can someone walk me through it?
If your MathRScore is in the interval (29.9, 34.9) then set the MathScore to (in simplified form)
MathScore = 10*Math.round((MathRScore*9+265)/10)
By dividing by 10, then using Math.round and then multiplying by 10 you will round to the nearest ten.
For example
10*Math.round(1111/10) = 10*Math.round(111.1) = 10*111 = 1110
if the value of MathRScore is between 30 and 34.8 , it will make the variable MathScore hold the value of 10 times the rounded value of all that stuff in the parenthesis.
If MathRScore is between 29.9 and 34.9, exclusive, then set MathScore to the value:
(MathRScore * 9) + 265
rounded to the nearest 10.
Beyond that, what this means can only be revealed by understanding how these scores work, and we can't tell you that.