Wierd Error while working with unlimited argument function Node JS - javascript

//##// Function Zone //##//
function out(type, ...strings) {
var str = strings.join(' ');
switch (type) {
case "log":
console.log("\x1b[37m", '[LOG]\t', str, "\x1b[0m");
break;
case "error":
console.log("\x1b[31m",'[ERROR]\t', str, "\x1b[0m");
break;
case "info":
console.log("\x1b[36m", '[INFO]\t', str, "\x1b[0m");
break;
default:
throw new Error('Bad output type');
}
}
When I start the program it crashes with this error:
FATAL ERROR: Error::New napi_get_last_error_info<br/>
1: 00007FF6F206CF2F napi_wrap+112799<br/>
2: 00007FF6F200CF26 v8::base::CPU::has_sse+55702<br/>
3: 00007FF6F200DDB3 v8::base::CPU::has_sse+59427<br/>
4: 00007FF6F200D509 v8::base::CPU::has_sse+57209<br/>
5: 00007FF6F2032410 napi_fatal_error+160<br/>
6: 00007FFC74541DA3 <br/>
7: 00007FFC74541CF7 <br/>
8: 00007FFC7454CB69 <br/>
9: 00007FF6F203032C node_module_register+1548<br/>
10: 00007FF6F20BD340 uv_timer_stop+560<br/>
11: 00007FF6F20BD417 uv_timer_stop+775<br/>
12: 00007FF6F20B9ECB uv_async_send+331<br/>
13: 00007FF6F20B966C uv_loop_init+1212<br/>
14: 00007FF6F20B9834 uv_run+244<br/>
15: 00007FF6F1FC9681 v8::internal::interpreter::BytecodeArrayWriter::source_position_table_builder+31713<br/>
16: 00007FF6F2036223 node::Start+275<br/>
17: 00007FF6F1EB6A9C RC4_options+340380<br/>
18: 00007FF6F2D2F3F8 v8::internal::SetupIsolateDelegate::SetupHeap+1300536<br/>
19: 00007FFCB7CA7034 BaseThreadInitThunk+20<br/>
20: 00007FFCB7DDCEC1 RtlUserThreadStart+33<br/>
Any Idea?
Thank you for helping me!

I finally found the problem, I forgot to modify a function in my code, the error is because of that. I called the method info() that is a standard node.js method an that caused the error.
Thank you anyway :D

Related

split function is duplicating values

I am trying to split a string with split() and for some reason, it is giving me duplicate values this is driving me mad can someone please assist me
The string is a comma separated response from a database here is the echo
echo $Title1 . ',' . $link. ','.$value. ','.'number'.','.'$' .',';
Here is my code:
function displayMatches(response2){
var str = response2;
var str_array = response2.split(',');
}
This is the console log from str_array
0: "Butter cauliflower & paneer"
1: "https://link1.com"
2: "4"
3: "friends5"
4: "$"
5: "Butter cauliflower & paneer"
6: "https://link1.com"
7: "4"
8: "friends5"
9: "$"
10: "Butter cauliflower and coconut sambal"
11: "https://link2.com"
12: "3"
13: "friends5"
14: "$"
15: "Butter cauliflower and coconut sambal"
16: "https://link2.com"
17: "3"
18: "friends5"
19: "$"
Here is the value of response2
Butter cauliflower & paneer,https://link1.com,4,friends5,$,Butter cauliflower & paneer,https://link2.com,4,friends5,$,Butter cauliflower and coconut sambal,https://link3.com,3,friends5,$,Butter cauliflower and coconut sambal,https://link4.com,3,friends5,$,Creamy vegetarian pumpkin curry,https://link5.com,5,friends5,$,
Why is `split()` doing this?
This one had also driven me mad.
Try using this Inside the function
let endArray = new Set([]);
var str_array = response2.split(',');
for(var i = 0;i < str_array.length - 1;i++){
endArray.add(str_array[i]);
}
console.log(endArray)
Also, It would be easier if you would have added the string that you are splitting, This will help more viewers find better options and in less time.
If this didn't work get back to me on this answer as a comment

