I need to solve the following problem:
Create class Intern with next fields:
Name(String)
Surname(String)
laziness(number from 20 to 50)
Curiosity (number from 0 to 100)
Skill (number from 0 to 100)
Irresponsibility (float number from 0.0 to 1.0)
For this class create method that calculate “mark” for intern, that is calculated by formula(in the code)
It looks simple, but when I run it, the output is NaN. Could somebody help please?
class Intern {
constructor(name, surname, laziness, curiosity, skill, irresponsibility) {
this.name = name
this.surname = surname
if (laziness <= 50 && laziness >= 20) {
this.laziness = laziness
} else {
this.laziness = 0
}
if (curiosity <= 100 && curiosity >= 0) {
this.curiosity = curiosity
}
if (skill <= 100 && skill >= 0) {
this.skill = skill
}
if (irresponsibility <= 0 && irresponsibility >= 1) {
this.irresponsibility = irresponsibility
}
}
getMark() {
let mark = (((this.skill + this.curiosity) * (1.5 - this.irresponsability)) / (this.laziness * 0.25));
return mark
}
}
You've misspelled the irresponsibility variable in the getMark() method, and the if statement in your constructor for it will never be true:
if (irresponsibility <=0 && irresponsibility >=1)
I think you were meaning to say:
if (irresponsibility >=0 && irresponsibility >=1)
Related
I have multiple errors in my code. When I run the code, it gives an error saying that there is an unexpected identifier. Here is the full error, "SyntaxError: Unexpected identifier at /script.js:46:16". However, when I check through lines 46 through 16, I cant find any unclosed functions or methods. When I comment the if statement on line 46, it just gives an error on the other if statement. Can someone help me?
Heres the code:
function print(str){
console.log(str)
}
function farhToKelvin(far){
const cel = Math.floor(far / (9/5) - 32)
const kel = cel + 273
return kel
}
function farhToCelsius(far){
const cel = Math.floor(far / (9/5) - 32)
return cel
}
function convertToFarh(type, val){
type.toLowerCase()
val.toLowerCase()
if (type == "kelvin"){
return Math.floor(far / (9/5) - 32 - 273)
}
else if(type == "celsius"){
return Math.floor(far / (9/5) - 32)
}
}
while (farh != "exit"){
var farh = prompt("enter a farhanhite tempature: ")
var type = prompt("convert it to celsius, or kelvin")
type.toLowerCase()
if (type == "celsius"){
const c = farhToCelsius(farh)
var val = convertToFarh(c)
if (val > 50 or val == 50){
print("it is cold, you should wear a coat")
}
if (val > 50 or val == 50){
print("it is hot, you should wear a tank top")
}
}
else if(type == "kelvin"){
const k = farhToKelvin(farh)
var val = convertToFarh(k)
if (val > 50 or val == 50){
print("it is cold, you should wear a coat")
}
if (val > 50 or val == 50){
print("it is hot, you should wear a tank top")
}
}
}
if (val > 50 or val == 50){
In Javascript, instead of or, we use ||. If you have a similar problem again, you might want to take a look at What does this symbol mean in JavaScript?
change
if (val > 50 or val == 50)
to this
if (val > 50 || val == 50)
or better to this
if (val >= 50)
there are similar problems on line 50 and 60 and 64
you need to update them all.
logically line 46 and 50 are the same. based on you print message the line 50 should be
if (val > 50 ) but line 45 should be if ( val <= 50)
so you have both syntax and semantic problems in your code to address
Here's a complete list of the issues you have in your code:
Using or instead of || (as others have stated)
Using incorrect variable name in the convertToFarh function
Omitting type parameter when calling convertToFarh
Attempting to call String.toLowerCase() on a number variable
Misspelling fahrenheit
Using or instead of || (Logical OR)
In your code, I believe you are intending to indicate if (val > 50 || val == 50) { instead of using or as used in other programming languages.
Before:
if (val > 50 or val == 50){
print("it is cold, you should wear a coat")
}
if (val > 50 or val == 50){
print("it is hot, you should wear a tank top")
}
After:
if (val > 50 || val == 50){
print("it is cold, you should wear a coat")
}
if (val > 50 || val == 50){
print("it is hot, you should wear a tank top")
}
This logic also doesn't make sense. Perhaps you mean value < 50 in the first one and val >= 50 in the second?
You also repeat yourself whether you're converting to/from Kelvin or Celsius, so that code could be extracted out into its own function or just reduce the if..else blocks down to only affect the necessary variables and perform the comparison after these blocks.
Using incorrect variable name in convertToFarh function
In the convertToFarh function, you reference a variable named far, but there's no variable by that name. So you either mean to reference the val argument or you are trying to reference the fahr variable declared outside the function. In my code, I assume the former is the case val and rename it as follows:
function convertToFarh(type, val){
type.toLowerCase()
val.toLowerCase()
if (type == "kelvin"){
return Math.floor(val / (9/5) - 32 - 273)
}
else if(type == "celsius"){
return Math.floor(val / (9/5) - 32)
}
}
Omitting type parameter when calling convertToFarh
In both function calls to convertToFarh, you use the c or k variable as the value of the val parameter, but you don't indicate the type. I have fixed this to indicate the type for each part:
var val = convertToFarh("celsius", c);
var val = convertToFarh("kelvin", k);
Attempting to call String.toLowerCase() on a number variable
In the convertToFarh function, you are attempting to call the String.toLowerCase() method on a number type (val) which gives an error. In my code, I simply commented this out and confirmed it can safely be removed.
Misspelling fahrenheit
It might not seem like a big deal, but making sure variables have proper spelling helps when others are reviewing your code (whether bug-fixing or general code review). I have fixed function names, variable names, and any references to fahrenheit in your code to be the proper spelling. This includes:
"enter a fahrenheit temperature: "
function fahrToKelvin and function calls
function fahrToCelsius and function calls
function convertToFahr and function calls
The variable named farh to fahr
Function parameters named far were changed to val to avoid variable name collision
Full code
function print(str) {
console.log(str);
}
function fahrToKelvin(val) {
const cel = (val - 32) / (9 / 5);
return Math.floor(cel + 273.15);
}
function fahrToCelsius(val) {
const cel = Math.floor((val - 32) * 5 / 9);
return cel;
}
function convertToFahr(type, val) {
if (type == "kelvin") {
return Math.floor(val / (5 / 9) - 459.67);
} else if (type == "celsius") {
return Math.floor(val / (9 / 5) + 32);
}
}
var fahr = prompt("enter a fahrenheit tempature: ");
var type = prompt("convert it to celsius, or kelvin");
type = type.toLowerCase();
if (type == "celsius") {
const c = fahrToCelsius(fahr);
var val = convertToFahr("celsius", c);
if (val < 50) {
print("it is cold, you should wear a coat");
}
if (val >= 50) {
print("it is hot, you should wear a tank top");
}
} else if (type == "kelvin") {
const k = fahrToKelvin(fahr);
var val = convertToFahr("kelvin", k);
if (val < 50) {
print("it is cold, you should wear a coat");
}
if (val >= 50) {
print("it is hot, you should wear a tank top");
}
}
I don't know how to word this but this is what I'm trying to do:
if (score >= 0 && score <= 10) overallScore = 0;
else if (score >= 11 && score <= 20) overallScore = 1;
else if (score >= 21 && score <= 30) overallScore = 2;
else if (score >= 31 && score <= 40) overallScore = 3;
else if (score >= 91 && score <= 100) overallScore = 9;
...
Is there any way to recursively do this using a function?
overallScore = Math.max(0, Math.floor((score - 1) / 10));
no need for recursion. But if you need that:
const getOverall = score => score <= 10 ? 0 : getOverall(score - 10) + 1;
Recursion is not really appropriate here, since you can get the required value in constant time. Recursion becomes interesting when you need at least O(logn) time.
But as you ask for it, here is one way to make it recursive:
function range(score, depth = 0) {
return score <= 10 || depth >= 9 ? 0 : range(score-10, depth+1) + 1;
}
console.log(range(0)); // 0
console.log(range(10)); // 0
console.log(range(11)); // 1
console.log(range(60)); // 5
console.log(range(91)); // 9
console.log(range(110)); // 9
I am not sure what i am doing wrong, it all works but something is off and i cant catch it since i am a newbie, any ideas?
function finalGrade(exam, projects) {
if (exam >= 90 || projects > 10) {
return 100;
} else if (exam >= 75 && projects === 5) {
return 90;
} else if (exam >= 50 && projects === 2) {
return 75;
} else {
return 0;
}
}
Question - This function should take two arguments: exam - grade for exam (from 0 to 100); projects - number of completed projects (from 0 and above);
This function should return a number (final grade). There are four types of final grades:
100, if a grade for the exam is more than 90 or if a number of completed projects more than 10.
90, if a grade for the exam is more than 75 and if a number of completed projects is minimum 5.
75, if a grade for the exam is more than 50 and if a number of completed projects is minimum 2.
0, in other cases
You are using === in last to if statements. You should use >= if you want to check minimum number of projects
function finalGrade(exam, projects) {
if (exam > 90 && projects >= 10) {
return 100;
} else if (exam > 75 && projects >= 5) {
return 90;
} else if (exam > 50 && projects >= 2) {
return 75;
} else {
return 0;
}
}
Just only one part, the structure
if () {
return
} else if () {
return
} else {
return
}
can be simplified to omit else parts and just go on with if
if () {
return
}
if () { // as many more
return
}
return
Inside the all if condition the grade for exam is more than 90, 75, and 50, so it will be just grade>90, grade>75 and grade>50 and not grade>=90, grade>=75 and grade>=50. And another thing is that you should use >= instead of === .
Here's my solution:-
function finalGrade (exam, projects) {
if(exam > 90 || projects > 10)
return 100;
if(exam > 75 & projects >= 5)
return 90;
if(exam > 50 & projects >= 2)
return 75;
return 0;
}
This is one of those times where the solution is staring me right in the face but I can't seem to find it! So please be patient with me. The kata instruction is the following:
Complete the function so that it finds the mean of the three scores passed to it and returns the letter value associated with that grade.
Numerical Score Letter Grade
90 <= score <= 100 'A'
80 <= score < 90 'B'
70 <= score < 80 'C'
60 <= score < 70 'D'
0 <= score < 60 'F'
Tested values are all between 0 and 100. There is no need to check for negative values or values greater than 100.
Here is my solution:
function getGrade (s1, s2, s3) {
var score = (s1 + s2 + s3) / 3;
if (90 <= score && score >= 100) {
return 'A';
} else if (80 <= score && score > 90) {
return 'B';
} else if (70 <= score && score > 80) {
return 'C';
} else if (60 <= score && score > 70) {
return 'D';
} else if (0 <= score && score > 60) {
return 'F';
}
}
getGrade(5,40,93);
getGrade(30,85,96);
getGrade(92,70,40);
Can't for the life of me figure out what I am doing wrong.
Your conditions in if statement are all wrong. These are the right conditions
function getGrade (s1, s2, s3) {
var score = (s1 + s2 + s3) / 3;
if (score >= 90 && score <= 100) {
return 'A';
} else if (score >= 80 && score < 90) {
return 'B';
} else if (score >= 70&& score < 80) {
return 'C';
} else if (score >= 60 && score < 70) {
return 'D';
} else {
return 'F';
}
}
your conditions are wrong and you don't need multiple check in same if .Change your code to this:
function getGrade (s1, s2, s3) {
var score = (s1 + s2 + s3) / 3;
if (score >= 90 && score <= 100) {
return 'A';
} else if (score >= 80 ) {
return 'B';
} else if (score >= 70 ) {
return 'C';
} else if (score >= 60) {
return 'D';
} else{
return 'F';
}
}
console.log(getGrade(5,40,93));
console.log(getGrade(30,85,96));
console.log(getGrade(92,70,40));
You could use only if clauses without else parts and check only the lower bound, because you have already checked the upper bound.
The check for upper 100 is missing, because your given range is between 0 and 100.
function getGrade(s1, s2, s3) {
var score = (s1 + s2 + s3) / 3;
if (score >= 90) {
return 'A';
}
if (score >= 80) {
return 'B';
}
if (score >= 70) {
return 'C';
}
if (score >= 60) {
return 'D';
}
return 'F';
}
console.log(getGrade(5, 40, 93));
console.log(getGrade(30, 85, 96));
console.log(getGrade(92, 70, 40));
Whenever you find yourself writing long chains of if-else statements, see if you can find a pattern and use a lookup table. Here, we have only 5 grade buckets, but what if we had 20 or 100? You can see the if-else approach isn't scalable.
In this case, if we use the string "FFFFFFDCBAA" then we've enumerated all 5 grade buckets in a way that lets us index into after dividing the score by 10. The code for that would be: "FFFFFFDCBAA"[score/10|0] where | 0 is the floor operation, chopping off the decimal. The extra "A" handles the case of 100.
Secondly, the arguments to the function (s1, s2, s3) make no sense. Why 3 scores? If we have 4 score, or 20 scores, the function can't be used and we have to rewrite the whole function with the right number of arguments (and the 20-argument one will be pretty ugly). I realize this header is what the kata author gave you, but there's no reason we can't make it handle any number of arguments using (...args) and still pass the tests. If we take the average of the arguments using args.reduce((a, e) => a + e, 0) / args.length, we're left with the following solution:
const sum = a => a.reduce((a, e) => a + e, 0);
const avg = a => sum(a) / a.length;
const getGrade = (...args) => "FFFFFFDCBAA"[avg(args)/10|0];
[
[0],
[0, 100, 50],
[90, 95, 100],
[80, 60],
[81, 79],
[80, 59],
].forEach(test => console.log(`${test} => ${getGrade(...test)}`));
Is there any way to check if value is in a number range? Like my example:
if (battery.level == 70 to 100) {
$('#battery').css('background-image', 'url("battery_full.png")');
}
What's the syntax for that? Thanks for help.
if (battery.level >= 70 && battery.level <= 100) {
something like this
if ( value >= 70 && value <= 100)
{
}
You could do this :
function inRange(n, from, to) {
return n >= from && n <= to;
}
if (inRange(battery.levelPercent, 70, 100))
YOU CAN CHECK NUMBER LIKE
if ( battery.level >= 70 && battery.level <= 100)
{
}
NOT A STRING LIKE ('70%' to '100%')
70% is actually not a number. However, you could get rid of the invalidating % by naming your variable battery.levelPercent instead and adding the percent sign whenever it needs to be output.
Then, you could check the number like this:
if (typeof battery.levelPercent === "number") {
if (battery.levelPercent >= 70 && battery.levelPercent <= 100) {
$('#battery').css('background-image', 'url("battery_full.png")');
} // else, not in range
} // else, not a number
You better remove % from battery.level to compare it with number.
Live Demo
level = battery.level.replace('%', '');
if (level >= 70 && level <= 100) {
$('#battery').css('background-image', 'url("battery_full.png")');
}
You can also use parseFloat or parseInt
level = parseFloat(battery.level);
if (level >= 70 && level <= 100) {
$('#battery').css('background-image', 'url("battery_full.png")');
}