calling switch statement from outside of component

I have kind of big switch statement for my weather app and I got a lot of ids going on and I found it makes my main file so big so I wanted to make a file for all of my switch statement and call it in my component but I could not
here is the switch code
export function switchCode (id){
switch(id){
case 200:
case 201:
case 202:
case 210:
case 211:
case 212:
case 221:
case 230:
case 231:
case 232:
console.log('thunder_icon');
break;
case 300:
case 301:
case 302:
case 310:
case 311:
case 312:
case 313:
case 314:
case 321:
console.log('cloud rainy');
break;
case 500:
case 501:
case 502:
case 503:
case 504:
case 511:
case 520:
case 521:
case 522:
case 531:
console.log('rainy with sun');
break;
case 600:
case 601:
case 602:
case 611:
case 612:
case 615:
case 616:
case 620:
case 621:
case 622:
console.log('snow icon');
break;
case 701:
case 711:
case 721:
case 731:
case 741:
case 751:
case 761:
case 762:
case 771:
case 781:
console.log('hot icon or just sun or fog');
break;
case 800:
console.log('big sunny');
break;
case 801:
case 802:
case 803:
case 804:
console.log('sun with cloud');
break;
}}
I tried to access as {this.props.switchCode(500)} and didn't work, just gave me blank space.
please, guys, help
thanks
When you are importing the function switchCode using:
import {switchCode} from './switch_weather_codes';
The switchCode function is available as a local variable. You just need to access it using:
switchCode(id);
Note that this will just call the console.log() function. If you really need the value inside that, change the console.log() to return.
When you try to use this.props.switchCode(id), React tries to find a property that's switchCode, which is not, but just a local variable. Replace the this.props.switchCode(id) with the above code.
Replace console.log with a return statement.
export function switchCode (id){
switch(id){
case 200:
case 201:
case 202:
case 210:
case 211:
case 212:
case 221:
case 230:
case 231:
case 232:
return 'thunder_icon';
break;
...
}}

How exactly are JS Case Switches supposed to be formatted?

So I have been working on a Discord bot for my server for a few days using Node.js and am brand new to JS. I am trying to convert some of the code from a Magic 8 Ball completed after a model for a tutorial to JS to Implement as a function (just the random number and case switch).
This is from the C# Project:
switch (random.Next(4))
{
case 0:
Console.WriteLine("YES");
speechSynthesizer.Speak("Yes");
break;
case 1:
Console.WriteLine("NO");
speechSynthesizer.Speak("No");
break;
case 2:
Console.WriteLine("HELL NO");
speechSynthesizer.Speak("Hell no");
break;
case 3:
speechSynthesizer.Speak("Hell yes");
Console.WriteLine("HELL YES");
break;
}
and this is in what I am trying to implement:
switch( Math.random.Next(4)) {
case 0:
msg.channel.send('Yes');
break;
case 1:
msg.channel.send('No');
break;
case 2:
msg.channel.send('Hell yes!');
break;
case 3:
msg.channel.send('Hell No!');
break;
}
I am essentially trying to add a part where you can send "Magic 8 Ball", and it will return a Yes/No/Hell Yes/Hell No.
Edit:
So I followed the advice of a comment, and switched "case" to all lowercase in each instance. The error I recieve now is that math is undefined. Another article just suggested switching math to Math. next is not a function.
If your problem is generating a random int to use in your switch, you'll need to know how to generate a pseudorandom int in the first place.
Math.random generates a a float in the range of [0, 1)
So you'll need to take that value, multiply it by your max, and then floor it to turn it in to an integer.
Math.floor(Math.random() * max)
Your final code would look like this:
switch(Math.floor(Math.random() * 4))) { // 4 is maximum n of options
case 0:
msg.channel.send('Yes');
break;
case 1:
msg.channel.send('No');
break;
case 2:
msg.channel.send('Hell yes!');
break;
case 3:
msg.channel.send('Hell No!');
break;
}

How to fetch Digits from a String in javascript?

I have a following string in javascript:
var xyz= "M429,100L504.5,100L504.5,106L580,106L570,98M580,106L570,114";
I want to fetch the numbers and store it in an array.
I tried following code:
var x=xyz.match(/\d+/g);
And I got the following output:
0: "429"
1: "100"
2: "504"
3: "5"
4: "100"
5: "504"
6: "5"
7: "106"
8: "580"
9: "106"
10: "570"
11: "98"
12: "580"
13: "106"
14: "570"
15: "114"
As you can see the floating point values such as 504.5 has come up seperately.
How can I fetch this properly?
You can simply change your regex to this one :
var x=xyz.match(/[0-9.]+/g);
It will allow you to capture the number and the float as well.
=> http://www.regexr.com/3b46a
You can change your regEx to this to get floating point values also
var x = xyz.match(/\d+\.*\d/g)
Try this one.
var x=xyz.match(/\d+(\.\d+)?/g);

Javascript switch not working with <option> tag values

I have the following HTML:
<select id="rankBox">
<option value="0">100</option>
<option value="1">150</option>
<option value="2">200</option>
<option value="3">250</option>
<option value="4">300</option>
<option value="5">350</option>
<option value="6">400</option>
</select>
<input type="Submit" id="button1" value="Generate" onclick="doStuff()">
and the following Javascript:
function doStuff() {
var choice = $("#rankBox option:selected").val();
console.log(choice);
switch (choice) {
case 0:
case 1:
colorQ = "^7";
break;
case 2:
colorQ = "^L";
break;
case 3:
colorQ = "^A";
break;
case 4:
colorQ = "^8";
break;
case 5:
colorQ = "^+";
break;
case 6:
colorQ = "^<";
break;
default:
alert("Something went wrong");
break;
}
alert(colorQ);
}
In short, I have a switch statement which is supposed to check the value of the option the user has selected. As you may see, I have added console.log to see if the problem is with acquiring the user's input, but that is not the issue. The issue is the switch statement, which just does not work.
Am I using wrong syntax or something?
jsFiddle: http://jsfiddle.net/3qTHW/2/ (jsFiddle doesn't work in my browser at all, says doStuff() is not defined)
Thanks in advance.
It's not an integer, it's a string, as all values from an element are strings.
You'll have to either parse it as an integer :
function doStuff() {
var choice = parseInt( $("#rankBox option:selected").val(), 10);
....etc
or change the switch to work with strings (you should be trimming)
switch ( $.trim(choice) ) {
case '0':
case '1':
....etc
This was an easy problem, by looking at the code one could see that the line var choice = $("#rankBox option:selected").val(); is adding string to variable choice. And that is the reason none of the case statements worked and the default code executed.
The best way not to run into such problems is to "Debug" your JavaScript code properly. This will save you a lot of precious time and you will find the root cause to each problem yourself.
To debug your code, you can use Firebug extension in Firefox. This will actually stop the code execution for you and guide you line by line telling you the state of each variable at every line of code. So in case of your problem, all I did was add your code in .html file and ran it in Firefox with "Debugger" points specified. The JS code looked like this.
function doStuff() {
var choice = $("#rankBox option:selected").val();
debugger; // This is the point where the debugging starts
console.log(choice); // console.log wont help as it did not tell me the type
switch (choice) { // Here Firebug tells me that choice is actually a string
case 0:
case 1:
colorQ = "^7";
break;
case 2:
colorQ = "^L";
break;
case 3:
colorQ = "^A";
break;
case 4:
colorQ = "^8";
break;
case 5:
colorQ = "^+";
break;
case 6:
colorQ = "^<";
break;
default:
alert("Something went wrong");
break;
}
alert(colorQ);
}
Note I put a "debugger" above console.log(). From this point onward, the code stopped at each line and I could see the value in each variable. This helped me notice string value in choice.
Happy Debugging :)
Use javascript parseInt() method like this :
var choice = parseInt($("#rankBox option:selected").val());

Categories

Resources